JTA分布式事务实战(atomikos)

转自http://690360459-qq-com.iteye.com/blog/2007001

最近需要用到分布式事务,研究了下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代码   收藏代码
  1. xml version="1.0" encoding="UTF-8"?>    
  2. <beans xmlns="http://www.springframework.org/schema/beans"    
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"    
  4.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"    
  5.     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"    
  6.     default-lazy-init="true">    
  7.     
  8.       
  9.     <context:component-scan base-package="com.atom.jta.test" />  
  10.     
  11.         
  12.     <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"    
  13.         init-method="init" destroy-method="close">    
  14.         <description>UserTransactionManagerdescription>    
  15.         <property name="forceShutdown">    
  16.             <value>truevalue>    
  17.         property>    
  18.     bean>    
  19.     
  20.     <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">    
  21.         <property name="transactionTimeout" value="300" />    
  22.     bean>    
  23.     
  24.         
  25.     <bean id="springTransactionManager"    
  26.         class="org.springframework.transaction.jta.JtaTransactionManager">    
  27.         <property name="transactionManager">    
  28.             <ref bean="atomikosTransactionManager" />    
  29.         property>    
  30.         <property name="userTransaction">    
  31.             <ref bean="atomikosUserTransaction" />    
  32.         property>    
  33.         <property name="allowCustomIsolationLevels" value="true">  
  34.         property>  
  35.     bean>    
  36.       
  37.       
  38.     <bean id="transactionInterceptor"  
  39.         class="org.springframework.transaction.interceptor.TransactionInterceptor">  
  40.           
  41.         <property name="transactionManager" ref="springTransactionManager" />  
  42.           
  43.         <property name="transactionAttributes">  
  44.             <props>  
  45.                 <prop key="insertTest">PROPAGATION_REQUIRED,ISOLATION_REPEATABLE_READ,-Throwable  
  46.                 prop>  
  47.             props>  
  48.         property>  
  49.     bean>  
  50.       
  51.     <bean  
  52.         class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
  53.         <property name="beanNames">  
  54.             <list>  
  55.                 <value>atomTransactionServiceImplvalue>  
  56.             list>  
  57.         property>  
  58.         <property name="interceptorNames">  
  59.             <list>  
  60.                 <value>transactionInterceptorvalue>  
  61.             list>  
  62.         property>  
  63.     bean>  
  64. beans>    

 

 

2.数据源:

datasource1:

 

Java代码   收藏代码
  1. package com.atom.jta.test;  
  2.   
  3. import java.util.Properties;  
  4.   
  5. import org.springframework.stereotype.Repository;  
  6.   
  7. import com.atomikos.jdbc.AtomikosDataSourceBean;  
  8. @Repository  
  9. public class MasterAtomDatasource extends AtomikosDataSourceBean {  
  10.       
  11.     private static final long serialVersionUID = -2471230875536339311L;  
  12.   
  13.     public MasterAtomDatasource(){  
  14.         Properties prop = new Properties();  
  15.         prop.put("user""root");  
  16.         prop.put("password""");  
  17.         prop.put("URL""jdbc:mysql://127.0.0.1:3306/test?autoReconnect=true");  
  18.         setXaDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlXADataSource");  
  19.         setUniqueResourceName("mysql_ds1");  
  20.         setPoolSize(5);  
  21.         setXaProperties(prop);  
  22.     }  
  23.       
  24. }  

 datasource2:

 

 

Java代码   收藏代码
  1. package com.atom.jta.test;  
  2.   
  3. import java.util.Properties;  
  4.   
  5. import org.springframework.stereotype.Repository;  
  6.   
  7. import com.atomikos.jdbc.AtomikosDataSourceBean;  
  8. @Repository  
  9. public class SlaveAtomDataSource extends AtomikosDataSourceBean {  
  10.   
  11.     private static final long serialVersionUID = -6210394799199416765L;  
  12.     public SlaveAtomDataSource(){  
  13.         Properties prop = new Properties();  
  14.         prop.put("user""root");  
  15.         prop.put("password""");  
  16.         prop.put("URL""jdbc:mysql://127.0.0.1:3306/test1?autoReconnect=true");  
  17.         setXaDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlXADataSource");  
  18.         setUniqueResourceName("mysql_ds2");  
  19.         setPoolSize(5);  
  20.         setXaProperties(prop);  
  21.     }  
  22.       
  23.   
  24. }  

 

 

3.BaseDao:

Java代码   收藏代码
  1. package com.atom.jta.test;  
  2.   
  3. import javax.annotation.Resource;  
  4. import javax.sql.DataSource;  
  5.   
  6. import org.springframework.jdbc.core.JdbcTemplate;  
  7. import org.springframework.stereotype.Repository;  
  8. @Repository  
  9. public class AtomBaseDao {  
  10.     private JdbcTemplate mastTemplate;  
  11.     private JdbcTemplate slaveTemplate;  
  12.       
  13.     public JdbcTemplate getMastTemplate() {  
  14.         return mastTemplate;  
  15.     }  
  16.     @Resource(name="masterAtomDatasource")  
  17.     public void setMastTemplate(DataSource source) {  
  18.         this.mastTemplate = new JdbcTemplate(source);  
  19.     }  
  20.     public JdbcTemplate getSlaveTemplate() {  
  21.         return slaveTemplate;  
  22.     }  
  23.     @Resource(name="slaveAtomDataSource")  
  24.     public void setSlaveTemplate(DataSource source) {  
  25.         this.slaveTemplate = new JdbcTemplate(source);  
  26.     }  
  27.       
  28.       
  29. }  

 4.测试service

Java代码   收藏代码
  1. package com.atom.jta.test;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4.   
  5. public interface AtomTransactionService {  
  6.     public void insertTest(ApplicationContext ctx) throws Exception;  
  7. }  

 

Java代码   收藏代码
  1. package com.atom.jta.test;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.stereotype.Service;  
  5. @Service  
  6. public class AtomTransactionServiceImpl implements AtomTransactionService {  
  7.     public void insertTest(ApplicationContext ctx) throws Exception {  
  8.         AtomBaseDao baseDao = ctx.getBean(AtomBaseDao.class);  
  9.         String str = "xxxx";  
  10.         String masterSql = "insert into demo (name) values "+"('"+str+"')";  
  11.         String slaveSql = "insert into test (name) values "+"('"+str+"')";  
  12.         baseDao.getMastTemplate().execute(masterSql);  
  13.         baseDao.getSlaveTemplate().execute(slaveSql);  
  14.         throw new Exception();  
  15.     }  
  16.   
  17. }  

 5.事务测试

Java代码   收藏代码
  1. package com.atom.jta.test;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. public class AtomTest {  
  7.     public AtomTest(){  
  8.         ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-atomikos.xml");  
  9.         AtomTransactionService service = ctx.getBean(AtomTransactionService.class);  
  10.         try {  
  11.             service.insertTest(ctx);  
  12.         } catch (Exception e) {  
  13.             e.printStackTrace();  
  14.         }  
  15.     }  
  16.       
  17.     public static void main(String[] args) {  
  18.         AtomTest test = new AtomTest();  
  19.         System.out.println("done.....");  
  20.     }  
  21.   
  22. }  

你可能感兴趣的:(高并发,分布式,事务)