环境介绍:atomikos3.8,spring3,hibernate3.6.10
jdbc.properties配置:
#first first.xaDataSourceClassName=oracle.jdbc.xa.client.OracleXADataSource first.jdbc.driver=oracle.jdbc.driver.OracleDriver first.jdbc.url=jdbc:oracle:thin:@192.168.0.2:1521:HYXT first.jdbc.username=*** first.jdbc.password=*** second.xaDataSourceClassName=com.mysql.jdbc.jdbc2.optional.MysqlXADataSource second.jdbc.driver=com.mysql.jdbc.Driver second.jdbc.url=jdbc:mysql://192.168.0.37:52/bs?useUnicode=true&characterEncoding=utf8&pinGlobalTxToPhysicalConnection=true second.jdbc.username=*** second.jdbc.password=*** third.xaDataSourceClassName=com.mysql.jdbc.jdbc2.optional.MysqlXADataSource third.jdbc.driver=com.mysql.jdbc.Driver third.jdbc.url=jdbc:mysql://192.168.0.37:52/third?useUnicode=true&characterEncoding=utf8&pinGlobalTxToPhysicalConnection=true third.jdbc.username=*** third.jdbc.password=***
persistence.xml配置:
<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"> <persistence-unit name="first" transaction-type="JTA"> </persistence-unit> <persistence-unit name="second" transaction-type="JTA"> </persistence-unit> </persistence>
atomikos-hibernate3.xml配置:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:annotation-config /> <aop:aspectj-autoproxy /> <tx:annotation-driven /> <context:component-scan base-package="org.lmc.platform.jta" /> <context:property-placeholder ignore-resource-not-found="true" location="classpath*:/jdbc.properties" /> <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close"> <property name="forceShutdown" value="true" /> </bean> <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp"> <property name="transactionTimeout" value="300" /> </bean> <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"> <property name="transactionManager" ref="atomikosTransactionManager" /> <property name="userTransaction" ref="atomikosUserTransaction" /> <property name="allowCustomIsolationLevels" value="true" /> </bean> <bean id="firstDataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close"> <property name="uniqueResourceName" value="firstDS" /> <property name="xaDataSourceClassName" value="${first.xaDataSourceClassName}" /> <property name="xaProperties"> <props> <prop key="user">${first.jdbc.username}</prop> <prop key="password">${first.jdbc.password}</prop> <prop key="URL">${first.jdbc.url}</prop> </props> </property> <property name="poolSize" value="10" /> <property name="minPoolSize" value="1" /> <property name="maxPoolSize" value="10" /> <property name="testQuery" value="select 1 from dual" /> </bean> <bean name="hibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="properties"> <props> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.current_session_context_class">jta</prop> <prop key="hibernate.transaction.manager_lookup_class">com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup</prop> </props> </property> </bean> <bean id="firstEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/> <property name="persistenceUnitName" value="first" /> <property name="dataSource" ref="firstDataSource" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="generateDdl" value="true"/> </bean> </property> <property name="jpaProperties" ref="hibernateProperties"/> </bean> <bean id="secondDataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close"> <property name="uniqueResourceName" value="secondDS" /> <property name="xaDataSourceClassName" value="${second.xaDataSourceClassName}" /> <property name="xaProperties"> <props> <prop key="user">${second.jdbc.username}</prop> <prop key="password">${second.jdbc.password}</prop> <prop key="URL">${second.jdbc.url}</prop> </props> </property> <property name="poolSize" value="2" /> <property name="minPoolSize" value="1" /> <property name="maxPoolSize" value="5" /> <property name="testQuery" value="select 1" /> </bean> <bean id="secondEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/> <property name="persistenceUnitName" value="second" /> <property name="dataSource" ref="secondDataSource" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="generateDdl" value="true"/> </bean> </property> <property name="jpaProperties" ref="hibernateProperties"/> </bean> <bean id="thirdDataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close"> <property name="uniqueResourceName" value="thirdDS" /> <property name="xaDataSourceClassName" value="${third.xaDataSourceClassName}" /> <property name="xaProperties"> <props> <prop key="user">${third.jdbc.username}</prop> <prop key="password">${third.jdbc.password}</prop> <prop key="URL">${third.jdbc.url}</prop> </props> </property> <property name="poolSize" value="2" /> <property name="minPoolSize" value="1" /> <property name="maxPoolSize" value="5" /> <property name="testQuery" value="select 1" /> </bean> <bean id="thirdSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="thirdDataSource" /> <property name="packagesToScan"> <list> <value>org.**.entity</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.transaction.manager_lookup_class">com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> </bean> </beans>
使用的时候这样写就ok了:
@PersistenceContext(unitName = "first") private EntityManager em ; @Autowired private SessionFactory thirdSessionFactory;
对指定的业务方法加上@Transactional就可以实现事物的管理。
下面是hibernate4的写法atomikos-hibernate4.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:annotation-config /> <aop:aspectj-autoproxy /> <tx:annotation-driven /> <context:component-scan base-package="org.lmc.platform.jta" /> <context:property-placeholder ignore-resource-not-found="true" location="classpath*:/jdbc.properties" /> <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close"> <property name="forceShutdown" value="true" /> </bean> <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp"> <property name="transactionTimeout" value="300" /> </bean> <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"> <property name="transactionManager" ref="atomikosTransactionManager" /> <property name="userTransaction" ref="atomikosUserTransaction" /> <property name="allowCustomIsolationLevels" value="true" /> </bean> <bean id="atomikosJtaPlatform" class="org.lmc.platform.jta.AtomikosJtaPlatform"> <property name="transactionManager" ref="atomikosTransactionManager" /> <property name="userTransaction" ref="atomikosUserTransaction" /> </bean> <bean id="firstDataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close"> <property name="uniqueResourceName" value="firstDS" /> <property name="xaDataSourceClassName" value="${first.xaDataSourceClassName}" /> <property name="xaProperties"> <props> <prop key="user">${first.jdbc.username}</prop> <prop key="password">${first.jdbc.password}</prop> <prop key="URL">${first.jdbc.url}</prop> </props> </property> <property name="maxPoolSize" value="10" /> <property name="testQuery" value="select 1 from dual" /> </bean> <bean name="hibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="properties"> <props> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.CMTTransactionFactory</prop> <prop key="hibernate.transaction.jta.platform">org.lmc.platform.jta.AtomikosJtaPlatform</prop> <prop key="hibernate.current_session_context_class">jta</prop> <prop key="hibernate.transaction.manager_lookup_class">com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup</prop> </props> </property> </bean> <bean id="firstEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/> <property name="persistenceUnitName" value="first" /> <property name="dataSource" ref="firstDataSource" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="generateDdl" value="true"/> </bean> </property> <property name="jpaProperties" ref="hibernateProperties"/> </bean> <bean id="secondDataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close"> <property name="uniqueResourceName" value="secondDS" /> <property name="xaDataSourceClassName" value="${second.xaDataSourceClassName}" /> <property name="xaProperties"> <props> <prop key="user">${second.jdbc.username}</prop> <prop key="password">${second.jdbc.password}</prop> <prop key="URL">${second.jdbc.url}</prop> </props> </property> <property name="maxPoolSize" value="10" /> <property name="testQuery" value="select 1 from dual" /> </bean> <bean id="secondEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/> <property name="persistenceUnitName" value="second" /> <property name="dataSource" ref="secondDataSource" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="generateDdl" value="true"/> </bean> </property> <property name="jpaProperties" ref="hibernateProperties"/> </bean> <bean id="thirdDataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close"> <property name="uniqueResourceName" value="thirdDS" /> <property name="xaDataSourceClassName" value="${third.xaDataSourceClassName}" /> <property name="xaProperties"> <props> <prop key="user">${third.jdbc.username}</prop> <prop key="password">${third.jdbc.password}</prop> <prop key="URL">${third.jdbc.url}</prop> </props> </property> <property name="maxPoolSize" value="10" /> <property name="testQuery" value="select 1 from dual" /> </bean> <bean id="thirdSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="thirdDataSource" /> <property name="packagesToScan"> <list> <value>org.**.entity</value> </list> </property> <property name="hibernateProperties" ref="hibernateProperties"/> </bean> </beans>
hibernate4多了一个类AtomikosJtaPlatform:
package org.lmc.platform.jta; import javax.transaction.TransactionManager; import javax.transaction.UserTransaction; import org.hibernate.engine.transaction.jta.platform.internal.AbstractJtaPlatform; public class AtomikosJtaPlatform extends AbstractJtaPlatform { private static final long serialVersionUID = 4896569440655581002L; private static TransactionManager transactionManager; private static UserTransaction userTransaction; @Override protected TransactionManager locateTransactionManager() { return AtomikosJtaPlatform.transactionManager; } @Override protected UserTransaction locateUserTransaction() { return AtomikosJtaPlatform.userTransaction; } public void setTransactionManager(TransactionManager transactionManager) { AtomikosJtaPlatform.transactionManager = transactionManager; } public void setUserTransaction(UserTransaction userTransaction) { AtomikosJtaPlatform.userTransaction = userTransaction; } }
oracle记着要授权,并且使用11的驱动包,jdbc注意连接的配置,
以sysdba登录数据库,给相应地用户赋予以下权限:
- grant select on sys.dba_pending_transactions to <当前数据库用户>;
- grant select on sys.pending_trans$ to <当前数据库用户>;
- grant select on sys.dba_2pc_pending to <当前数据库用户>;
- grant execute on sys.dbms_system to <当前数据库用户>;
mysql注意使用5以上,加上pinGlobalTxToPhysicalConnection参数。