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>