在Spring中使用JOTM 实现JTA事务管理(支持多个数据库事务)

参考: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 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值