分布式事务 (spring+jotm+xapool)

JTA: http://en.wikipedia.org/wiki/Java_Transaction_API

分布式事务在企业应用中一般使用EJB或是通过Spring使用J2EE Application Server中的事务管理器。

如果你想在J2SE应用使用分布式事务,一般会选择atomikos或jotm.

以下是在spring中配置jotm的例子,数据源使用xapool.

spring(2.5.6): http://static.springsource.org/spring/docs/2.0.x/reference/transaction.html

jotm(2.2.1): http://forge.ow2.org/projects/jotm/

xapool(1.5.0): http://forge.ow2.org/projects/xapool

在Spring中配置要注意以下几个问题。

1。默认只对RuntimeException和Error进行回滚。

在事务属性中配置需要额外回滚的自定义checked exception,在以下配置中搜索 rollback-for.

2。 应该是在business方法上或是更高层次加上事务AOP,而不是在DAO这层。

<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:security="http://www.ubs.com/schema/security" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd           
           http://www.ubs.com/schema/security http://www.ubs.com/schema/security.xsd">
	
	<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean"
		destroy-method="shutdown" />

	<bean id="dsMaster" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource">
		<property name="dataSource">
			<bean class="org.enhydra.jdbc.standard.StandardXADataSource">				
				<property name="transactionManager">
					<ref local="jotm" />
				</property>				
				<property name="driverName">
					<value>com.mysql.jdbc.Driver</value>
				</property>
				<property name="url">
					<value>jdbc:mysql://localhost:3306/test1</value>
				</property>
			</bean>
		</property>
		<property name="user">
			<value>root</value>
		</property>
		<property name="password">
			<value>Linux</value>
		</property>
	</bean>
	
	<bean id="dsSlave" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource">
		<property name="dataSource">
			<bean class="org.enhydra.jdbc.standard.StandardXADataSource">				
				<property name="transactionManager">
					<ref local="jotm" />
				</property>				
				<property name="driverName">
					<value>com.mysql.jdbc.Driver</value>
				</property>
				<property name="url">
					<value>jdbc:mysql://localhost:3307/test2</value>
				</property>
			</bean>
		</property>
		<property name="user">
			<value>root</value>
		</property>
		<property name="password">
			<value>Linux</value>
		</property>
	</bean>	

	<bean id="txManager"
		class="org.springframework.transaction.jta.JtaTransactionManager">
		<property name="userTransaction">
			<ref local="jotm" />
		</property>
	</bean>
	
	<tx:advice id="txAdvice" transaction-manager="txManager">		
		<tx:attributes>		
			<tx:method name="get*" read-only="true" />
			<tx:method name="*" rollback-for="SQLException,CustomException"/>
		</tx:attributes>		
	</tx:advice>

	<aop:config>
		<aop:pointcut id="bizOperation" expression="execution(* test.mysql.xa.Business.*(..))" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="bizOperation" />
	</aop:config>	
	
	<bean id="business" class="test.mysql.xa.Business">
		<property name="masterDAO" ref="masterDAO"/>
		<property name="slaveDAO" ref="slaveDAO"/>
	</bean>
	
	<bean id="masterDAO" class="test.mysql.xa.dao.MasterDAO">
		<property name="datasource" ref="dsMaster"/>
	</bean>
	
	<bean id="slaveDAO" class="test.mysql.xa.dao.SlaveDAO">
		<property name="datasource" ref="dsSlave"/>
	</bean>
</beans>




猜你喜欢

转载自steven2011.iteye.com/blog/1336902