spring3.1+Atomikos 构建jta的分布式事务
第一.下载Atomikos,需要以下这些包
transactions-api.jar(必须) transactions-hibernate2.jar transactions-hibernate3.jar transactions-jdbc-deprecated.jar transactions-jdbc.jar(必须) transactions-jms-deprecated.jar transactions-jms.jar transactions-jta.jar(必须) transactions-osgi.jar(必须) transactions.jar(必须)
可以根据参与事务的资源和使用方式不同选择上面的不用jar。
第二.配置
在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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:task="http://www.springframework.org/schema/task" 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/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/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <context:property-placeholder location="classpath:jdbc.properties" ignore-unresolvable="true" /> <!--数据源 couponDataSource--> <bean id="couponDataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close"> <description>mysql xa datasource</description> <property name="uniqueResourceName"> <value>couponDataSource</value> </property> <property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" /> <property name="xaProperties"> <props> <prop key="user">${luckybee.coupon.jdbc.username}</prop> <prop key="password">${luckybee.coupon.jdbc.password}</prop> <prop key="URL">${luckybee.coupon.jdbc.url}</prop> </props> </property> <property name="poolSize" value="3"/> </bean> <bean id="couponJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" autowire="default"> <property name="dataSource"> <ref bean="couponDataSource" /> </property> </bean> <!--数据源 couponDataSource--> <bean id="shopDataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close"> <description>mysql xa datasource</description> <property name="uniqueResourceName"> <value>shopDataSource</value> </property> <property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" /> <property name="xaProperties"> <props> <prop key="user">${luckybee.shop.jdbc.username}</prop> <prop key="password">${luckybee.shop.jdbc.password}</prop> <prop key="URL">${luckybee.shop.jdbc.url}</prop> </props> </property> <property name="poolSize" value="3"/> </bean> <bean id="shopJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" autowire="default"> <property name="dataSource"> <ref bean="shopDataSource" /> </property> </bean> <!-- atomikos事务管理器 --> <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close"> <description>UserTransactionManager</description> <property name="forceShutdown"> <value>true</value> </property> </bean> <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp"> <property name="transactionTimeout" value="300" /> </bean> <bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager"> <property name="transactionManager"> <ref bean="atomikosTransactionManager" /> </property> <property name="userTransaction"> <ref bean="atomikosUserTransaction"/> </property> <property name="allowCustomIsolationLevels" value="true"/> </bean> <tx:advice id="shopTxAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="get*" read-only="true" /> <tx:method name="query*" read-only="true" /> <tx:method name="find*" read-only="true" /> <tx:method name="insert*" read-only="false" isolation="READ_COMMITTED" rollback-for="Exception" /> <tx:method name="update*" read-only="false" isolation="READ_COMMITTED" rollback-for="Exception" /> <tx:method name="delete*" read-only="false" isolation="READ_COMMITTED" rollback-for="Exception" /> <tx:method name="execute*" read-only="false" isolation="READ_COMMITTED" rollback-for="Exception" /> <tx:method name="add*" read-only="false" isolation="READ_COMMITTED" rollback-for="Exception" /> <tx:method name="modify*" read-only="false" isolation="READ_COMMITTED" rollback-for="Exception" /> <tx:method name="create*" read-only="false" isolation="READ_COMMITTED" rollback-for="Exception" /> <tx:method name="increase*" read-only="false" isolation="READ_COMMITTED" rollback-for="Exception" /> <tx:method name="subPoint*" read-only="false" isolation="READ_COMMITTED" rollback-for="Exception" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="shopServiceOperation" expression="execution(* com.newstar.luckybee.*.service.*.*(..))" /> <aop:advisor advice-ref="shopTxAdvice" pointcut-ref="shopServiceOperation" /> </aop:config> </beans>
第三.在src文件夹下面加入一个jta.properties文件.
文件内容如下,这个文件是必须的.主是是设置atomikos启动的一些参数,比如日志的输出级别,日志文件的名称等.
第四.具体应用
在dao中应用不同的JdbcTemplate即可。
http://gongjiayun.iteye.com/blog/1570111