spring+hibernate+jotm分布式事务配置总结

两个不同网域的oracle数据库,处理之间的事务,

     spring+hibernate+jotm组合,

一、环境及框架
       Tomcat+spring+hibernate+jotm,struts,Oracle等
二、需求说明
      系统里有2套不同网域的oracle数据库,之间的数据需要进行交互。
三、Dao配置
       1、定义jtom Bean  
  1. < bean   id = "jotm"   class = "org.springframework.transaction.jta.JotmFactoryBean"   />   

       2、定义数据源
  1. < bean   id = "dataSourceA"   class = "org.enhydra.jdbc.pool.StandardXAPoolDataSource"   destroy-method = "shutdown" >   
  2.     < property   name = "dataSource" >   
  3.         < bean   class = "org.enhydra.jdbc.standard.StandardXADataSource"   destroy-method = "shutdown" >   
  4.             < property   name = "transactionManager" >   
  5.                 < ref   local = "jotm"   />   
  6.             </ property >   
  7.             < property   name = "driverName" >   
  8.                 < value > oracle.jdbc.driver.OracleDriver </ value >   
  9.             </ property >   
  10.             < property   name = "url" >   
  11.                 < value > jdbc:oracle:thin:@192.168.0.10:1521:A </ value >   
  12.             </ property >   
  13.         </ bean >   
  14.     </ property >   
  15.     < property   name = "user" >   
  16.         < value > </ value >   
  17.     </ property >   
  18.     < property   name = "password" >   
  19.         < value >   </ value >   
  20.     </ property >   
  21. </ bean >    
  22.   
  23.   
  24. < bean   id = "dataSourceB"   class = "org.enhydra.jdbc.pool.StandardXAPoolDataSource"   destroy-method = "shutdown" >   
  25.     < property   name = "dataSource" >   
  26.         < bean   class = "org.enhydra.jdbc.standard.StandardXADataSource"   destroy-method = "shutdown" >   
  27.             < property   name = "transactionManager" >   
  28.                 < ref   local = "jotm"   />   
  29.             </ property >   
  30.             < property   name = "driverName" >   
  31.                 < value > oracle.jdbc.driver.OracleDriver </ value >   
  32.             </ property >   
  33.             < property   name = "url" >   
  34.                 < value > jdbc:oracle:thin:@192.168.0.10:1521:B </ value >   
  35.             </ property >   
  36.         </ bean >   
  37.     </ property >   
  38.     < property   name = "user" >   
  39.         < value >   </ value >   
  40.     </ property >   
  41.     < property   name = "password" >   
  42.         < value >   </ value >   
  43.     </ property >   
  44. </ bean >    

       3、定义sessionFactory
xml 代码
 
  1. < bean   id = "sessionFactoryA"   class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean" >   
  2.     < property   name = "dataSource" >   
  3.         < ref   local = "dataSourceA"   />   
  4.     </ property >   
  5.     < property   name = "mappingDirectoryLocations" >   
  6.         < list >   
  7.             < value > classpath:/org/testa/hibernate/model/ </ value >   
  8.         </ list >   
  9.     </ property >   
  10.     < property   name = "hibernateProperties" >   
  11.         < props >   
  12.             < prop   key = "hibernate.dialect" > org.hibernate.dialect.Oracle9Dialect </ prop >   
  13.             < prop   key = "hibernate.show_sql" > true </ prop >   
  14.             < prop   key = "hibernate.jdbc.batch_size" > 50 </ prop >   
  15.             < prop   key = "hibernate.cache.use_query_cache" > true </ prop >   
  16.             < prop   key = "hibernate.cache.provider_class" > org.hibernate.cache.EhCacheProvider </ prop >   
  17.         </ props >   
  18.     </ property >   
  19.     < property   name = "jtaTransactionManager" >   
  20.         < ref   bean = "jotm"   />   
  21.     </ property >   
  22.     < property   name = "lobHandler"   ref = "lobHandler" />   
  23. </ bean >        
  24.           
  25. < bean   id = "sessionFactoryB"   class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean" >   
  26.     < property   name = "dataSource" >   
  27.         < ref   local = "dataSourceB"   />   
  28.     </ property >   
  29.     < property   name = "mappingDirectoryLocations" >   
  30.         < list >   
  31.             < value > classpath:/org/testb/hibernate/model/ </ value >   
  32.         </ list >   
  33.     </ property >   
  34.     < property   name = "hibernateProperties" >   
  35.         < props >   
  36.             < prop   key = "hibernate.dialect" > org.hibernate.dialect.Oracle9Dialect </ prop >   
  37.             < prop   key = "hibernate.show_sql" > true </ prop >   
  38.             < prop   key = "hibernate.jdbc.batch_size" > 50 </ prop >   
  39.             < prop   key = "hibernate.cache.use_query_cache" > true </ prop >   
  40.             < prop   key = "hibernate.cache.provider_class" > org.hibernate.cache.EhCacheProvider </ prop >   
  41.         </ props >   
  42.     </ property >   
  43.     < property   name = "jtaTransactionManager" >   
  44.         < ref   bean = "jotm"   />   
  45.     </ property >   
  46.     < property   name = "lobHandler"   ref = "lobHandler" />   
  47. </ bean >   
       4、事务管理配置
