spring +jotm分布式数据库

首先引入jotm和xpool包,通过maven引入

<dependency>
			<groupId>com.experlog</groupId>
			<artifactId>xapool</artifactId>
			<version>1.5.0</version>
		</dependency>
		<dependency>
			<groupId>org.ow2.jotm</groupId>
			<artifactId>jotm-core</artifactId>
			<version>2.2.2</version>
		</dependency>
		<dependency>
			<groupId>org.ow2.jotm</groupId>
			<artifactId>jotm</artifactId>
			<version>2.2.2</version>
			<type>pom</type>
		</dependency>
		<dependency>
			<groupId>org.ow2.spec.ee</groupId>
			<artifactId>ow2-connector-1.5-spec</artifactId>
			<version>1.0.9</version>
		</dependency>
 

spring配置

<!--JOTM本地实例 -->
<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean" />

<!-- 指定userTransaction属性 -->
<bean id="txManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
	<property name="userTransaction" ref="jotm" />
</bean>

<!-- XAPool配置,内部包含了一个XA数据源,对应database1数据库 -->
	<bean id="dataSource1" 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>
				<property name="driverName" value="${jdbc.driver}"></property>
				<property name="url" value="${jdbc.url1}"></property>
			</bean>
		</property>
		<property name="user" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>

	<bean id="dataSource2" 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>
				<property name="driverName" value="${jdbc.driver}"></property>
				<property name="url" value="${jdbc.url2}"></property>
			</bean>
		</property>
		<property name="user" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>
<bean id="sessionFactory1"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource1" />
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
				<prop key="hibernate.hbm2ddl.auto">${hibernate.auto}</prop>
				<prop key="current_session_context_class">jta</prop>
				<prop key="hibernate.search.default.directory_provider">org.hibernate.search.store.FSDirectoryProvider</prop>
				<prop key="hibernate.search.default.indexBase">F:/temp/index</prop>
			</props>
		</property>
		<property name="annotatedClasses">
			<list>
				<value>com.zyn.ssh.pojo.Student</value>
				<value>com.zyn.ssh.pojo.Teacher</value>
				<value>com.zyn.ssh.pojo.Course</value>
				<value>com.zyn.ssh.pojo.StudentInfo</value>
			</list>
		</property>
	</bean>
	<bean id="sessionFactory2"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource2" />
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
				<prop key="hibernate.hbm2ddl.auto">${hibernate.auto}</prop>
				<prop key="current_session_context_class">jta</prop>
				<prop key="hibernate.search.default.directory_provider">org.hibernate.search.store.FSDirectoryProvider</prop>
				<prop key="hibernate.search.default.indexBase">F:/temp/index</prop>
			</props>
		</property>
		<property name="annotatedClasses">
			<list>
				<value>com.zyn.ssh.pojo.Student</value>
				<value>com.zyn.ssh.pojo.Teacher</value>
				<value>com.zyn.ssh.pojo.Course</value>
				<value>com.zyn.ssh.pojo.StudentInfo</value>
			</list>
		</property>
	</bean>
<!-- 使用annotation定义事务 -->
	<tx:annotation-driven transaction-manager="txManager"
		proxy-target-class="true" />

 测试如下:

//第一个dao中注入
@Resource(name="sessionFactory1")
public void setSessionFactory(final SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
}

//第二个dao中注入

@Resource(name="sessionFactory2")
public void setSessionFactory(SessionFactory sessionFactory) {
	this.sessionFactory = sessionFactory ;
}

@Test
@Transactional
public void save(){
	Student student = new Student("mmmm",20,2008988L);
	dao1.save(student);
	dao2.save(student);
}
 

 

你可能感兴趣的:(spring)