用Spring集成的JOTM配置分布式事务

【藏自】http://boy00fly.iteye.com/blog/1110655

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

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

 

Xhtml代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"  
  4.        xmlns:aop="http://www.springframework.org/schema/aop"  
  5.        xmlns:context="http://www.springframework.org/schema/context"  
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">  
  7.     <!-- 支持注解 -->  
  8.     <context:annotation-config />  
  9.     <bean id="jotm"  
  10.           class="org.springframework.transaction.jta.JotmFactoryBean" />  
  11.     <bean id="txManager"  
  12.           class="org.springframework.transaction.jta.JtaTransactionManager">  
  13.         <property name="userTransaction" ref="jotm" />  
  14.     </bean>  
  15.     <!--两个数据源-->  
  16.     <bean id="cmcDataSource" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">  
  17.         <property name="dataSource">  
  18.             <bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">  
  19.                 <property name="transactionManager" ref="jotm"/>  
  20.                 <property name="driverName" value="oracle.jdbc.driver.OracleDriver"/>  
  21.                 <property name="url" value="jdbc:oracle:thin:@192.168.6.80:1521:C2SServer"/>  
  22.                 <property name="user" value="d1xn_cmc"/>  
  23.                 <property name="password" value="d1xn_cmc"/>  
  24.             </bean>  
  25.         </property>  
  26.         <property name="user" value="d1xn_cmc"/>  
  27.         <property name="password" value="d1xn_cmc"/>  
  28.     </bean>  
  29.     <bean id="woniuDataSource" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">  
  30.         <property name="dataSource">  
  31.             <bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">  
  32.                 <property name="transactionManager" ref="jotm"/>  
  33.                 <property name="driverName" value="oracle.jdbc.driver.OracleDriver"/>  
  34.                 <property name="url" value="jdbc:oracle:thin:@192.168.6.80:1521:C2SServer"/>  
  35.                 <property name="user" value="sn_woniu"/>  
  36.                 <property name="password" value="sn_woniu"/>  
  37.             </bean>  
  38.         </property>  
  39.         <property name="user" value="sn_woniu"/>  
  40.         <property name="password" value="sn_woniu"/>  
  41.     </bean>  
  42.     <!-- =================== 事务管理AOP配置 =================== -->  
  43.     <bean id="cmcEntityManagerFactory"  
  44.           class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">  
  45.         <property name="persistenceXmlLocation" value="classpath:com/d1xn/commercial/dao/config/persistence.xml"/>  
  46.         <property name="persistenceUnitName" value="CMC_PU"/>  
  47.         <property name="dataSource" ref="cmcDataSource"/>  
  48.         <property name="loadTimeWeaver">  
  49.             <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>  
  50.         </property>  
  51.         <property name="jpaPropertyMap">  
  52.             <map>  
  53.                 <entry key="hibernate.transaction.manager_lookup_class"  
  54.                        value="org.hibernate.transaction.JOTMTransactionManagerLookup" />  
  55.                 <entry key="hibernate.transaction.flush_before_completion" value="true" />  
  56.                 <entry key="hibernate.transaction.auto_close_session" value="true" />  
  57.                 <entry key="hibernate.current_session_context_class" value="jta" />  
  58.                 <entry key="hibernate.connection.release_mode" value="auto" />  
  59.             </map>  
  60.         </property>  
  61.     </bean>  
  62.     <bean id="woniuEntityManagerFactory"  
  63.           class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">  
  64.         <property name="persistenceXmlLocation" value="classpath:com/d1xn/commercial/dao/config/persistenceWoniu.xml"/>  
  65.         <property name="persistenceUnitName" value="WONIU_PU"/>  
  66.         <property name="dataSource" ref="woniuDataSource"/>  
  67.         <property name="loadTimeWeaver">  
  68.             <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>  
  69.         </property>  
  70.         <property name="jpaPropertyMap">  
  71.             <map>  
  72.                 <entry key="hibernate.transaction.manager_lookup_class"  
  73.                        value="org.hibernate.transaction.JOTMTransactionManagerLookup" />  
  74.                 <entry key="hibernate.transaction.flush_before_completion" value="true" />  
  75.                 <entry key="hibernate.transaction.auto_close_session" value="true" />  
  76.                 <entry key="hibernate.current_session_context_class" value="jta" />  
  77.                 <entry key="hibernate.connection.release_mode" value="auto" />  
  78.             </map>  
  79.         </property>  
  80.     </bean>  
  81.     <tx:advice id="txAdvice" transaction-manager="txManager">  
  82.         <tx:attributes>  
  83.             <tx:method name="save*" rollback-for="Excepiton"/>  
  84.             <tx:method name="delete*" rollback-for="Excepiton"/>  
  85.             <tx:method name="update*" rollback-for="Excepiton"/>  
  86.             <tx:method name="find*" read-only="true"/>  
  87.             <tx:method name="*" rollback-for="java.lang.Exception"/>  
  88.         </tx:attributes>  
  89.     </tx:advice>  
  90.     <aop:config>  
  91.         <aop:pointcut id="userOperation"  
  92.                       expression="execution(public  * com.d1xn.commercial.dao.imp.*Service*.*(..))"/>  
  93.         <aop:advisor advice-ref="txAdvice"  
  94.                      pointcut-ref="userOperation"/>  
  95.     </aop:config>  
  96.     <!-- ===================== AOP事务配置结束 ====================== -->  
  97.     <!-- 业务类 -->  
  98.     <bean id="cmcDaoHelper" class="com.d1xn.dao.core.DAOHelper" init-method="init">  
  99.         <property name="entityManagerHelper">  
  100.             <bean class="com.d1xn.commercial.dao.cmc.CmcEntityManagerHelper"/>  
  101.         </property>  
  102.     </bean>  
  103.     <bean id="woniuDaoHelper" class="com.d1xn.dao.core.DAOHelper" init-method="init">  
  104.         <property name="entityManagerHelper">  
  105.             <bean class="com.woniu.system.dao.WoniuEntityManagerHelper"/>  
  106.         </property>  
  107.     </bean>  
  108.     <bean id="jtaTestService" class="com.d1xn.commercial.dao.imp.JTATestService">  
  109.         <property name="cmcDaoHelper" ref="cmcDaoHelper"/>  
  110.         <property name="woniuDaoHelper" ref="woniuDaoHelper"/>  
  111.     </bean>  
  112. </beans>  

 

