标签:
使用SpringJTA进行分布式事务管理,需要引入第三方UserTransaction。例如:JOTM、Atomikos
JOTM (Java Open Transaction Manager)是一个独立的开源事务管理器,它实现了 XA 协议并且与 JTA API 兼容。
Atomikos是一个公司的名字,AtomikosTransactionsEssentials是其开源的分布式事务软件包,而ExtremeTransactions是商业的分布式事务软件包。TransactionsEssentials是基于apache-license的,是JTA/XA的开源实现,支持Java Application和J2EE应用。
CREATE DATABASE IF NOT EXISTS testdb_a DEFAULT CHARACTER SET utf8; USE testdb_a; DROP TABLE IF EXISTS tab_a; CREATE TABLE tab_a ( id bigint(20) NOT NULL, name varchar(60) DEFAULT NULL, address varchar(120) DEFAULT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE DATABASE IF NOT EXISTS testdb_b DEFAULT CHARACTER SET utf8; USE testdb_b; DROP TABLE IF EXISTS tab_b; CREATE TABLE tab_b ( id bigint(20) NOT NULL, name varchar(60) DEFAULT NULL, address varchar(120) DEFAULT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
com.atomikos.icatch.service=com.atomikos.icatch.standalone.UserTransactionServiceFactory com.atomikos.icatch.console_file_name = tm.out com.atomikos.icatch.log_base_name = tmlog com.atomikos.icatch.tm_unique_name = com.atomikos.spring.jdbc.tm com.atomikos.icatch.console_log_level = INFO
<?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:jee="http://www.springframework.org/schema/jee" 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-2.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <!--指定Spring配置中用到的属性文件--> <bean id="propertyConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> </list> </property> </bean> <!-- 数据源A --> <bean id="dataSourceA" class="com.atomikos.jdbc.SimpleDataSourceBean" init-method="init" destroy-method="close"> <property name="uniqueResourceName"> <value>mysql/db_a</value> </property> <property name="xaDataSourceClassName"> <value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value> </property> <property name="xaDataSourceProperties"> <value>URL=${jdbc.url};user=${jdbc.username};password=${jdbc.password}</value> </property> <property name="exclusiveConnectionMode"> <value>true</value> </property> <property name="connectionPoolSize"> <value>3</value> </property> <property name="validatingQuery"> <value>SELECT 1</value> </property> </bean> <!-- 数据源B --> <bean id="dataSourceB" class="com.atomikos.jdbc.SimpleDataSourceBean" init-method="init" destroy-method="close"> <property name="uniqueResourceName"> <value>mysql/db_b</value> </property> <property name="xaDataSourceClassName"> <value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value> </property> <property name="xaDataSourceProperties"> <value>URL=${jdbc2.url};user=${jdbc2.username};password=${jdbc2.password}</value> </property> <property name="exclusiveConnectionMode"> <value>true</value> </property> <property name="connectionPoolSize"> <value>3</value> </property> <property name="validatingQuery"> <value>SELECT 1</value> </property> </bean> <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> <!-- JTA事务管理器 --> <bean id="springTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"> <property name="transactionManager" ref="atomikosTransactionManager"/> <property name="userTransaction" ref="atomikosUserTransaction"/> </bean> <!-- 事务切面配置 --> <aop:config> <aop:pointcut id="serviceOperation" expression="execution(* *..service*..*(..))"/> <aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice"/> </aop:config> <!-- 通知配置 --> <tx:advice id="txAdvice" transaction-manager="springTransactionManager"> <tx:attributes> <tx:method name="delete*" rollback-for="Exception"/> <tx:method name="save*" rollback-for="Exception"/> <tx:method name="update*" rollback-for="Exception"/> <tx:method name="*" read-only="true" rollback-for="Exception"/> </tx:attributes> </tx:advice> <!--根据dataSourceA和sql-map-config_A.xml创建一个SqlMapClientA--> <bean id="sqlMapClientA" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="dataSource"> <ref local="dataSourceA"/> </property> <property name="configLocation"> <value>classpath:/sql-map-config_A.xml</value> </property> </bean> <!--根据dataSourceB和sql-map-config_B.xml创建一个SqlMapClientB--> <bean id="sqlMapClientB" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="dataSource"> <ref local="dataSourceB"/> </property> <property name="configLocation"> <value>classpath:/sql-map-config_B.xml</value> </property> </bean> <!--根据sqlMapClientA创建一个SqlMapClientTemplate的模版类实例sqlMapClientTemplateA--> <bean id="sqlMapClientTemplateA" class="org.springframework.orm.ibatis.SqlMapClientTemplate"> <property name="sqlMapClient" ref="sqlMapClientA"/> </bean> <!--根据sqlMapClientB创建一个SqlMapClientTemplate的模版类实例sqlMapClientTemplateB--> <bean id="sqlMapClientTemplateB" class="org.springframework.orm.ibatis.SqlMapClientTemplate"> <property name="sqlMapClient" ref="sqlMapClientB"/> </bean> <!-- 配置DAO,并注入所使用的sqlMapClientTemplate实例 --> <bean id="tabADAO" class="com.lavasoft.stu.atomikos.dao.impl.TabADAOImpl"> <property name="sqlMapClientTemplate" ref="sqlMapClientTemplateA"/> </bean> <bean id="tabBDAO" class="com.lavasoft.stu.atomikos.dao.impl.TabBDAOImpl"> <property name="sqlMapClientTemplate" ref="sqlMapClientTemplateB"/> </bean> <!-- Service配置,注入DAO --> <bean id="stuJotmService" class="com.lavasoft.stu.atomikos.service.StuJotmServiceImpl"> <property name="tabADAO" ref="tabADAO"/> <property name="tabBDAO" ref="tabBDAO"/> </bean> </beans>
标签:
原文地址:http://www.cnblogs.com/duanxz/p/4672640.html