分布式事务处理-jotm应用

由于项目的需要,初步学习了分布式事务的相关知识,了解了可以和tomcat集成的几款分布式软件,在此基础之上,将事务处理和项目集成到了一起。

 

应用层采用了spring的声明式事务处理方式,经过测试知道sping本身支持单数据源的跨表事务。

集成了jotm后,对跨数据源的事务进行统一管理。经过测试,可以通过配置文件的方式将jotm和spring较好的集成在一起。

 

项目使用的框架:struts2+spring2.5
数据库:mysql5

第三方事务软件:jotm2.2.1

 

下面是配置文件,仅供参考。

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">
	
	
	<bean id="dsDefault" class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">
    	<property name="transactionManager" ref="jotm"/>
        <property name="driverName" value="org.logicalcobwebs.proxool.ProxoolDriver" />
        <property name="url" value="proxool.guopei" />
    </bean>

    
    <bean id="dsTestLocal" class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">
    	<property name="transactionManager" ref="jotm"/>
        <property name="driverName" value="org.logicalcobwebs.proxool.ProxoolDriver" />
        <property name="url" value="proxool.test" />
    </bean>
    
    <bean id="dsTestVm" class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">
    	<property name="transactionManager" ref="jotm"/>
        <property name="driverName" value="org.logicalcobwebs.proxool.ProxoolDriver" />
        <property name="url" value="proxool.vm" />
    </bean>
	
	
	<!--JOTM本地实例-->
	<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean"/>
	
	<!--JTA事务管理器-->
	<bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager">
		<property name="userTransaction" ref="jotm"/><!--指定userTransaction属性引用JOTM本地实例-->
	</bean>
	
	<!--XAPool配置,内部包含了一XA数据源,对应了数据库jtatesta支持JTA事务的数据源,必须封装成XAPool-->
	<bean id="jtaDs" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
		<property name="dataSource">
			<ref local="dsDefault" />
		</property>
	</bean>
	
	
	<!--类似地,对应了数据库jtatestb的XAPool配置,内部包含了一XA数据源-->
	<bean id="jtaTest" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
		<property name="dataSource">
			<ref local="dsTestLocal" />
		</property>
	</bean>	
	
	<bean id="jtaVm" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
		<property name="dataSource">
			<ref local="dsTestVm" />
		</property>
	</bean>
	
	
	<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">	
		<property name="transactionManager" ref="txManager" />
		
		<property name="transactionAttributes">
			<props>
				<prop key="insert*">PROPAGATION_REQUIRED, -Throwable</prop>
				<prop key="*">PROPAGATION_REQUIRED, readOnly</prop>
			</props>
		</property>
	</bean>
	
	<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
		<property name="proxyTargetClass">
         	<value>true</value>
        </property>
		<property name="beanNames">
			<list>
				<value>*Service</value>
			</list>
		</property>
		<property name="interceptorNames">
			<list>
				<value>transactionInterceptor</value>
			</list>
		</property>
	</bean>
	
	
	<bean id="singleDsService" class="com.test.service.SingleDsService">
	</bean>
	
	
	<bean id="dsMultiService" class="com.test.service.DsMultiService">
		<property name="dsLocalGuopeiDao" ref="dsLocalGuopeiDao"/>
		<property name="dsLocalTestDao" ref="dsLocalTestDao"/>
		<property name="dsVmTestDao" ref="dsVmTestDao"/>
	</bean>
	
	
	<bean id="dsLocalGuopeiDao" class="com.test.dao.DsLocalGuopeiDao">
		<property name="dataSource" ref="jtaDs" />
	</bean>
	
	<bean id="dsLocalTestDao" class="com.test.dao.DsLocalTestDao">
		<property name="dataSource" ref="jtaTest" />
	</bean>
	
	<bean id="dsVmTestDao" class="com.test.dao.DsVmTestDao">
		<property name="dataSource" ref="jtaVm" />
	</bean>
	
</beans>

 

你可能感兴趣的:(分布式事务)