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

[b]JTA:[/b] [url]http://en.wikipedia.org/wiki/Java_Transaction_API[/url]

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

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

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

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

[b]jotm(2.2.1):[/b] [url]http://forge.ow2.org/projects/jotm/[/url]

[b]xapool(1.5.0):[/b] [url]http://forge.ow2.org/projects/xapool[/url]

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

[b]1。默认只对RuntimeException和Error进行回滚。[/b]

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

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

<?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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值