日期:2014-05-16  浏览次数:20458 次

Spring-test做数据库操作的单元测试2-跨库访问

首先说明,我使用的是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" />