2.persistence.xml

 

 

Xhtml代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <persistence xmlns="http://java.sun.com/xml/ns/persistence"  
  3.              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.              xsi:schemaLocation="http://java.sun.com/xml/ns/persistence   
  5.     http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"   
  6.              version="1.0">  
  7.     <persistence-unit name="CMC_PU" transaction-type="JTA">  
  8.         <provider>org.hibernate.ejb.HibernatePersistence</provider>  
  9.         <class>com.d1xn.commercial.pojo.cmc.entity.GameGoodsType</class>  
  10.         <class>com.d1xn.commercial.pojo.cmc.entity.ThreeRole</class>  
  11.         <properties>  
  12.            <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect" />  
  13.             <property name="hibernate.show_sql" value="true" />  
  14.             <!-- 最大连接数 -->  
  15.             <property name="hibernate.c3p0.max_size" value="20"/>  
  16.             <!-- 最小连接数 -->  
  17.             <property name="hibernate.c3p0.min_size" value="5"/>  
  18.             <!-- 获得连接的超时时间,如果超过这个时间,会抛出异常,单位毫秒 -->  
  19.             <property name="hibernate.c3p0.timeout" value="120"/>  
  20.             <!-- 最大的PreparedStatement的数量 -->  
  21.             <property name="hibernate.c3p0.max_statements" value="100"/>  
  22.             <!-- 每隔120秒检查连接池里的空闲连接 ,单位是秒-->  
  23.             <property name="hibernate.c3p0.idle_test_period" value="120"/>  
  24.             <!-- 当连接池里面的连接用完的时候,C3P0一下获取的新的连接数 -->  
  25.             <property name="hibernate.c3p0.acquire_increment" value="2"/>  
  26.             <!-- 每次都验证连接是否可用 -->  
  27.             <property name="hibernate.c3p0.validate" value="true"/>  
  28.         </properties>  
  29.     </persistence-unit>  
  30. </persistence>  

 

3.persistenceWoniu.xml

 

 

