关于用spring的JtaTransactionManager,配置分布式事务

如果容器支持jta,那么spring配置起来非常简单,因为spring提供了JtaTransactionManager,在这里面提供了几乎所有的服务器的jta的jndi名字,比如
*   " java:comp/UserTransaction "   for  Resin  2 . x ,  Oracle OC4J (Orion) ,  JOnAS (JOTM) ,  BEA WebLogic
*   " java:comp/TransactionManager "   for  Resin  3 . x
*   " java:appserver/TransactionManager "   for  GlassFish
*   " java:pm/TransactionManager "   for  Borland Enterprise Server and Sun Application Server (Sun ONE  7  and later)
*   " java:/TransactionManager "   for  JBoss Application Server

由于大部分服务器获取jta的方式都不尽相同,spring还针对不同的服务器提供了不同的jta实现,例如:
WebLogicJtaTransactionManager
WebSphereUowTransactionManager
OC4JJtaTransactionManager

具体内容参加:
http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/transaction/jta/JtaTransactionManager.html

不管是jdbc、hibernate还是ibatis,都可以通过JtaTransactionManager来配置分布式事务
下面简单贴出部分配置代码:
hibernate:
< beans >
  
< jee:jndi-lookup  id ="myDataSource1"  jndi-name ="dsjndi1"   />
  
< jee:jndi-lookup  id ="myDataSource2"  jndi-name ="dsjndi2"   />

  
< bean  id ="mySessionFactory1"
        class
="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
        
< property  name ="dataSource" >
            
< ref  bean ="myDataSource1"   />
        
</ property >
        
< property  name ="hibernateProperties" >
            
< props >
                
< prop  key ="hibernate.dialect" > org.hibernate.dialect.MySQLDialect </ prop >
                
< prop  key ="hibernate.show_sql" > true </ prop >
                
< prop  key ="hiberante.format_sql" > true </ prop >
            
</ props >
        
</ property >  
        
< property  name ="annotatedClasses" >
            
< list >
                
< value > com.landor.test.entity.User </ value >
            
</ list >
        
</ property >
        
< property  name ="annotatedPackages" >
            
< list >
                
< value > com.landor.test.entity </ value >
            
</ list >
        
</ property >
  
</ bean >
  
< bean  id ="mySessionFactory2"
        class
="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
        
< property  name ="dataSource" >
            
< ref  bean ="myDataSource2"   />
        
</ property >
        
  
</ bean >

  
< bean  id ="myTxManager"  class ="org.springframework.transaction.jta.JtaTransactionManager" />
  <!--如果默认id=
transactionManager,那么可以有下面的标签,这样spring会自动寻找相应的jta的jndi并且注入
  <tx:jta-transaction-manager/> -->
   < tx:advice  id ="txAdvice"  transaction-manager ="myTxManager" >
    
< tx:attributes >
        
< tx:method  name ="select*"  read-only ="true"  propagation ="REQUIRED" />
        
< tx:method  name ="find*"  read-only ="true"   propagation ="REQUIRED" />
        
< tx:method  name ="save*"   propagation ="REQUIRED"  isolation ="REPEATABLE_READ" />
        
< tx:method  name ="update*"   propagation ="REQUIRED"  isolation ="REPEATABLE_READ" />
        
< tx:method  name ="add*"   propagation ="REQUIRED"  isolation ="REPEATABLE_READ"   />
        
< tx:method  name ="delete*"   propagation ="REQUIRED"  isolation ="REPEATABLE_READ" />
    
</ tx:attributes >
  
</ tx:advice >

</ beans >
ibatis:
ibatis与hibernate是一样的,只需要将hibernate的sessionFactory部分更换一下
    ......
   
< bean  id ="sqlMap1Client"
        class
="org.springframework.orm.ibatis.SqlMapClientFactoryBean" >
        
< property  name ="dataSource"  ref ="myDataSource1"   />
        
< property  name ="configLocation"  value ="xxx/SqlMapConfig.xml"   />
    
</ bean >
    
< bean  id ="sqlMap2Client"
        class
="org.springframework.orm.ibatis.SqlMapClientFactoryBean" >
        
< property  name ="dataSource"  ref ="OssDS"   />
        
< property  name ="configLocation"  value ="xxx/SqlMapConfig.xml"   />
    
</ bean >
    ......



你可能感兴趣的:(关于用spring的JtaTransactionManager,配置分布式事务)