在使用Atomikos過程中遇到的一些問題,以作記錄:
MySQL does not support TMJOIN
MySQL does not allow for joining an existing XA transaction branch, as mentioned here:http://dev.mysql.com/doc/refman/5.0/en/xa-restrictions.html - the consequence is that one transaction accessing the same MySQL multiple times can run into problems like this:
com.atomikos.datasource.ResourceException: resume for XID 192.168.162.50.tm0000100012192.168.162.50.tm1 raised -5: invalid arguments were given for the XA operation at com.atomikos.datasource.xa.XAResourceTransaction.resume(Unknown Source) at com.atomikos.jdbc.ConnectionProxy.invoke(Unknown Source) at $Proxy8.prepareStatement(Unknown Source) at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:505) at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:94) at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:87) at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:222) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2224) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2660) at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:56) at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:234) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141) at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298) at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000) at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:343) at org.hibernate.transaction.JTATransaction.commit(JTATransaction.java:135) [...]
A possible workaround might be to try and start TransactionsEssentials with the parametercom.atomikos.icatch.serial_jta_transactions set to false.
com.atomikos.icatch.serial_jta_transactions=false
Also, make sure to set the following property on the MySQL datasource:
pinGlobalTxToPhysicalConnection="true"
以上是Atomikos官方關於Mysql bug的一些說明,解決這個問題按照官方說明,我們可以在jta.properties或者transactions.properties里面添加com.atomikos.icatch.serial_jta_transactions=false這么一段,可能我姿勢不正確,沒起作用。第二種方法是在Mysql的XADataSource的pinGlobalTxToPhysicalConnection設置為true,這可難辦了,我使用的是druid的數據源,好像沒有pinGlobalTxToPhysicalConnection這個的屬性吧?苦惱了一段時間,查了一些資料都沒有發現解決辦法,突然靈機一動,試一下在url后綴加上這段東西,沒想到真行了。
jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&pinGlobalTxToPhysicalConnection=true
MyBatis不能回滾事務?
2016-10-24 11:07:04,458 INFO [com.atomikos.datasource.xa.XAResourceTransaction] - XAResource.end ( 31302E3232382E37352E3132352E746D30303030313030303134:31302E3232382E37352E3132352E746D31 , XAResource.TMSUCCESS ) on resource apikore represented by XAResource instance com.mysql.jdbc.jdbc2.optional.JDBC4SuspendableXAConnection@12a8ee2
2016-10-24 11:07:04,462 INFO [com.atomikos.datasource.xa.XAResourceTransaction] - XAResource.rollback ( 31302E3232382E37352E3132352E746D30303030313030303134:31302E3232382E37352E3132352E746D31 ) on resource apikore represented by XAResource instance com.mysql.jdbc.jdbc2.optional.JDBC4SuspendableXAConnection@12a8ee2
2016-10-24 11:07:04,466 INFO [com.atomikos.icatch.imp.CompositeTransactionImp] - rollback() done of transaction 127.0.0.1.tm0000100014
檢查一下控制台是否有着段輸出,如果有,那么應該是回滾成功的。如果你斷點spring的TransactionManager確定事務應經啟用的情況下,出錯了卻沒有回滾,請檢查一下,事務設置是否帶有rollback-for="Exception"
<tx:method name="*" propagation="REQUIRED" rollback-for="Exception"/>
Connection pool exhausted - try increasing 'maxPoolSize'
錯誤:
[ERROR][2016-10-26 14:25:31,937][org.hibernate.engine.jdbc.spi.SqlExceptionHelper]Connection pool exhausted - try increasing 'maxPoolSize' and/or 'borrowConnectionTimeout' on the DataSourceBean. [ERROR][2016-10-26 14:25:31,952][org.apache.struts2.dispatcher.Dispatcher]Exception occurred during processing request: Could not open connection
在AtomikosDataSourceBean配置添加如下代碼:
<property name="minPoolSize" value="5" /> <property name="maxPoolSize" value="20" /> <property name="borrowConnectionTimeout" value="60" />
Error in recovery
錯誤:
[ERROR][2016-11-03 10:17:30,771][com.atomikos.recovery.imp.CachedRepository]Corrupted log file - restart JVM com.atomikos.recovery.LogReadException: java.lang.ArrayIndexOutOfBoundsException: 1 at com.atomikos.recovery.imp.FileSystemRepository.getAllCoordinatorLogEntries(FileSystemRepository.java:139) at com.atomikos.recovery.imp.CachedRepository.init(CachedRepository.java:47) at com.atomikos.icatch.provider.imp.AssemblerImp.createCoordinatorLogEntryRepository(AssemblerImp.java:184) at com.atomikos.icatch.provider.imp.AssemblerImp.assembleTransactionService(AssemblerImp.java:135) at com.atomikos.icatch.config.Configuration.assembleSystemComponents(Configuration.java:484) at com.atomikos.icatch.config.Configuration.init(Configuration.java:446) at com.atomikos.icatch.jta.UserTransactionManager.startupTransactionService(UserTransactionManager.java:71) at com.atomikos.icatch.jta.UserTransactionManager.initializeTransactionManagerSingleton(UserTransactionManager.java:60) at com.atomikos.icatch.jta.UserTransactionManager.checkSetup(UserTransactionManager.java:52) at com.atomikos.icatch.jta.UserTransactionManager.init(UserTransactionManager.java:122) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
atomikos版本升級到4.0.4解決。
后續若有發現問題,再行添加。