Xhtml代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <persistence xmlns="http://java.sun.com/xml/ns/persistence"  
  3.              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.              xsi:schemaLocation="http://java.sun.com/xml/ns/persistence   
  5.     http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"   
  6.              version="1.0">  
  7.     <persistence-unit name="WONIU_PU" transaction-type="JTA">  
  8.         <provider>org.hibernate.ejb.HibernatePersistence</provider>  
  9.         <class>com.woniu.system.pojo.SysCompany</class>  
  10.         <class>com.woniu.system.pojo.SysUserRole</class>  
  11.         <class>com.woniu.system.pojo.SysUserRoleId</class>  
  12.         <properties>  
  13.            <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect" />  
  14.             <property name="hibernate.show_sql" value="true" />  
  15.             <!-- 最大连接数 -->  
  16.             <property name="hibernate.c3p0.max_size" value="20"/>  
  17.             <!-- 最小连接数 -->  
  18.             <property name="hibernate.c3p0.min_size" value="5"/>  
  19.             <!-- 获得连接的超时时间,如果超过这个时间,会抛出异常,单位毫秒 -->  
  20.             <property name="hibernate.c3p0.timeout" value="120"/>  
  21.             <!-- 最大的PreparedStatement的数量 -->  
  22.             <property name="hibernate.c3p0.max_statements" value="100"/>  
  23.             <!-- 每隔120秒检查连接池里的空闲连接 ,单位是秒-->  
  24.             <property name="hibernate.c3p0.idle_test_period" value="120"/>  
  25.             <!-- 当连接池里面的连接用完的时候,C3P0一下获取的新的连接数 -->  
  26.             <property name="hibernate.c3p0.acquire_increment" value="2"/>  
  27.             <!-- 每次都验证连接是否可用 -->  
  28.             <property name="hibernate.c3p0.validate" value="true"/>  
  29.         </properties>  
  30.     </persistence-unit>  
  31. </persistence>  

 

4.MyTest-context.xml

 

 

Xhtml代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">  
  5.         
  6.       <import resource="classpath:com/d1xn/commercial/dao/config/dao-core.xml"/>  
  7.        
  8. </beans>  

 

 

5.IJTATestService.java接口

 

Java代码 复制代码
  1. public interface IJTATestService {   
  2.     public boolean saveEntity(IEntity woniu, IEntity cmc);   
  3. }  

 

 

6.JTATestService.java类

 

Java代码 复制代码
  1. public class JTATestService implements IJTATestService {   
  2.     private DAOHelper woniuDaoHelper;   
  3.     private DAOHelper cmcDaoHelper;   
  4.     public boolean saveEntity(IEntity woniu, IEntity cmc) {   
  5.          woniuDaoHelper.persist(woniu);   
  6.         cmcDaoHelper.persist(cmc);   
  7.         return false;   
  8.     }   
  9.     public void setWoniuDaoHelper(DAOHelper woniuDaoHelper) {   
  10.         this.woniuDaoHelper = woniuDaoHelper;   
  11.     }   
  12.     public void setCmcDaoHelper(DAOHelper cmcDaoHelper) {   
  13.         this.cmcDaoHelper = cmcDaoHelper;   
  14.     }   
  15. }  

 

7. MyTest.java

 

 

Java代码 复制代码
  1. @ContextConfiguration  
  2. @RunWith(SpringJUnit4ClassRunner.class)   
  3. public class MyTest {   
  4.     @Autowired  
  5.     private IJTATestService service;   
  6.     @org.junit.Test   
  7.     public void test() {   
  8.         SysCompany sysCompany = new SysCompany();   
  9.         sysCompany.setSName("sss");   
  10.         sysCompany.setSState("0");   
  11.         SysUserRole role = new SysUserRole();   
  12.         SysUserRoleId id = new SysUserRoleId();   
  13.         id.setNRoleId(2L);   
  14.         id.setNUserId(1L);   
  15.         role.setId(id);   
  16.         GameGoodsType type = new GameGoodsType();   
  17.         type.setSNo("aaa");   
  18.         type.setSName("222");   
  19.         type.setSState("1");   
  20.         service.saveEntity(sysCompany, type);   
  21.     }   
  22. }  

 

8.特别说明

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

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)