spring 4+hibernate 3+atomikos3.8
http://timtang.me/blog/2012/12/25/spring-hibernate4-atomiks-jta/
http://fogbugz.atomikos.com/default.asp?community.6.2561.3
http://fogbugz.atomikos.com/default.asp?community.6.2550.5
http://gyfbao.i.sohu.com/blog/view/199984679.htm
问题:http://fogbugz.atomikos.com/default.asp?community.6.1984.7
Please leave out the "hibernate.transaction.factory_class" - read the link Guy wrote or the Hibernate docs linked at the wiki - its not needed anymore.
If you want to set it take the:
org.hibernate.ejb.transaction.JoinableCMTTransactionFactory
but this is "implementation" detail of hibernate - let hibernate choose the "right" JTA factory to use as you do not need to set it anymore.
测试,插入没问题,可是修改及删除去无法进行,去掉
hibernate.transaction.factory_class=org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory
配置后正常。
最后相关配置信息如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.1.xsd
">
<!-- 对包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
<context:component-scan base-package="com.cnge06.aiushtha.service.impl" />
<context:component-scan base-package="com.cnge06.aiushtha.controller" />
<!-- 设定Dao中所使用的 entityManagerFactory-->
<jpa:repositories base-package="com.cnge06.aiushtha.dao" repository-impl-postfix="Impl" entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="globalTransactionManager">
</jpa:repositories>
<context:component-scan base-package="com.cnge06.jta.service.impl" />
<context:component-scan base-package="com.cnge06.jta.controller" />
<jpa:repositories base-package="com.cnge06.jta.dao" repository-impl-postfix="Impl" entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="globalTransactionManager">
</jpa:repositories>
<jpa:repositories base-package="com.cnge06.jta2.dao" repository-impl-postfix="Impl" entity-manager-factory-ref="entityManagerFactory2" transaction-manager-ref="globalTransactionManager">
</jpa:repositories>
<!--配置数据源-->
<bean id="dataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close">
<description>first XA DataSource</description>
<property name="uniqueResourceName">
<value>firstDS</value>
</property>
<property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
<property name="xaProperties">
<props>
<prop key="user">root</prop>
<prop key="password">root</prop>
<prop key="serverName">localhost</prop>
<prop key="portNumber">3306</prop>
<prop key="databaseName">a</prop>
</props>
</property>
<property name="poolSize" value="10"/>
</bean>
<bean id="dataSource2" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close">
<description>second XA DataSource</description>
<property name="uniqueResourceName">
<value>secondDS</value>
</property>
<property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
<property name="xaProperties">
<props>
<prop key="user">root</prop>
<prop key="password">root</prop>
<prop key="serverName">localhost</prop>
<prop key="portNumber">3306</prop>
<prop key="databaseName">a2</prop>
</props>
</property>
<property name="poolSize" value="10"/>
</bean>
<!-- 配置 entityManagerFactory 注意hibernate.transaction.factory_class项被注销-->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="first"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="true" />
<property name="database" value="MYSQL" />
<property name="showSql" value="true"></property>
</bean>
</property>
<!-- 扫描包 entity -->
<property name="packagesToScan">
<list>
<value>com.cnge06.aiushtha.entity</value>
<value>com.cnge06.jta.entity</value>
</list>
</property>
<property name="jpaProperties">
<value>
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.current_session_context_class=jta
hibernate.temp.use_jdbc_metadata_defaults=false
hibernate.connection.release_mode=on_close
hibernate.connection.isolation=3
<!-- hibernate.transaction.factory_class=org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory -->
hibernate.transaction.manager_lookup_class=com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup
</value>
</property>
</bean>
<bean id="entityManagerFactory2"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource2" />
<property name="persistenceUnitName" value="second"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="true" />
<property name="database" value="MYSQL" />
<property name="showSql" value="true"></property>
</bean>
</property>
<!-- 扫描包 entity -->
<property name="packagesToScan">
<list>
<value>com.cnge06.aiushtha.entity</value>
<value>com.cnge06.jta2.entity</value>
</list>
</property>
<property name="jpaProperties">
<value>
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.temp.use_jdbc_metadata_defaults=false
hibernate.current_session_context_class=jta
hibernate.connection.release_mode=on_close
hibernate.connection.isolation=3
<!-- hibernate.transaction.factory_class=org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory -->
hibernate.transaction.manager_lookup_class=com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup
</value>
</property>
</bean>
<!-- 拦截器 -->
<mvc:interceptors>
<!-- *************openSessionInViewInterceptor ************* -->
<bean id="openEntityManagerInterceptor" class="org.springframework.orm.jpa.support.OpenEntityManagerInViewInterceptor">
<property name="entityManagerFactory" ref="entityManagerFactory" /></bean>
<bean id="openEntityManagerInterceptor2" class="org.springframework.orm.jpa.support.OpenEntityManagerInViewInterceptor">
<property name="entityManagerFactory" ref="entityManagerFactory2" /></bean>
</mvc:interceptors>
<!-- 配置jta事务-->
<bean id="globalTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManager" ref="atomikosTransactionManager"/>
<property name="userTransaction" ref="atomikosUserTransaction"/>
</bean>
<!-- 配置atomikos事务管理器-->
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
init-method="init" destroy-method="close">
<description>Atomikos Transaction Manager</description>
<property name="forceShutdown">
<value>true</value>
</property>
</bean>
<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp" >
<property name="transactionTimeout" value="300" />
</bean>
<!-- 事务传播特性 -->
<tx:advice id="txAdvice" transaction-manager="globalTransactionManager">
<tx:attributes>
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 哪些类哪些方法使用事务 -->
<aop:config>
<aop:advisor
pointcut="execution(* com.cnge06.aiushtha.service.impl.*.*(..))"
advice-ref="txAdvice" />
</aop:config>
<aop:config>
<aop:advisor
pointcut="execution(* com.cnge06.jta.service.impl.*.*(..))"
advice-ref="txAdvice" />
</aop:config>
</beans>