使用JOTM實現分佈式事務管理(多數據源)

使用spring和hibernate能夠很方便的實現一個數據源的事務管理,可是若是須要同時對多個數據源進行事務控制,而且不想使用重量級容器提供的機制的話,能夠使用JOTM達到目的.spring

JOTM的配置十分簡單,spring已經內置了對JOTM的支持, 一. <bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean"/>sql

<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"> <property name="userTransaction"><ref local="jotm"/></property> </bean>session

首先定義如上的兩個bean,利用spring對JOTM進行初始化.oracle

二.接下來定義所需的數據源 <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>app

<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的壓縮包中都以自帶.url

三.若是還須要定義多個數據源的話,就照如上的格式定義便可.hibernate

四.定義好數據源後,咱們再定義相應的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這個數據源code

五.定義一個進行事務控制的代理 <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>orm

六.以上都是一些準備工做,完成後,接下來就能夠對咱們本身的須要進行事務控制的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>

相關文章
相關標籤/搜索