xml 代码
 
  1. < bean   id = "myTransactionManager"   
  2.     class = "org.springframework.transaction.jta.JtaTransactionManager" >   
  3.     < property   name = "userTransaction" >   
  4.         < ref   local = "jotm"   />   
  5.     </ property >   
  6. </ bean >   

       5、dao的配置
xml 代码
 
  1. < bean   id = "testADaoTarget"   class = "org.testa.hibernate.dao.impl.TestADAO" >   
  2. < property   name = "sessionFactory" >   
  3.   < ref   bean = " sessionFactoryA " />   
  4. </ property >   
  5. </ bean >   
  6.   
  7. < bean   id = "testADao"   class = "org.springframework.aop.framework.ProxyFactoryBean" >   
  8. < property   name = "proxyInterfaces" >   
  9.   < value > org.testA.hibernate.dao.ITestADAO </ value >   
  10. </ property >   
  11. < property   name = "interceptorNames" >   
  12.   < list >   
  13.     < value >  testADaoTarget  </ value >   
  14.   </ list >   
  15. </ property >   
  16. </ bean >   
  17.   
  18. < bean   id = "testBDaoTarget"   class = "org.testa.hibernate.dao.impl.TestBDAO" >   
  19. < property   name = "sessionFactory" >   
  20.   < ref   bean = " sessionFactoryB " />   
  21. </ property >   
  22. </ bean >   
  23.   
  24. < bean   id = "testBDao"   class = "org.springframework.aop.framework.ProxyFactoryBean" >   
  25. < property   name = "proxyInterfaces" >   
  26.   < value > org.testA.hibernate.dao.ITestBDAO </ value >   
  27. </ property >   
  28. < property   name = "interceptorNames" >   
  29.   < list >   
  30.     < value >  testBDaoTarget  </ value >   
  31.   </ list >   
  32. </ property >   
  33. </ bean >   


四、注意事项
       1、spring已经整合了jotm,如果你使用spring自带的jotm,可能会提示某些类找不到,下载一个最新的jotm包,把lib目录里的所有jar包拷贝到lib目录。

      2、如果系统中使用jndi,则原有的jndi服务可能会无效,提示NameNotFoundException异常,此时在src目录下添加一个carol.properties的属性文件,添加如下内容:
xml 代码
 
  1. # do not use CAROL JNDI wrapper  
  2. carol.start.jndi = false   
  3.   
  4. # do not start a name server  
  5. carol.start.ns = false   
  6.   
  7. # Naming Factory  
  8. carol.jndi.java.naming.factory.url.pkgs = org .apache.naming  

 

 觉得这个地方有点问题啊

<bean id="testADao" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>org.testA.hibernate.dao.ITestADAO</value>
</property>
<property name="interceptorNames">
<list>
<value> testADaoTarget </value>
</list>
</property>
</bean>

<property name="interceptorNames">里面应该是截取器列表,testADaoTarget应放在target属性吧

 

这里的dao只是个代理而已,如你所说,它的执行者最后是testADaoTarget
我的想法是,既然是分布式事务,不应该在dao层进行事务拦截,而应该提升到service层。

上面我把service层省略了,比如我这么写:

Java代码 复制代码
  1. <bean id= "myTransactionManager"   
  2.     class = "org.springframework.transaction.jta.JtaTransactionManager" >  
  3.     <property name="userTransaction" >  
  4.         <ref local="jotm"  />  
  5.     </property>  
  6. </bean>  


Java代码 复制代码
  1. <bean id= "transactionInterceptor"   class = "org.springframework.transaction.interceptor.TransactionInterceptor" >  
  2.   <property name="transactionManager" >  
  3.   <ref bean="myTransactionManager" />  
  4.   </property>  
  5.   
  6. </bean>  



然后你在自己的service层的拦截器列表中添加这个transactionInterceptor拦截器就可以了,由它里边的jtom管理事务。
如:

  1.     <bean id= "testManagerTarget"   class = "org.test.spring.service.impl.testServiceImpl" >      
  2. <property name="testADao" >  
  3.   <ref bean="testADao" />  
  4. </property>     
  5. <property name="testBDao" >  
  6.   <ref bean="testBDao" />  
  7. </property>  
  8. </bean>  
  1. <bean id= "testService"   class = "org.springframework.aop.framework.ProxyFactoryBean" >  
  2.    <property name="proxyInterfaces" >  
  3.    <value>org.test.spring.service.ItestService</value>  
  4.    </property>  
  5.    <property name="interceptorNames" >  
  6.       <list>  
  7.          <idref bean="transactionInterceptor" />          
  8.       </list>  
  9.    </property>  
  10. </bean> 

你可能感兴趣的:(spring+hibernate+jotm分布式事务配置总结)