使用JOTM实现分布式事务管理(多数据源)
使用spring和hibernate可以很方便的实现一个数据源的事务管理,但是如果需要同时对多个数据源进行事务控制,并且不想使用重量级容器提供的机制的话,可以使用JOTM达到目的.
JOTM的配置十分简单,spring已经内置了对JOTM的支持,
一.
<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean"/>
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="userTransaction"><ref local="jotm"/></property>
</bean>
首先定义如上的两个bean,利用spring对JOTM进行初始化.
二.接下来定义所需的数据源
<bean id="innerMysqlDataSource" class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">
<property name="transactionManager"><ref local="jotm"/></property>
<property name="driverName"><value>oracle.jdbc.driver.OracleDriver</value></property>
<property name="url"><value>jdbc:oracle:thin:@192.168.1.224:1521:speed</value></property>
</bean>
<bean id="dataSource" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
<property name="dataSource"><ref local="innerMysqlDataSource"/></property>
<property name="maxSize"><value>5</value></property>
<property name="minSize"><value>2</value></property>
<property name="user"><value>stms</value></property>
<property name="password"><value>speed</value></property>
</bean>
通过如上的两个bean定义一个数据源,所需的jar在下载的jotm的压缩包中都以自带.
三.如果还需要定义多个数据源的话,就照如上的格式定义即可.
四.定义好数据源后,我们再定义相应的sessionFactory
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>com/vtradex/edi/example/om/oracle/Example.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
</props>
</property>
<property name="jtaTransactionManager">
<ref bean="jotm" />
</property>
</bean>
mySessionFactory使用dataSource这个数据源
五.定义一个进行事务控制的代理
<bean id="abstractTransactionProxy" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributeSource">
<bean class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource"/>
</property>
</bean>
六.以上都是一些准备工作,完成后,接下来就可以对我们自己的需要进行事务控制的bean进行定义了
<bean id="hqlExecutor1" class="com.vtradex.edi.example.util.HqlExecutor" singleton="false">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
<bean id="hqlExecutor2" class="com.vtradex.edi.example.util.HqlExecutor" singleton="false">
<property name="sessionFactory" ref="mySessionFactory2"/>
</bean>
<bean id="tt" parent="abstractTransactionProxy">
<property name="target">
<bean class="com.vtradex.edi.server.service.Tt">
<property name="hqlExec1" ref="hqlExecutor1"/>
<property name="hqlExec2" ref="hqlExecutor2"/>
</bean>
</property>
</bean>
hqlExecutor1使用mySessionFactory来对数据源进行操作
而hqlExecutor2使用mySessionFactory2来对数据源进行操作
tt这个bean中使用了hqlExecutor1和hqlExecutor2来进行操作.
================================================================================
看一下配置实例
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean"/>
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="userTransaction"><ref local="jotm"/></property>
</bean>
<bean id="innerMysqlDataSource" class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">
<property name="transactionManager"><ref local="jotm"/></property>
<property name="driverName"><value>oracle.jdbc.driver.OracleDriver</value></property>
<property name="url"><value>jdbc:oracle:thin:@192.168.1.224:1521:speed</value></property>
</bean>
<bean id="dataSource" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
<property name="dataSource"><ref local="innerMysqlDataSource"/></property>
<property name="maxSize"><value>5</value></property>
<property name="minSize"><value>2</value></property>
<property name="user"><value>stms</value></property>
<property name="password"><value>speed</value></property>
</bean>
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>com/vtradex/edi/example/om/oracle/Example.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
</props>
</property>
<property name="jtaTransactionManager">
<ref bean="jotm" />
</property>
</bean>
<bean id="innerMysqlDataSource2" class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">
<property name="transactionManager"><ref local="jotm"/></property>
<property name="driverName"><value>oracle.jdbc.driver.OracleDriver</value></property>
<property name="url"><value>jdbc:oracle:thin:@192.168.1.224:1521:speed</value></property>
</bean>
<bean id="dataSource2" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
<property name="dataSource"><ref local="innerMysqlDataSource2"/></property>
<property name="maxSize"><value>5</value></property>
<property name="minSize"><value>2</value></property>
<property name="user"><value>swms</value></property>
<property name="password"><value>speed</value></property>
</bean>
<bean id="mySessionFactory2" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource2"/>
<property name="mappingResources">
<list>
<value>com/vtradex/edi/example/om/oracle/Message.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
</props>
</property>
<property name="jtaTransactionManager">
<ref bean="jotm" />
</property>
</bean>
<bean id="hqlExecutor2" class="com.vtradex.edi.example.util.HqlExecutor" singleton="false">
<property name="sessionFactory" ref="mySessionFactory2"/>
</bean>
<bean id="abstractTransactionProxy" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributeSource">
<bean class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource"/>
</property>
</bean>
<bean id="tt" parent="abstractTransactionProxy">
<property name="target">
<bean class="com.vtradex.edi.server.service.Tt">
</bean>
</property>
</bean>
</beans>