关键字: jta jotm atomikos
上节建立了一个简单的Java Application以及所需要的数据库和数据表,本节将介绍JOTM在Spring中的配置。
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
Java 代码
1. # do not use CAROL JNDI wrapper
2. carol.start.jndi=false
3.
4. # do not start a name server
5. carol.start.ns=false
6.
7. # Naming Factory
8. 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
Java 代码
1. <?xml version="1.0" encoding="UTF-8"?>
2. <beans xmlns="http://www.springframework.org/schema/beans"
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
5.
6. <bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean"/>
7.
8. <bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager">
9. <property name="userTransaction" ref="jotm" />
10. </bean>
11.
12. <bean id="ds1" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
13. <property name="dataSource">
14. <bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">
15. <property name="transactionManager" ref="jotm" />
16. <property name="driverName" value="com.mysql.jdbc.Driver" />
17. <property name="url" value="jdbc:MySQL://localhost:3306/test" />
18. </bean>
19. </property>
20. <property name="user" value="root" />
21. <property name="password" value="admin" />
22. </bean>
23.
24. <bean id="ds2" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
25. <property name="dataSource">
26. <bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">
27. <property name="transactionManager" ref="jotm" />
28. <property name="driverName" value="com.mysql.jdbc.Driver" />
29. <property name="url" value="jdbc:MySQL://localhost:3306/test2" />
30. </bean>
31. </property>
32. <property name="user" value="root" />
33. <property name="password" value="admin" />
34. </bean>
35.
36. <bean id="template1" class="org.springframework.jdbc.core.JdbcTemplate">
37. <property name="dataSource" ref="ds1" />
38. </bean>
39.
40. <bean id="template2" class="org.springframework.jdbc.core.JdbcTemplate">
41. <property name="dataSource" ref="ds2" />
42. </bean>
43.
44. <bean id="dao1" class="com.xa.dao.UserDao1">
45. <property name="jdbcTemplate">
46. <ref bean="template1"></ref>
47. </property>
48. </bean>
49.
50. <bean id="dao2" class="com.xa.dao.UserDao2">
51. <property name="jdbcTemplate">
52. <ref bean="template2"></ref>
53. </property>
54. </bean>
55.
56. <bean id="userServiceTarget" class="com.xa.service.UserServiceImpl">
57. <property name="dao1" ref="dao1"/>
58. <property name="dao2" ref="dao2"/>
59. </bean>
60.
61.
62. <bean id="userTest" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
63. <property name="transactionManager">
64. <ref bean="txManager"/>
65. </property>
66. <property name="target">
67. <ref bean="userServiceTarget"/>
68. </property>
69. <property name="transactionAttributes">
70. <props>
71. <prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>
72. </props>
73. </property>
74. </bean>
75. </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支持的工厂类,参考下面的配置代码段
Java 代码
1. <bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean"/>
<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean"/>
随后,配置了JTA事务管理器,并且在管理器中使用上面所配置的jotm,如下面的代码
Java 代码
1. <bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager">
2. <property name="userTransaction" ref="jotm" />
3. </bean>
<bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="userTransaction" ref="jotm" />
</bean>
再接下来就是配置多个数据源了,使用jotm提供的 org.enhydra.jdbc.pool.StandardXAPoolDataSource类,根据类名可以明确地看出它是用以配置多个数据源的啦,配置的代码如下
Java 代码
1. <bean id="ds1" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
2. <property name="dataSource">
3. <bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">
4. <property name="transactionManager" ref="jotm" />
5. <property name="driverName" value="com.mysql.jdbc.Driver" />
6. <property name="url" value="jdbc:MySQL://localhost:3306/test" />
7. </bean>
8. </property>
9. <property name="user" value="root" />
10. <property name="password" value="admin" />
11. </bean>
12.
13. <bean id="ds2" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
14. <property name="dataSource">
15. <bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">
16. <property name="transactionManager" ref="jotm" />
17. <property name="driverName" value="com.mysql.jdbc.Driver" />
18. <property name="url" value="jdbc:MySQL://localhost:3306/test2" />
19. </bean>
20. </property>
21. <property name="user" value="root" />
22. <property name="password" value="admin" />
23. </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纳入事务管理,并在事务代理配置中配置回滚规则,意思为如遇异常,则强制回滚内容。配置如下所示
Java 代码
1. <property name="transactionAttributes">
2. <props>
3. <prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>
4. </props>
5. </property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>
</props>
</property>
这样,一个使用JOTM JTA事务的简单应用算大致成型了,最后,写一个JUnit,来测试一下结果
TestXa.java
Java 代码
1. package com.xa;
2.
3. import org.springframework.context.ApplicationContext;
4. import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
5.
6. import com.xa.service.UserService;
7.
8. public class TestXa extends AbstractDependencyInjectionSpringContextTests
9. {
10. protected String[] getConfigLocations() {
11. return new String[] { "classpath:ApplicationContext-jotm.xml" };
12. }
13.
14. public void testInsertBothDatabase() {
15. ApplicationContext ctx = this.getApplicationContext();
16. UserService ut = (UserService)ctx.getBean("userTest");
17. try {
18. ut.insertBothDatabase("1", null);
19. }
20. catch (Exception e) {
21. e.printStackTrace();
22. }
23. }
24. }
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 都被纳入一个事务进行管理,呵呵。修改一下方法的参数,修改为
Java 代码
1. ut.insertBothDatabase("1", "name1");
ut.insertBothDatabase("1", "name1");
然后再试试test看数据库结果,如何?
第三节将简单介绍Atomikos在Spring中的配置和使用。
上节建立了一个简单的Java Application以及所需要的数据库和数据表,本节将介绍JOTM在Spring中的配置。
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
Java 代码
1. # do not use CAROL JNDI wrapper
2. carol.start.jndi=false
3.
4. # do not start a name server
5. carol.start.ns=false
6.
7. # Naming Factory
8. 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
Java 代码
1. <?xml version="1.0" encoding="UTF-8"?>
2. <beans xmlns="http://www.springframework.org/schema/beans"
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
5.
6. <bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean"/>
7.
8. <bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager">
9. <property name="userTransaction" ref="jotm" />
10. </bean>
11.
12. <bean id="ds1" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
13. <property name="dataSource">
14. <bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">
15. <property name="transactionManager" ref="jotm" />
16. <property name="driverName" value="com.mysql.jdbc.Driver" />
17. <property name="url" value="jdbc:MySQL://localhost:3306/test" />
18. </bean>
19. </property>
20. <property name="user" value="root" />
21. <property name="password" value="admin" />
22. </bean>
23.
24. <bean id="ds2" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
25. <property name="dataSource">
26. <bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">
27. <property name="transactionManager" ref="jotm" />
28. <property name="driverName" value="com.mysql.jdbc.Driver" />
29. <property name="url" value="jdbc:MySQL://localhost:3306/test2" />
30. </bean>
31. </property>
32. <property name="user" value="root" />
33. <property name="password" value="admin" />
34. </bean>
35.
36. <bean id="template1" class="org.springframework.jdbc.core.JdbcTemplate">
37. <property name="dataSource" ref="ds1" />
38. </bean>
39.
40. <bean id="template2" class="org.springframework.jdbc.core.JdbcTemplate">
41. <property name="dataSource" ref="ds2" />
42. </bean>
43.
44. <bean id="dao1" class="com.xa.dao.UserDao1">
45. <property name="jdbcTemplate">
46. <ref bean="template1"></ref>
47. </property>
48. </bean>
49.
50. <bean id="dao2" class="com.xa.dao.UserDao2">
51. <property name="jdbcTemplate">
52. <ref bean="template2"></ref>
53. </property>
54. </bean>
55.
56. <bean id="userServiceTarget" class="com.xa.service.UserServiceImpl">
57. <property name="dao1" ref="dao1"/>
58. <property name="dao2" ref="dao2"/>
59. </bean>
60.
61.
62. <bean id="userTest" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
63. <property name="transactionManager">
64. <ref bean="txManager"/>
65. </property>
66. <property name="target">
67. <ref bean="userServiceTarget"/>
68. </property>
69. <property name="transactionAttributes">
70. <props>
71. <prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>
72. </props>
73. </property>
74. </bean>
75. </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支持的工厂类,参考下面的配置代码段
Java 代码
1. <bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean"/>
<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean"/>
随后,配置了JTA事务管理器,并且在管理器中使用上面所配置的jotm,如下面的代码
Java 代码
1. <bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager">
2. <property name="userTransaction" ref="jotm" />
3. </bean>
<bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="userTransaction" ref="jotm" />
</bean>
再接下来就是配置多个数据源了,使用jotm提供的 org.enhydra.jdbc.pool.StandardXAPoolDataSource类,根据类名可以明确地看出它是用以配置多个数据源的啦,配置的代码如下
Java 代码
1. <bean id="ds1" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
2. <property name="dataSource">
3. <bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">
4. <property name="transactionManager" ref="jotm" />
5. <property name="driverName" value="com.mysql.jdbc.Driver" />
6. <property name="url" value="jdbc:MySQL://localhost:3306/test" />
7. </bean>
8. </property>
9. <property name="user" value="root" />
10. <property name="password" value="admin" />
11. </bean>
12.
13. <bean id="ds2" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
14. <property name="dataSource">
15. <bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">
16. <property name="transactionManager" ref="jotm" />
17. <property name="driverName" value="com.mysql.jdbc.Driver" />
18. <property name="url" value="jdbc:MySQL://localhost:3306/test2" />
19. </bean>
20. </property>
21. <property name="user" value="root" />
22. <property name="password" value="admin" />
23. </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纳入事务管理,并在事务代理配置中配置回滚规则,意思为如遇异常,则强制回滚内容。配置如下所示
Java 代码
1. <property name="transactionAttributes">
2. <props>
3. <prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>
4. </props>
5. </property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>
</props>
</property>
这样,一个使用JOTM JTA事务的简单应用算大致成型了,最后,写一个JUnit,来测试一下结果
TestXa.java
Java 代码
1. package com.xa;
2.
3. import org.springframework.context.ApplicationContext;
4. import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
5.
6. import com.xa.service.UserService;
7.
8. public class TestXa extends AbstractDependencyInjectionSpringContextTests
9. {
10. protected String[] getConfigLocations() {
11. return new String[] { "classpath:ApplicationContext-jotm.xml" };
12. }
13.
14. public void testInsertBothDatabase() {
15. ApplicationContext ctx = this.getApplicationContext();
16. UserService ut = (UserService)ctx.getBean("userTest");
17. try {
18. ut.insertBothDatabase("1", null);
19. }
20. catch (Exception e) {
21. e.printStackTrace();
22. }
23. }
24. }
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 都被纳入一个事务进行管理,呵呵。修改一下方法的参数,修改为
Java 代码
1. ut.insertBothDatabase("1", "name1");
ut.insertBothDatabase("1", "name1");
然后再试试test看数据库结果,如何?
第三节将简单介绍Atomikos在Spring中的配置和使用。