首先说明,我使用的是jotm来实现跨数据库的事务管理。
第二,在我完成这个测试的时候才发现,其实这个主要还是讲如何使用jotm来做多数据源的事务管理。貌似和spring-test的关系并不大了。至少单元代码上可以说是一模一样。
下面直接看spring配置文件
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean" /> <bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager"> <property name="userTransaction" ref="jotm" /> <property name="allowCustomIsolationLevels" value="true" /> </bean> <bean id="ds1" 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="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:MySQL://localhost:3306/test" /> </bean> </property> <property name="user" value="root" /> <property name="password" value="11" /> </bean> <bean id="ds2" 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="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:MySQL://localhost:3306/test2" /> </bean> </property> <property name="user" value="root" /> <property name="password" value="11" /> </bean> <!-- <bean id="sessionFactory1" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource"> <ref local="ds1" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLInnoDBDialect </prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.jdbc.batch_size">20</prop> </props> </property> <property name="annotatedClasses" value="com.koubei.jotm.model.User" /> </bean> <bean id="sessionFactory2" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource"> <ref local="ds2" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLInnoDBDialect </prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.jdbc.batch_size">20</prop> </props> </property> <property name="annotatedClasses" value="com.koubei.jotm.model.User" /> </bean> --> <bean id="userDao1" class="com.koubei.jotm.dao.UserDaoImpl"> <property name="dataSource" ref="ds1" /> <!-- <property name="sessionFactory" ref="sessionFactory1" /> --> </bean> <bean id="userDao2" class="com.koubei.jotm.dao.UserDao2Impl"> <property name="dataSource" ref="ds2" /> <!-- <property name="sessionFactory" ref="sessionFactory2" /> --> </bean> <bean id="userService" class="com.koubei.jotm.service.UserServiceImpl"> <property name="userDao1" ref="userDao1" /> <property name="userDao2" ref="userDao2" /> </bean> </beans>
注意我使用的是jdbc,而不是hibernate,原因下面会说。
其他的代码不贴了,就贴下UserServiceImpl.java的代码
package com.koubei.jotm.service; import com.koubei.jotm.dao.UserDao; import com.koubei.jotm.dao.UserDao2; import com.koubei.jotm.model.User; /** * @author zhenghui * @version 1.0 * @date 2011-2-22 下午02:57:24 * */ public class UserServiceImpl implements UserService { private UserDao userDao1; private UserDao2 userDao2; public void insertUser(User user) { userDao1.insert(user); userDao2.insert(user); } public UserDao getUserDao1() { return userDao1; } public void setUserDao1(UserDao userDao1) { this.userDao1 = userDao1; } public UserDao2 getUserDao2() { return userDao2; } public void setUserDao2(UserDao2 userDao2) { this.userDao2 = userDao2; } }
最后是最主要的测试代码
package com.koubei.jotm.service; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.annotation.Rollback; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.transaction.TransactionConfiguration; import org.springframework.transaction.annotation.Transactional; import com.koubei.jotm.model.User; /** * @author zhenghui * @version 1.0 * @data 2011-2-11 下午03:22:14 * */ @RunWith(SpringJUnit4ClassRunner.class) //指定测试用例的运行器 这里是指定了Junit4 @ContextConfiguration("classpath:applicationContext.xml") @TransactionConfiguration(transactionManager="txManager",defaultRollback=true) @Transactional() public class TestUserDao { @Autowired private UserService userService; @Test @Rollback(false) public void testSaveUser() { User user = new User(); user.setName("ceshi1"); user.setPassword("hello123"); userService.insertUser(user); } }
经测试,只要设置@Rollback(true) 那么就能支持在不同数据源自动回滚。
最后说明一个问题:最开始的时候我使用的是hibernate,而不是jdbc。最后写单元测试代码的时候发现 @Rollback(true)的时候单元测试通过,事务也会自动回滚,但是@Rollback(false)的时候,就会报org.hibernate.HibernateException: identifier of an instance of com.koubei.jotm.model.User was altered from 10 to 8 错误。网上的资料说明是hibernate在同一个事务中修改对应实体对象的ID,导致出错。找了好多资料,没找到解决的方案。
上传源代码,其中也有我用hibernate的配置(不过已经都注释了)。如果有朋友找到这个问题的解决方案,望告之。万分感谢。