JTA分布式事务实战(atomikos)

阅读更多

最近需要用到分布式事务,研究了下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配置文件如下:

   

  
  
  
  	
  	
  
      
      
        UserTransactionManager  
          
            true  
          
      
  
      
          
      
  
      
      
          
              
          
          
              
          
        
        
      
    
    
	
		
		
		
		
			
				PROPAGATION_REQUIRED,ISOLATION_REPEATABLE_READ,-Throwable
				
			
		
	
	
	
		
			
				atomTransactionServiceImpl
			
		
		
			
				transactionInterceptor
			
		
	
  

 

 

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.....");
	}

}

 就是如此简单

你可能感兴趣的:(jta,spring3.0-jta,atomikos,分布式事务)