我目前正在Tomcat下使用Spring、Hibernate等开发Java应用程序。
我试图使用Atomikos配置JTA,但完成刷新时似乎有问题:(
有很多配置,所以我将主要部分:
<bean id="dataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close">
<property name="xaDataSourceClassName">
<value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value>
</property>
</bean>
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" />
<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.J2eeUserTransaction" />
<bean id="jtaTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" depends-on="userTransactionService,atomikosTransactionManager,atomikosUserTransaction">
<property name="transactionManager" ref="atomikosTransactionManager" />
<property name="userTransaction" ref="atomikosUserTransaction" />
<property name="allowCustomIsolationLevels" value="true"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jtaTransactionManager" ref="jtaTransactionManager"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.use_sql_comments">false</prop>
<prop key="hibernate.current_session_context_class">jta</prop>
</props>
</property>
</bean>
在使用@Transactional启动事务时,我能够运行查询,但是在通过Hibernate getCurrentSession()保存时,我得到以下异常:(当提交完成时)
org.springframework.transaction.UnexpectedRollbackException: JTA transaction unexpectedly rolled back (maybe due to a timeout); nested exception is javax.transaction.RollbackException: The transaction was set to rollback only
at org.springframework.transaction.jta.JtaTransactionManager.doCommit(JtaTransactionManager.java:1024)
..
Caused by: com.atomikos.datasource.ResourceException: XA resource 'XADBMS': resume for XID '31302E38382E36382E32342E746D30303030313030303137:31302E38382E36382E32342E746D31' raised -5: invalid arguments
..
[11:36:30:534 (http-bio-8080-exec-3) org.hibernate.engine.transaction.synchronization.internal.SynchronizationCallbackCoordinatorNonTrackingImpl$2.mapManagedFlushFailure(SynchronizationCallbackCoordinatorNonTrackingImpl.java:179)]
ERROR: HHH000346: Error during managed flush [XA resource 'XADBMS': resume for XID '31302E38382E36382E32342E746D30303030313030303139:31302E38382E36382E32342E746D31' raised -5: invalid arguments were given for the XA operation]
最佳答案
我想我找到了问题所在!这与与MySQL相关的Atomikos bug有关。
在将pinglobaltextophysicalconnection=true参数添加到连接URL或作为com.atomikos.jdbc.AtomikosDataSourceBean Spring定义中的参数之后,它解决了我的问题。
<bean id="dataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean"
init-method="init" destroy-method="close">
...
<property name="xaProperties">
<props>
<prop key="pinGlobalTxToPhysicalConnection">true</prop>
</props>
</property>
</bean>
关于mysql - 带有MySQL JTA的Atomikos无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27729496/