用Spring集成的JOTM配置分布式事务,结合hibernate jpa

阅读更多
最近项目中涉及到了两个Oracle数据库的操作。需要用到分布式事务,研究之后贴出代码供大家参考。

1.dao-core.xml(核心事务配置XML)


    
    
    
    
        
    
    
    
        
            
                
                
                
                
                
            
        
        
        
    
    
        
            
                
                
                
                
                
            
        
        
        
    
    
    
        
        
        
        
            
        
        
            
                
                
                
                
                
            
        
    
    
        
        
        
        
            
        
        
            
                
                
                
                
                
            
        
    
    
        
            
            
            
            
            
        
    
    
        
        
    
    
    
    
        
            
        
    
    
        
            
        
    
    
        
        
    



2.persistence.xml



    
        org.hibernate.ejb.HibernatePersistence
        com.d1xn.commercial.pojo.cmc.entity.GameGoodsType
        com.d1xn.commercial.pojo.cmc.entity.ThreeRole
        
           
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
        
    



3.persistenceWoniu.xml




    
        org.hibernate.ejb.HibernatePersistence
        com.woniu.system.pojo.SysCompany
        com.woniu.system.pojo.SysUserRole
        com.woniu.system.pojo.SysUserRoleId
        
           
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
        
    



4.MyTest-context.xml



     
      
    


5.IJTATestService.java接口

public interface IJTATestService {
	public boolean saveEntity(IEntity woniu, IEntity cmc);
}


6.JTATestService.java类

public class JTATestService implements IJTATestService {
    private DAOHelper woniuDaoHelper;
    private DAOHelper cmcDaoHelper;
    public boolean saveEntity(IEntity woniu, IEntity cmc) {
         woniuDaoHelper.persist(woniu);
        cmcDaoHelper.persist(cmc);
        return false;
    }
    public void setWoniuDaoHelper(DAOHelper woniuDaoHelper) {
        this.woniuDaoHelper = woniuDaoHelper;
    }
    public void setCmcDaoHelper(DAOHelper cmcDaoHelper) {
        this.cmcDaoHelper = cmcDaoHelper;
    }
}


7. MyTest.java


@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class MyTest {
    @Autowired
	private IJTATestService service;
    @org.junit.Test
    public void test() {
        SysCompany sysCompany = new SysCompany();
        sysCompany.setSName("sss");
        sysCompany.setSState("0");
        SysUserRole role = new SysUserRole();
        SysUserRoleId id = new SysUserRoleId();
        id.setNRoleId(2L);
        id.setNUserId(1L);
        role.setId(id);
        GameGoodsType type = new GameGoodsType();
        type.setSNo("aaa");
        type.setSName("222");
        type.setSState("1");
        service.saveEntity(sysCompany, type);
    }
}


8.通过EntityManagerFactory得到sessionFactory

EntityManager entityManager=this.getCmsEntityManagerFactory().createEntityManager();
Session session=(Session)entityManager.getDelegate();
SessionFactory sessionFactory=session.getSessionFactory();


9.特别说明

在项目测试的过程中,有一个错误困扰了我很久,总会报这样的错误

Hibernate: select SN_WONIU.SYS_COMPANY_SQ.nextval from dual
java.sql.SQLException: Cannot get connection for URL jdbc:oracle:thin:@192.168.6.80:1521:C2SServer : 调用中无效的参数

Google了一大圈有不少人碰到这个问题,后来偶是在http://forum.springsource.org/archive/index.php/t-28145.html找到了答案,就是在配置数据源的时候,StandardXAPoolDataSource中要配置user和password,StandardXADataSource中也要配置user和password。 不明白为啥,希望高人不吝赐教。

你可能感兴趣的:(用Spring集成的JOTM配置分布式事务,结合hibernate jpa)