JBPM5多数据源管理Bitronix和Atomikos
http://blog.csdn.net/taxuexunmei414923794/article/details/7673246
选择哪种transaction manager?
在单数据源情况下,JDBC,Hibernate,ibatis等自带的 transaction manager已能用于处理事务。
选择哪种transaction manager?
在单数据源情况下,JDBC,Hibernate,ibatis等自带的 transaction manager已能用于处理事务。
但当设计多种数据源的事务处理时,上面的transaction manager就没法用了。这个时候可选事务管理组件有:Bitronixhttp://docs.codehaus.org/display/BTM,SimpleJTAhttp://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 Atomikoshttp://www.atomikos.com/. 其中Atomikos 被大多数人所推荐。
参考:http://stackoverflow.com/questions/2978207/atomikos-vs-jotm-vs-bitronix-vs
一个使用Bitronix的applicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
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> |