最近项目中涉及到了两个Oracle数据库的操作。需要用到分布式事务,研究之后贴出代码供大家参考。
1.dao-core.xml(核心事务配置XML)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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">
<!-- 支持注解 -->
<context:annotation-config />
<bean id="jotm"
class="org.springframework.transaction.jta.JotmFactoryBean" />
<bean id="txManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="userTransaction" ref="jotm" />
</bean>
<!--两个数据源-->
<bean id="cmcDataSource" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
<property name="dataSource">
<bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">
<property name="transactionManager" ref="jotm"/>
<property name="driverName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@192.168.6.80:1521:C2SServer"/>
<property name="user" value="d1xn_cmc"/>
<property name="password" value="d1xn_cmc"/>
</bean>
</property>
<property name="user" value="d1xn_cmc"/>
<property name="password" value="d1xn_cmc"/>
</bean>
<bean id="woniuDataSource" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
<property name="dataSource">
<bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">
<property name="transactionManager" ref="jotm"/>
<property name="driverName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@192.168.6.80:1521:C2SServer"/>
<property name="user" value="sn_woniu"/>
<property name="password" value="sn_woniu"/>
</bean>
</property>
<property name="user" value="sn_woniu"/>
<property name="password" value="sn_woniu"/>
</bean>
<!-- =================== 事务管理AOP配置 =================== -->
<bean id="cmcEntityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath:com/d1xn/commercial/dao/config/persistence.xml"/>
<property name="persistenceUnitName" value="CMC_PU"/>
<property name="dataSource" ref="cmcDataSource"/>
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
</property>
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.transaction.manager_lookup_class"
value="org.hibernate.transaction.JOTMTransactionManagerLookup" />
<entry key="hibernate.transaction.flush_before_completion" value="true" />
<entry key="hibernate.transaction.auto_close_session" value="true" />
<entry key="hibernate.current_session_context_class" value="jta" />
<entry key="hibernate.connection.release_mode" value="auto" />
</map>
</property>
</bean>
<bean id="woniuEntityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath:com/d1xn/commercial/dao/config/persistenceWoniu.xml"/>
<property name="persistenceUnitName" value="WONIU_PU"/>
<property name="dataSource" ref="woniuDataSource"/>
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
</property>
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.transaction.manager_lookup_class"
value="org.hibernate.transaction.JOTMTransactionManagerLookup" />
<entry key="hibernate.transaction.flush_before_completion" value="true" />
<entry key="hibernate.transaction.auto_close_session" value="true" />
<entry key="hibernate.current_session_context_class" value="jta" />
<entry key="hibernate.connection.release_mode" value="auto" />
</map>
</property>
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="save*" rollback-for="Excepiton"/>
<tx:method name="delete*" rollback-for="Excepiton"/>
<tx:method name="update*" rollback-for="Excepiton"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="*" rollback-for="java.lang.Exception"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="userOperation"
expression="execution(public * com.d1xn.commercial.dao.imp.*Service*.*(..))"/>
<aop:advisor advice-ref="txAdvice"
pointcut-ref="userOperation"/>
</aop:config>
<!-- ===================== AOP事务配置结束 ====================== -->
<!-- 业务类 -->
<bean id="cmcDaoHelper" class="com.d1xn.dao.core.DAOHelper" init-method="init">
<property name="entityManagerHelper">
<bean class="com.d1xn.commercial.dao.cmc.CmcEntityManagerHelper"/>
</property>
</bean>
<bean id="woniuDaoHelper" class="com.d1xn.dao.core.DAOHelper" init-method="init">
<property name="entityManagerHelper">
<bean class="com.woniu.system.dao.WoniuEntityManagerHelper"/>
</property>
</bean>
<bean id="jtaTestService" class="com.d1xn.commercial.dao.imp.JTATestService">
<property name="cmcDaoHelper" ref="cmcDaoHelper"/>
<property name="woniuDaoHelper" ref="woniuDaoHelper"/>
</bean>
</beans>
2.persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="CMC_PU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.d1xn.commercial.pojo.cmc.entity.GameGoodsType</class>
<class>com.d1xn.commercial.pojo.cmc.entity.ThreeRole</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect" />
<property name="hibernate.show_sql" value="true" />
<!-- 最大连接数 -->
<property name="hibernate.c3p0.max_size" value="20"/>
<!-- 最小连接数 -->
<property name="hibernate.c3p0.min_size" value="5"/>
<!-- 获得连接的超时时间,如果超过这个时间,会抛出异常,单位毫秒 -->
<property name="hibernate.c3p0.timeout" value="120"/>
<!-- 最大的PreparedStatement的数量 -->
<property name="hibernate.c3p0.max_statements" value="100"/>
<!-- 每隔120秒检查连接池里的空闲连接 ,单位是秒-->
<property name="hibernate.c3p0.idle_test_period" value="120"/>
<!-- 当连接池里面的连接用完的时候,C3P0一下获取的新的连接数 -->
<property name="hibernate.c3p0.acquire_increment" value="2"/>
<!-- 每次都验证连接是否可用 -->
<property name="hibernate.c3p0.validate" value="true"/>
</properties>
</persistence-unit>
</persistence>
3.persistenceWoniu.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="WONIU_PU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.woniu.system.pojo.SysCompany</class>
<class>com.woniu.system.pojo.SysUserRole</class>
<class>com.woniu.system.pojo.SysUserRoleId</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect" />
<property name="hibernate.show_sql" value="true" />
<!-- 最大连接数 -->
<property name="hibernate.c3p0.max_size" value="20"/>
<!-- 最小连接数 -->
<property name="hibernate.c3p0.min_size" value="5"/>
<!-- 获得连接的超时时间,如果超过这个时间,会抛出异常,单位毫秒 -->
<property name="hibernate.c3p0.timeout" value="120"/>
<!-- 最大的PreparedStatement的数量 -->
<property name="hibernate.c3p0.max_statements" value="100"/>
<!-- 每隔120秒检查连接池里的空闲连接 ,单位是秒-->
<property name="hibernate.c3p0.idle_test_period" value="120"/>
<!-- 当连接池里面的连接用完的时候,C3P0一下获取的新的连接数 -->
<property name="hibernate.c3p0.acquire_increment" value="2"/>
<!-- 每次都验证连接是否可用 -->
<property name="hibernate.c3p0.validate" value="true"/>
</properties>
</persistence-unit>
</persistence>
4.MyTest-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="classpath:com/d1xn/commercial/dao/config/dao-core.xml"/>
</beans>
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。 不明白为啥,希望高人不吝赐教。