jotm跨数据库 事务
jotm跨数据库 spring + hibernate
软件包
ow2-jotm-dist-2.1.9-bin.tar.gz
spring-framework-3.0.5.RELEASEhibernate-3.6.0.Final
hibernate-3.6.0.Final
spring和hibernate不再详述,讲讲jotm如何与spring集成
web开发中,使用hibernate的SessionFactory,而在SessionFactory中注入DataSource
DataSource需要支持事务,因此采用软件包ow2-jotm-dist-2.1.9-bin.tar.gz中的xapool.jar包来配置DataSource,放弃了org.springframework.jdbc.datasource.DriverManagerDataSource
配置jta事务管理器JtaTransactionManager
重点<property name="userTransaction" ref="jotm" />
Java代码 收藏代码
<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean" />
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="userTransaction" ref="jotm" />
<property name="allowCustomIsolationLevels" value="true"></property>
</bean>
数据源配置(DataSource)
重点<property name="transactionManager" ref="jotm" />
Java代码 收藏代码
//1号数据库
<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 name="driverName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/db1?characterEncoding=utf-8" />
</bean>
</property>
<property name="user" value="root" />
<property name="password" value="" />
</bean>
//2号数据库
<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 name="driverName" value="com.mysql.jdbc.Driver" />
&nbs