原文地址:http://tom-duan.javaeye.com/blog/147594
JOTM(Java Open Transaction Manager)是ObjectWeb的一个开源JTA实现,本身也是开源应用程序服务器JOnAS(Java Open Application Server)的一部分,为其提供JTA分布式事务的功能。Spring对JOTM提供了较好的支持,提供了一个org.springframework.transaction.jta.JotmFactoryBean的支持类,在Spring2.0中也包含了JOTM相关的一些library。
jotm的下载地址为http://jotm.objectweb.org,最新版本为2.0.10.
下载完成后解压缩,然后打开jotm下面conf文件夹,拷贝carol.properties文件到classpath中,并修改这个文件如下
carol.properties
- # do not use CAROL JNDI wrapper
- carol.start.jndi=false
-
- # do not start a name server
- carol.start.ns=false
-
- # Naming Factory
- carol.jndi.java.naming.factory.url.pkgs=org.apache.naming
# do not use CAROL JNDI wrapper
carol.start.jndi=false
# do not start a name server
carol.start.ns=false
# Naming Factory
carol.jndi.java.naming.factory.url.pkgs=org.apache.naming
上面配置文件的目的是不使用JNDI的方式来加载JOTM的配置,当然也可以根据需要选择其它的一些配置,例如JTOM所提供的默认配置。
然后开始在Spring上下文中配置JOTM,在classpath中建立一个ApplicationContext-jotm.xml,配置如下
ApplicationContext-jotm.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"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
-
- <bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean"/>
-
- <bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager">
- <property name="userTransaction" ref="jotm" />
- </bean>
-
- <bean id="ds1" 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="com.mysql.jdbc.Driver" />
- <property name="url" value="jdbc:MySQL://localhost:3306/test" />
- </bean>
- </property>
- <property name="user" value="root" />
- <property name="password" value="admin" />
- </bean>
-
- <bean id="ds2" 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="com.mysql.jdbc.Driver" />
- <property name="url" value="jdbc:MySQL://localhost:3306/test2" />
- </bean>
- </property>
- <property name="user" value="root" />
- <property name="password" value="admin" />
- </bean>
-
- <bean id="template1" class="org.springframework.jdbc.core.JdbcTemplate">
- <property name="dataSource" ref="ds1" />
- </bean>
-
- <bean id="template2" class="org.springframework.jdbc.core.JdbcTemplate">
- <property name="dataSource" ref="ds2" />
- </bean>
-
- <bean id="dao1" class="com.xa.dao.UserDao1">
- <property name="jdbcTemplate">
- <ref bean="template1"></ref>
- </property>
- </bean>
-
- <bean id="dao2" class="com.xa.dao.UserDao2">
- <property name="jdbcTemplate">
- <ref bean="template2"></ref>
- </property>
- </bean>
-
- <bean id="userServiceTarget" class="com.xa.service.UserServiceImpl">
- <property name="dao1" ref="dao1"/>
- <property name="dao2" ref="dao2"/>
- </bean>
-
-
- <bean id="userTest" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
- <property name="transactionManager">
- <ref bean="txManager"/>
- </property>
- <property name="target">
- <ref bean="userServiceTarget"/>
- </property>
- <property name="transactionAttributes">
- <props>
- <prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>
- </props>
- </property>
- </bean>
- </beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean"/>
<bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="userTransaction" ref="jotm" />
</bean>
<bean id="ds1" 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="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:MySQL://localhost:3306/test" />
</bean>
</property>
<property name="user" value="root" />
<property name="password" value="admin" />
</bean>
<bean id="ds2" 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="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:MySQL://localhost:3306/test2" />
</bean>
</property>
<property name="user" value="root" />
<property name="password" value="admin" />
</bean>
<bean id="template1" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="ds1" />
</bean>
<bean id="template2" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="ds2" />
</bean>
<bean id="dao1" class="com.xa.dao.UserDao1">
<property name="jdbcTemplate">
<ref bean="template1"></ref>
</property>
</bean>
<bean id="dao2" class="com.xa.dao.UserDao2">
<property name="jdbcTemplate">
<ref bean="template2"></ref>
</property>
</bean>
<bean id="userServiceTarget" class="com.xa.service.UserServiceImpl">
<property name="dao1" ref="dao1"/>
<property name="dao2" ref="dao2"/>
</bean>
<bean id="userTest" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="txManager"/>
</property>
<property name="target">
<ref bean="userServiceTarget"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>
</props>
</property>
</bean>
</beans>
上面是一个完整的Spring上下文配置,可以看第一个bean “jotm”,实际上引用了Spring内部所提供的对JOTM支持的工厂类,参考下面的配置代码段
- <bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean"/>
<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean"/>
随后,配置了JTA事务管理器,并且在管理器中使用上面所配置的jotm,如下面的代码
- <bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager">
- <property name="userTransaction" ref="jotm" />
- </bean>
<bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="userTransaction" ref="jotm" />
</bean>
再接下来就是配置多个数据源了,使用jotm提供的org.enhydra.jdbc.pool.StandardXAPoolDataSource类,根据类名可以明确地看出它是用以配置多个数据源的啦,配置的代码如下
- <bean id="ds1" 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="com.mysql.jdbc.Driver" />
- <property name="url" value="jdbc:MySQL://localhost:3306/test" />
- </bean>
- </property>
- <property name="user" value="root" />
- <property name="password" value="admin" />
- </bean>
-
- <bean id="ds2" 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="com.mysql.jdbc.Driver" />
- <property name="url" value="jdbc:MySQL://localhost:3306/test2" />
- </bean>
- </property>
- <property name="user" value="root" />
- <property name="password" value="admin" />
- </bean>
<bean id="ds1" 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="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:MySQL://localhost:3306/test" />
</bean>
</property>
<property name="user" value="root" />
<property name="password" value="admin" />
</bean>
<bean id="ds2" 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="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:MySQL://localhost:3306/test2" />
</bean>
</property>
<property name="user" value="root" />
<property name="password" value="admin" />
</bean>
这里配置的两个数据源都连接到本地的mysql,实际上可以连接到不同的db server和不同类型的数据库,已经经过测试,这里为了方便,在本地建立了两个不同的数据库(test,test2)做测试。
随后的配置基本上和普通的Spring上下文配置相同了,根据不同的数据源配置两个jdbcTemplate,两个dao分别引用不同的jdbcTemplate, 将两个dao注入到UserService中, 最后将service纳入事务管理,并在事务代理配置中配置回滚规则,意思为如遇异常,则强制回滚内容。配置如下所示
- <property name="transactionAttributes">
- <props>
- <prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>
- </props>
- </property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>
</props>
</property>
这样,一个使用JOTM JTA事务的简单应用算大致成型了,最后,写一个JUnit,来测试一下结果
TestXa.java
- package com.xa;
-
- import org.springframework.context.ApplicationContext;
- import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
-
- import com.xa.service.UserService;
-
- public class TestXa extends AbstractDependencyInjectionSpringContextTests
- {
- protected String[] getConfigLocations() {
- return new String[] { "classpath:ApplicationContext-jotm.xml" };
- }
-
- public void testInsertBothDatabase() {
- ApplicationContext ctx = this.getApplicationContext();
- UserService ut = (UserService)ctx.getBean("userTest");
- try {
- ut.insertBothDatabase("1", null);
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
package com.xa;
import org.springframework.context.ApplicationContext;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
import com.xa.service.UserService;
public class TestXa extends AbstractDependencyInjectionSpringContextTests
{
protected String[] getConfigLocations() {
return new String[] { "classpath:ApplicationContext-jotm.xml" };
}
public void testInsertBothDatabase() {
ApplicationContext ctx = this.getApplicationContext();
UserService ut = (UserService)ctx.getBean("userTest");
try {
ut.insertBothDatabase("1", null);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
在test中,调用了UserService的insertBothDatabase方法,有两个参数,userId和UserName,另外在方法的实现中调用了两个使用不同数据源dao,分别向两个不同的数据库插入输入,而test2数据库的xa_test表中,name字段是不允许为空的,因此,在插入test2数据库时会失败.
运行这个test,然后察看数据库结果:),test和test2数据库中都没有插入成功,看serviceImpl中的代码可以知道,逻辑上dao1会先于dao2执行,但是由于JTA事务,在dao2插入数据出现异常时整个事务被回滚,由于事务被配置在service层,dao1和dao2都被纳入一个事务进行管理,呵呵。修改一下方法的参数,修改为
- ut.insertBothDatabase("1", "name1");
ut.insertBothDatabase("1", "name1");
然后再试试test看数据库结果,如何?