Use jotm for Distributed Transaction Management (multiple data sources)

Source: Internet
Author: User

Using spring and hibernate can easily implement transaction management for a data source. However, if you need to control transactions for multiple data sources at the same time and do not want to use the mechanism provided by heavyweight containers, you can use jotm for the purpose.

Jotm configuration is very simple. Spring has built-in support for jotm,
I.

<property name="userTransaction"><ref local="jotm"/></property>

First, define the two beans above and use spring to initialize jotm.

2. Define the required data source.


Oracle. JDBC. Driver. oracledriver
JDBC: oracle: thin: @ 192.168.1.20.: 1521: Speed



5
2
STMs
Speed

Use the preceding two beans to define a data source. The required jar files are included in the downloaded jotm package.

3. If you need to define multiple data sources, just define them in the above format.

4. After defining the data source, we will define the corresponding sessionfactory

<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">



Mysessionfactory uses the datasource Data Source

5. Define a transaction control proxy
<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>

6. All of the above are preparations. After completing these preparations, we can define our own bean for transaction control.

<property name="sessionFactory" ref="mySessionFactory"/>


<property name="sessionFactory" ref="mySessionFactory2"/>

<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 uses mysessionfactory to operate the data source
Hqlexecutor2 uses mysessionfactory2 to operate the data source.

The hqlexecutor1 and hqlexecutor2 are used in the TT bean.

========================================================== ==========================================================

Take a look at the configuration instance




<property name="userTransaction"><ref local="jotm"/></property>




Oracle. JDBC. Driver. oracledriver
JDBC: oracle: thin: @ 192.168.1.20.: 1521: Speed



5
2
STMs
Speed

<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">





Oracle. JDBC. Driver. oracledriver
JDBC: oracle: thin: @ 192.168.1.20.: 1521: Speed



5
2
Swms
Speed

<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">


<property name="sessionFactory" ref="mySessionFactory2"/>

<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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.