选择哪种transaction manager?
在单数据源情况下,JDBC,Hibernate,ibatis等自带的 transaction manager已能用于处理事务。
但当设计多种数据源的事务处理时,上面的transaction manager就没法用了。这个时候可选事务管理组件有:Bitronix http://docs.codehaus.org/display/BTM,SimpleJTA http://simplejta.sourceforge.net/,Tyrex (dead?) http://jotm.objectweb.org/, JOTM (used in Jonas) http://jencks.codehaus.org/Transaction+Manager,GeronimoTM/Jencks (used in Geronimo) http://jencks.codehaus.org/Transaction+Manager,JBossTS (used in JBoss) http://www.jboss.org/jbosstm/ and Atomikos http://www.atomikos.com/. 其中Atomikos 被大多数人所推荐。
参考: http://stackoverflow.com/questions/2978207/atomikos-vs-jotm-vs-bitronix-vs
一个使用 Bitronix的applicationContext.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" 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" default-autowire="byName" default-lazy-init="true"> <context:annotation-config/> <context:component-scan base-package="org.jbpm.dao"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/> </context:component-scan> <context:component-scan base-package="org.jbpm.service"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/> </context:component-scan> <!-- =================================================================== --> <!-- AOP: Configuration and Aspects --> <!-- =================================================================== --> <aop:config> <aop:advisor id="managerTx" advice-ref="txAdvice" pointcut="execution(* *..service.*Manager.*(..))" order="0"/> </aop:config> <tx:advice id="txAdvice"> <tx:attributes> <!-- Read-only commented out to make things easier for end-users --> <!-- http://issues.appfuse.org/browse/APF-556 --> <!--tx:method name="get*" read-only="true"/--> <!--<tx:method name="save*" propagation="REQUIRES_NEW"/>--> <!--<tx:method name="update*" propagation="REQUIRES_NEW"/>--> <!--<tx:method name="remove*" propagation="REQUIRES_NEW"/>--> <!--<tx:method name="get*" read-only="true"/>--> <!--<tx:method name="list*" read-only="true"/>--> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- Enable @Transactional support --> <!--<tx:annotation-driven/>--> <!-- Enable @AspectJ support --> <aop:aspectj-autoproxy/> <!-- Enable @Configured support --> <!--<aop:spring-configured/>--> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> </list> </property> </bean> <bean id="basicdataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <bean id="xadataSource" class="bitronix.tm.resource.jdbc.PoolingDataSource" init-method="init" destroy-method="close"> <property name="className" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" /> <property name="uniqueName" value="jdbc/testDS1" /> <property name="minPoolSize" value="1" /> <property name="maxPoolSize" value="5" /> <property name="allowLocalTransactions" value="true" /> <property name="driverProperties"> <props> <prop key="URL">${jdbc.url}</prop> <prop key="user">${jdbc.username}</prop> <prop key="password">${jdbc.password}</prop> </props> </property> </bean> <bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager"> <property name="transactionManager" ref="bitronixTransactionManager" /> <property name="userTransaction" ref="bitronixTransactionManager" /> </bean> <bean id="bitronixTransactionManager" factory-method="getTransactionManager" class="bitronix.tm.TransactionManagerServices" depends-on="xadataSource,txManager" destroy-method="shutdown" /> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="basicdataSource"/> <property name="persistenceUnitName" value="leaveJPA"/> <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" /> <property name="showSql" value="true" /> <property name="generateDdl" value="false" /> </bean> </property> </bean> <bean id="entityManagerFactoryJbpmPersistanceJpa" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="xadataSource" /> <property name="persistenceUnitName" value="org.jbpm.persistence.jpa" /> <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" /> <property name="showSql" value="true" /> <property name="generateDdl" value="false" /> </bean> </property> </bean> <bean id="entityManagerFactoryJbpmTask" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="basicdataSource" /> <property name="persistenceUnitName" value="org.jbpm.task" /> <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" /> <property name="showSql" value="true" /> <property name="generateDdl" value="false" /> </bean> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory"/> </bean> <bean id="jpaTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactoryJbpmPersistanceJpa"/> </bean> <bean id="taskTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactoryJbpmTask"/> </bean> <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/> <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/> <!--<bean id="md5PasswordEncoder" class="org.springframework.security.authentication.encoding.Md5PasswordEncoder"/>--> </beans>
一个使用 Atomikos的applicationContext.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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <!-- active component annotations like @Service,@Repository,@Component --> <context:component-scan base-package="cmei.mysql" > <!-- <context:include-filter type="regex" expression=".dao.*"/>--> </context:component-scan> <aop:aspectj-autoproxy/> <!-- active annotations like @autowired, @required,... in java class,have to add xmlns:context to beans header--> <!-- <context:annotation-config/> removed after using context:component-scan--> <!--step1:dataSource from connection pool, must use atomikos data source --> <bean id="dataSource11" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close"> <property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" /> <property name="uniqueResourceName" value="mysql/test_transaction"></property> <property name="poolSize" value="10"></property> <property name="xaProperties"> <props> <prop key="url">jdbc:mysql://localhost:3306/test_transaction</prop> <prop key="user">root</prop> <prop key="password"></prop> </props> </property> </bean> <bean id="dataSource22" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close"> <property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" /> <property name="uniqueResourceName" value="mysql/test_transaction2"></property> <property name="poolSize" value="10"></property> <property name="xaProperties"> <props> <prop key="url">jdbc:mysql://localhost:3306/test_transaction2</prop> <prop key="user">root</prop> <prop key="password"></prop> </props> </property> </bean> <!-- step2:sql session:mybatis --> <bean id="sqlSessionFactory1" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource11" /> <property name="mapperLocations" value="/mappers/*.xml" /> <property name="typeAliasesPackage" value="cmei.mysql.dao" /> </bean> <bean id="sqlSessionFactory2" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource22" /> <property name="mapperLocations" value="/mappers/*.xml" /> <property name="typeAliasesPackage" value="cmei.mysql.dao" /> </bean> <!-- step 3:config different daos and service--> <bean id="accountDAO1" class="cmei.mysql.dao.AccountDAO"> <property name="sqlSessionFactory" ref="sqlSessionFactory1"></property> </bean> <bean id="accountDAO2" class="cmei.mysql.dao.AccountDAO"> <property name="sqlSessionFactory" ref="sqlSessionFactory2"></property> </bean> <bean id="accountService" class="cmei.mysql.dao.AccountService"> <property name="accountDAO1" ref="accountDAO1"></property> <property name="accountDAO2" ref="accountDAO2"></property> </bean> <!-- step4:config transactionManager atomikos --> <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close"> <property name="forceShutdown" value="true"/> </bean> <bean id="atomikoUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp"> <property name="transactionTimeout" value="300"></property> </bean> <!-- step5:config spring JTA transactionManager --> <bean id="springTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"> <property name="transactionManager" ref="atomikosTransactionManager"></property> <property name="userTransaction" ref="atomikoUserTransaction"></property> </bean> <!-- step6:config aop --> <tx:annotation-driven transaction-manager="springTransactionManager" proxy-target-class="true" /> <tx:advice id="txAdvice" transaction-manager="springTransactionManager"> <tx:attributes> <tx:method name="update*" rollback-for="Exception"/> <tx:method name="*" read-only="true" rollback-for="Exception"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression="execution(* cmei.mysql.dao.AccountService.transfer(..))" id="serviceOperation"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation"/> </aop:config> </beans>