JTA分布式事务实战(atomikos)
- - 企业架构 - ITeye博客最近需要用到分布式事务,研究了下jta,使用了atomikos这个jta的实现,使用的是spring3.0,废话少说,直接贴代码. 2.spring配置文件如下:. . . . .
最近需要用到分布式事务,研究了下jta,使用了atomikos这个jta的实现,使用的是spring3.0,废话少说,直接贴代码。
1.使用如下jar包
atomikos-util.3.7.0.jar
cglib-nodep-2.2.2.jar
transactions-3.7.0.jar
transactions-api-3.7.0.jar
transactions-jdbc-3.7.0.jar
transactions-jta-3.7.0.jar
2.spring配置文件如下:
<?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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd" default-lazy-init="true"> <context:component-scan base-package="com.atom.jta.test" /> <!-- 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> <!-- spring 事务管理器 --> <bean id="springTransactionManager" 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"> </property> </bean> <!-- 事务拦截器 --> <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <!-- 配置事务管理器 --> <property name="transactionManager" ref="springTransactionManager" /> <!-- 方法名:要求的事务属性 --> <property name="transactionAttributes"> <props> <prop key="insertTest">PROPAGATION_REQUIRED,ISOLATION_REPEATABLE_READ,-Throwable </prop> </props> </property> </bean> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames"> <list> <value>atomTransactionServiceImpl</value> </list> </property> <property name="interceptorNames"> <list> <value>transactionInterceptor</value> </list> </property> </bean> </beans>
2.数据源:
datasource1:
package com.atom.jta.test; import java.util.Properties; import org.springframework.stereotype.Repository; import com.atomikos.jdbc.AtomikosDataSourceBean; @Repository public class MasterAtomDatasource extends AtomikosDataSourceBean { private static final long serialVersionUID = -2471230875536339311L; public MasterAtomDatasource(){ Properties prop = new Properties(); prop.put("user", "root"); prop.put("password", ""); prop.put("URL", "jdbc:mysql://127.0.0.1:3306/test?autoReconnect=true"); setXaDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"); setUniqueResourceName("mysql_ds1"); setPoolSize(5); setXaProperties(prop); } }
datasource2:
package com.atom.jta.test; import java.util.Properties; import org.springframework.stereotype.Repository; import com.atomikos.jdbc.AtomikosDataSourceBean; @Repository public class SlaveAtomDataSource extends AtomikosDataSourceBean { private static final long serialVersionUID = -6210394799199416765L; public SlaveAtomDataSource(){ Properties prop = new Properties(); prop.put("user", "root"); prop.put("password", ""); prop.put("URL", "jdbc:mysql://127.0.0.1:3306/test1?autoReconnect=true"); setXaDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"); setUniqueResourceName("mysql_ds2"); setPoolSize(5); setXaProperties(prop); } }
3.BaseDao:
package com.atom.jta.test; import javax.annotation.Resource; import javax.sql.DataSource; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; @Repository public class AtomBaseDao { private JdbcTemplate mastTemplate; private JdbcTemplate slaveTemplate; public JdbcTemplate getMastTemplate() { return mastTemplate; } @Resource(name="masterAtomDatasource") public void setMastTemplate(DataSource source) { this.mastTemplate = new JdbcTemplate(source); } public JdbcTemplate getSlaveTemplate() { return slaveTemplate; } @Resource(name="slaveAtomDataSource") public void setSlaveTemplate(DataSource source) { this.slaveTemplate = new JdbcTemplate(source); } }
4.测试service
package com.atom.jta.test; import org.springframework.context.ApplicationContext; public interface AtomTransactionService { public void insertTest(ApplicationContext ctx) throws Exception; }
package com.atom.jta.test; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Service; @Service public class AtomTransactionServiceImpl implements AtomTransactionService { public void insertTest(ApplicationContext ctx) throws Exception { AtomBaseDao baseDao = ctx.getBean(AtomBaseDao.class); String str = "xxxx"; String masterSql = "insert into demo (name) values "+"('"+str+"')"; String slaveSql = "insert into test (name) values "+"('"+str+"')"; baseDao.getMastTemplate().execute(masterSql); baseDao.getSlaveTemplate().execute(slaveSql); throw new Exception(); } }
5.事务测试
package com.atom.jta.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class AtomTest { public AtomTest(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-atomikos.xml"); AtomTransactionService service = ctx.getBean(AtomTransactionService.class); try { service.insertTest(ctx); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { AtomTest test = new AtomTest(); System.out.println("done....."); } }
就是如此简单