Spring分布式事务实现

阅读更多
    分布式事务是指操作多个数据库之间的事务,spring的org.springframework.transaction.jta.JtaTransactionManager,提供了分布式事务支持。如果使用WAS的JTA支持,把它的属性改为WebSphere对应的TransactionManager。
    在tomcat下,是没有分布式事务的,不过可以借助于第三方软件jotm(Java Open Transaction Manager )和AtomikosTransactionsEssentials实现,在spring中分布式事务是通过jta(jotm,atomikos)来进行实现。
1、http://jotm.objectweb.org/
2、http://www.atomikos.com/Main/TransactionsEssentials

一、使用JOTM例子
(1)、Dao及实现
public interface GenericDao {

	public int save(String ds, String sql, Object[] obj) throws Exception;
	
	public int findRowCount(String ds, String sql);
	
}

public class GenericDaoImpl implements GenericDao{

	private  JdbcTemplate jdbcTemplateA;
	private  JdbcTemplate jdbcTemplateB;

	public void setJdbcTemplateA(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplateA = jdbcTemplate;
	}

	public void setJdbcTemplateB(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplateB = jdbcTemplate;
	}
	
	public int save(String ds, String sql, Object[] obj) throws Exception{
		if(null == ds || "".equals(ds)) return -1;
		try{
			if(ds.equals("A")){
				return this.jdbcTemplateA.update(sql, obj);
			}else{
				return this.jdbcTemplateB.update(sql, obj);
			}
		}catch(Exception e){
			e.printStackTrace();
			throw new Exception("执行" + ds + "数据库时失败!");
		}
	}

	public int findRowCount(String ds, String sql) {
		if(null == ds || "".equals(ds)) return -1;
		
		if(ds.equals("A")){
			return this.jdbcTemplateA.queryForInt(sql);
		}else{
			return this.jdbcTemplateB.queryForInt(sql);
		}
	}

}

(2)、Service及实现
public interface UserService {
	
	public void saveUser() throws Exception;
	
}

public class UserServiceImpl implements UserService{

	private GenericDao genericDao;
	
	public void setGenericDao(GenericDao genericDao) {
		this.genericDao = genericDao;
	}

	public void saveUser() throws Exception {
		String userName = "user_" + Math.round(Math.random()*10000);
		System.out.println(userName);
		
		StringBuilder sql = new StringBuilder();
		sql.append(" insert into t_user(username, gender) values(?,?); ");
		Object[] objs = new Object[]{userName,"1"};
		
		genericDao.save("A", sql.toString(), objs);
		
		sql.delete(0, sql.length());
		sql.append(" insert into t_user(name, sex) values(?,?); ");
		objs = new Object[]{userName,"男的"};//值超出范围
		genericDao.save("B", sql.toString(), objs);
	}

}

(3)、applicationContext-jotm.xml




	springJTA

	 
	 
		 
			 
				classpath:jdbc.properties 
			 
		 
	 
	
	
	
	      
	

	
	   
		   
	

     
     
        
            
                
                
                
            
        
        
        
     

     
     
        
            
                
                
                
            
        
        
        
     

     
          
    
    
     
          
        

	 
	 
		
         
        
        
	 

	 
	 
        
           
           
           
           
        
	 

	
	

	
	



(4)、测试
public class TestUserService{

	private static UserService userService;
	
	@BeforeClass
	public static void init(){
		ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext-jotm.xml");
		userService = (UserService)app.getBean("userService");
	}
	
	@Test
	public void save(){
		System.out.println("begin...");
		try{
			userService.saveUser();
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		System.out.println("finish...");
	}
	
}

二、关于使用atomikos实现
(1)、数据源配置

	
		${datasource.uniqueResourceName}
	
	 
		${database.driver_class} 
	 
	
		URL=${database.url};user=${database.username};password=${database.password} 
	 
	 
		${connection.exclusive.mode} 
	
	 
		${connection.pool.size}
	
	
		${connection.timeout}
	
	 
		SELECT 1 
	 


(2)、事务配置
	 
		 
	 
 
	 
		 
	
 
	 
	 
		 
		 
	
 
	 
	 
		 
		 
	
	
	
	 
		
			 
		 
	 

  • lib.rar (5.2 MB)
  • 描述: JOTM使用包
  • 下载次数: 1074

你可能感兴趣的:(Spring,SQL,配置管理,XML,Websphere)