参考:http://huqilong.blog.51cto.com/53638/109113 在Spring中使用JTA事务管理(支持多个数据库事务)
JOTM原是JonAs使用的一个开源JTA事务实现,可以独立出来运行,和spring结合,使得spring具有“脱离容器”的JTA事务管理能力。JOTM可以在官方网站http://jotm.objectweb.org/download/index.html 下载到,下载后需要引用到的包有:carol.jar ,carol-interceptors.jar,howl.jar,jotm-core.jar,ow2-connector-1.5-spec.jar,ow2-jta-1.1-spec.jar,xapool.jar
1. application-context-jta.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:hibernate.properties</value>
</list>
</property>
</bean>
<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="${datasource.driverClassName}" />
<property name="url" value="${datasource.url}" />
</bean>
</property>
<property name="user" value="${datasource.username}" />
<property name="password" value="${datasource.password}" />
</bean>
<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="${datasource.driverClassName}" />
<property name="url" value="${datasource.url2}" />
</bean>
</property>
<property name="user" value="${datasource.username2}" />
<property name="password" value="${datasource.password2}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource1" />
<property name="mappingLocations">
<list>
<value>classpath:com/tftech/club/model/hbm/*.hbm.xml</value>
<value>classpath:com/tftech/rbac/model/*.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
${hibernate.dialect}
</prop>
<prop key="hibernate.show_sql">
${hibernate.show_sql}
</prop>
</props>
</property>
</bean>
<bean id="sessionFactory2"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource2" />
<property name="mappingLocations">
<list>
<value>classpath:com/tftech/club/model/hbm2/*.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
${hibernate.dialect}
</prop>
<prop key="hibernate.show_sql">
${hibernate.show_sql}
</prop>
</props>
</property>
</bean>
<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean" />
<bean id="transactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="userTransaction" ref="jotm" />
</bean>
</beans>
<bean id="activityDao" class="com.tftech.club.admin.dao.impl.ActivityDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="activityBakDao" class="com.tftech.club.admin.dao2.impl.ActivityBakDaoImpl">
<property name="sessionFactory" ref="sessionFactory2" />
</bean>
<bean id="txProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="get*">PROPAGATION_SUPPORTS,readOnly</prop>
<prop key="list*">PROPAGATION_SUPPORTS,readOnly</prop>
<prop key="find*">PROPAGATION_SUPPORTS,readOnly</prop>
</props>
</property>
</bean>
<bean id="activityService" parent="txProxyTemplate">
<property name="target">
<bean class="com.tftech.club.admin.service.impl.ActivityServiceImpl" autowire="byName">
</bean>
</property>
</bean>
2. carol.properties
#JNDI调用协议
carol.protocols=jrmp
#不使用CAROL JNDI封装器
carol.start.jndi=false
#不启动命名服务器
carol.start.ns=false
本文介绍如何在Spring框架中配置和使用JTA事务管理,支持跨多个数据库的数据一致性操作。通过具体配置示例,展示了如何设置数据源、会话工厂及事务管理器等关键组件。
615

被折叠的 条评论
为什么被折叠?



