laiwanwanyihao 2011-10-11
关于SpringJTA的介绍非常多了,这里就不再一再阐述其优越性怎么怎么了,直接开始正题。一个大致的需求如下,用户在进行增删改操作时,会同时更新2至3个数据库的数据表,操作需要事务来包裹,以便在操作其中一个数据库的数据表失败时,可以将其他数据库已执行的动作回滚。
由于主框架是Spring,所以将研究的重点放在Spring所提供的的JTA事务上,google了很多资料,发现有几种实现的技术,如JOTM,Atomikos,以及J2EE容器所提供的JTA实现,如JBOSS也都提供了JTA的实现,不过个人喜好来讲,非常厌恶使用JNDI来访问容器组件,所以,重点放在JOTM和Atoimikos两种技术上面,JOTM网上很多资料介绍,配置也非常简单,相信大家通过google就可以快速的上手,本着研究的原则,少少了解了另一种实现技术Atomikos,发现使用方法也很简单,下面就用一个简单的例子来详细说明一下他们在Spring中的配置和使用方法。需要说明的一点是,这个例子也是当时学习两种技术,然后测试的例子,仅仅最后做了一些整理。
初步的设想:一个包括Dao/Service/Test三层的简单应用,事务配置在service层.用来同时插入两个数据库(test,test2)的数据表xa_test,表结构非常简单,包含两个字段id,name,其中test2数据库中表xa_test的name字段不允许为空。
使用的数据库为Mysql5.0,数据库连接驱动版本5.0.8,这里需要注意,过低版本的Mysql或驱动可能不支持XADatasource。
Databasetestddl
Java代码
1.CREATETABLE`xa_test`(
2.`id`int(11)NOTNULL,
3.`name`varchar(20)defaultNULL
4.)ENGINE=InnoDBDEFAULTCHARSET=latin1;
CREATETABLE`xa_test`(
`id`int(11)NOTNULL,
`name`varchar(20)defaultNULL
)ENGINE=InnoDBDEFAULTCHARSET=latin1;
Databasetest2ddl
Java代码
1.CREATETABLE`xa_test`(
2.`id`int(11)NOTNULL,
3.`name`varchar(20)NOTNULL
4.)ENGINE=InnoDBDEFAULTCHARSET=latin1;
CREATETABLE`xa_test`(
`id`int(11)NOTNULL,
`name`varchar(20)NOTNULL
)ENGINE=InnoDBDEFAULTCHARSET=latin1;
UserDao,这里使用了两个,UserDao1和UserDao2,用以操作两个不同数据库的数据表(画蛇添足了,呵呵,为了描述清晰).Dao都继承自Spring的JdbcDaoSupport,使用JdbcTemplate进行数据库读写.
UserDao1.java
Java代码
1.packagecom.xa.dao;
2.
3.importorg.springframework.jdbc.core.support.JdbcDaoSupport;
4.
5.publicclassUserDao1extendsJdbcDaoSupport{
6.publicvoidinsertData(Stringid,Stringname){
7.getJdbcTemplate().execute("insertintoxa_testvalues('"+id+"',"+name+")");
8.}
9.}
packagecom.xa.dao;
importorg.springframework.jdbc.core.support.JdbcDaoSupport;
publicclassUserDao1extendsJdbcDaoSupport{
publicvoidinsertData(Stringid,Stringname){
getJdbcTemplate().execute("insertintoxa_testvalues('"+id+"',"+name+")");
}
}
UserDao2.java
Java代码
1.packagecom.xa.dao;
2.
3.importorg.springframework.jdbc.core.support.JdbcDaoSupport;
4.
5.publicclassUserDao2extendsJdbcDaoSupport
6.{
7.publicvoidinsertData(Stringid,Stringname){
8.getJdbcTemplate().execute("insertintoxa_testvalues('"+id+"',"+name+")");
9.}
10.}
packagecom.xa.dao;
importorg.springframework.jdbc.core.support.JdbcDaoSupport;
publicclassUserDao2extendsJdbcDaoSupport
{
publicvoidinsertData(Stringid,Stringname){
getJdbcTemplate().execute("insertintoxa_testvalues('"+id+"',"+name+")");
}
}
然后创建UserService和UserServiceImpl,在UserServiceImpl中,将调用Dao1和Dao2的insertData的方法同时插入两个数据库的内容,稍后,会将事务配置在service层.
UserService.java
Java代码
1.packagecom.xa.service;
2.
3.publicinterfaceUserService
4.{
5.publicvoidinsertBothDatabase(Stringid,Stringname)throwsException;
6.}
packagecom.xa.service;
publicinterfaceUserService
{
publicvoidinsertBothDatabase(Stringid,Stringname)throwsException;
}
UserServiceImpl.java
Java代码
1.packagecom.xa.service;
2.
3.importcom.xa.dao.UserDao1;
4.importcom.xa.dao.UserDao2;
5.
6.publicclassUserServiceImplimplementsUserService
7.{
8.privateUserDao1dao1;
9.
10.privateUserDao2dao2;
11.
12.publicvoidinsertBothDatabase(Stringid,Stringname)throwsException{
13.dao1.insertData(id,name);
14.dao2.insertData(id,name);
15.}
16.
17.publicUserDao1getDao1(){
18.returndao1;
19.}
20.
21.publicvoidsetDao1(UserDao1dao1){
22.this.dao1=dao1;
23.}
24.
25.publicUserDao2getDao2(){
26.returndao2;
27.}
28.
29.publicvoidsetDao2(UserDao2dao2){
30.this.dao2=dao2;
31.}
32.}
packagecom.xa.service;
importcom.xa.dao.UserDao1;
importcom.xa.dao.UserDao2;
publicclassUserServiceImplimplementsUserService
{
privateUserDao1dao1;
privateUserDao2dao2;
publicvoidinsertBothDatabase(Stringid,Stringname)throwsException{
dao1.insertData(id,name);
dao2.insertData(id,name);
}
publicUserDao1getDao1(){
returndao1;
}
publicvoidsetDao1(UserDao1dao1){
this.dao1=dao1;
}
publicUserDao2getDao2(){
returndao2;
}
publicvoidsetDao2(UserDao2dao2){
this.dao2=dao2;
}
}
UserServiceImpl.java的代码可以看到,在insertBothDatabase方法中,调用了两个Dao的方法.
一个JOTM(JavaOpenTransactionManager)是ObjectWeb的一个开源JTA实现,本身也是开源应用程序服务器JOnAS(JavaOpenApplicationServer)的一部分,为其提供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.#donotuseCAROLJNDIwrapper
2.carol.start.jndi=false
3.
4.#donotstartanameserver
5.carol.start.ns=false
6.
7.#NamingFactory
8.carol.jndi.java.naming.factory.url.pkgs=org.apache.naming
#donotuseCAROLJNDIwrapper
carol.start.jndi=false
#donotstartanameserver
carol.start.ns=false
#NamingFactory
carol.jndi.java.naming.factory.url.pkgs=org.apache.naming
上面配置文件的目的是不使用JNDI的方式来加载JOTM的配置,当然也可以根据需要选择其它的一些配置,例如JTOM所提供的默认配置。
然后开始在Spring上下文中配置JOTM,在classpath中建立一个ApplicationContext-jotm.xml,配置如下
ApplicationContext-jotm.xml
Java代码
1.<?xmlversion="1.0"encoding="UTF-8"?>
2.<beansxmlns="http://www.springframework.org/schema/beans"
3.xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4.xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
5.
6.<beanid="jotm"class="org.springframework.transaction.jta.JotmFactoryBean"/>
7.
8.<beanid="txManager"class="org.springframework.transaction.jta.JtaTransactionManager">
9.<propertyname="userTransaction"ref="jotm"/>
10.</bean>
11.
12.<beanid="ds1"class="org.enhydra.jdbc.pool.StandardXAPoolDataSource"destroy-method="shutdown">
13.<propertyname="dataSource">
14.<beanclass="org.enhydra.jdbc.standard.StandardXADataSource"destroy-method="shutdown">
15.<propertyname="transactionManager"ref="jotm"/>
16.<propertyname="driverName"value="com.mysql.jdbc.Driver"/>
17.<propertyname="url"value="jdbc:MySQL://localhost:3306/test"/>
18.</bean>
19.</property>
20.<propertyname="user"value="root"/>
21.<propertyname="password"value="admin"/>
22.</bean>
23.
24.<beanid="ds2"class="org.enhydra.jdbc.pool.StandardXAPoolDataSource"destroy-method="shutdown">
25.<propertyname="dataSource">
26.<beanclass="org.enhydra.jdbc.standard.StandardXADataSource"destroy-method="shutdown">
27.<propertyname="transactionManager"ref="jotm"/>
28.<propertyname="driverName"value="com.mysql.jdbc.Driver"/>
29.<propertyname="url"value="jdbc:MySQL://localhost:3306/test2"/>
30.</bean>
31.</property>
32.<propertyname="user"value="root"/>
33.<propertyname="password"value="admin"/>
34.</bean>
35.
36.<beanid="template1"class="org.springframework.jdbc.core.JdbcTemplate">
37.<propertyname="dataSource"ref="ds1"/>
38.</bean>
39.
40.<beanid="template2"class="org.springframework.jdbc.core.JdbcTemplate">
41.<propertyname="dataSource"ref="ds2"/>
42.</bean>
43.
44.<beanid="dao1"class="com.xa.dao.UserDao1">
45.<propertyname="jdbcTemplate">
46.<refbean="template1"></ref>
47.</property>
48.</bean>
49.
50.<beanid="dao2"class="com.xa.dao.UserDao2">
51.<propertyname="jdbcTemplate">
52.<refbean="template2"></ref>
53.</property>
54.</bean>
55.
56.<beanid="userServiceTarget"class="com.xa.service.UserServiceImpl">
57.<propertyname="dao1"ref="dao1"/>
58.<propertyname="dao2"ref="dao2"/>
59.</bean>
60.
61.
62.<beanid="userTest"class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
63.<propertyname="transactionManager">
64.<refbean="txManager"/>
65.</property>
66.<propertyname="target">
67.<refbean="userServiceTarget"/>
68.</property>
69.<propertyname="transactionAttributes">
70.<props>
71.<propkey="insert*">PROPAGATION_REQUIRED,-Exception</prop>
72.</props>
73.</property>
74.</bean>
75.</beans>
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<beanid="jotm"class="org.springframework.transaction.jta.JotmFactoryBean"/>
<beanid="txManager"class="org.springframework.transaction.jta.JtaTransactionManager">
<propertyname="userTransaction"ref="jotm"/>
</bean>
<beanid="ds1"class="org.enhydra.jdbc.pool.StandardXAPoolDataSource"destroy-method="shutdown">
<propertyname="dataSource">
<beanclass="org.enhydra.jdbc.standard.StandardXADataSource"destroy-method="shutdown">
<propertyname="transactionManager"ref="jotm"/>
<propertyname="driverName"value="com.mysql.jdbc.Driver"/>
<propertyname="url"value="jdbc:MySQL://localhost:3306/test"/>
</bean>
</property>
<propertyname="user"value="root"/>
<propertyname="password"value="admin"/>
</bean>
<beanid="ds2"class="org.enhydra.jdbc.pool.StandardXAPoolDataSource"destroy-method="shutdown">
<propertyname="dataSource">
<beanclass="org.enhydra.jdbc.standard.StandardXADataSource"destroy-method="shutdown">
<propertyname="transactionManager"ref="jotm"/>
<propertyname="driverName"value="com.mysql.jdbc.Driver"/>
<propertyname="url"value="jdbc:MySQL://localhost:3306/test2"/>
</bean>
</property>
<propertyname="user"value="root"/>
<propertyname="password"value="admin"/>
</bean>
<beanid="template1"class="org.springframework.jdbc.core.JdbcTemplate">
<propertyname="dataSource"ref="ds1"/>
</bean>
<beanid="template2"class="org.springframework.jdbc.core.JdbcTemplate">
<propertyname="dataSource"ref="ds2"/>
</bean>
<beanid="dao1"class="com.xa.dao.UserDao1">
<propertyname="jdbcTemplate">
<refbean="template1"></ref>
</property>
</bean>
<beanid="dao2"class="com.xa.dao.UserDao2">
<propertyname="jdbcTemplate">
<refbean="template2"></ref>
</property>
</bean>
<beanid="userServiceTarget"class="com.xa.service.UserServiceImpl">
<propertyname="dao1"ref="dao1"/>
<propertyname="dao2"ref="dao2"/>
</bean>
<beanid="userTest"class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<propertyname="transactionManager">
<refbean="txManager"/>
</property>
<propertyname="target">
<refbean="userServiceTarget"/>
</property>
<propertyname="transactionAttributes">
<props>
<propkey="insert*">PROPAGATION_REQUIRED,-Exception</prop>
</props>
</property>
</bean>
</beans>
上面是一个完整的Spring上下文配置,可以看第一个bean“jotm”,实际上引用了Spring内部所提供的对JOTM支持的工厂类,参考下面的配置代码段
Java代码
1.<beanid="jotm"class="org.springframework.transaction.jta.JotmFactoryBean"/>
<beanid="jotm"class="org.springframework.transaction.jta.JotmFactoryBean"/>
随后,配置了JTA事务管理器,并且在管理器中使用上面所配置的jotm,如下面的代码
Java代码
1.<beanid="txManager"class="org.springframework.transaction.jta.JtaTransactionManager">
2.<propertyname="userTransaction"ref="jotm"/>
3.</bean>
<beanid="txManager"class="org.springframework.transaction.jta.JtaTransactionManager">
<propertyname="userTransaction"ref="jotm"/>
</bean>
再接下来就是配置多个数据源了,使用jotm提供的org.enhydra.jdbc.pool.StandardXAPoolDataSource类,根据类名可以明确地看出它是用以配置多个数据源的啦,配置的代码如下
Java代码
1.<beanid="ds1"class="org.enhydra.jdbc.pool.StandardXAPoolDataSource"destroy-method="shutdown">
2.<propertyname="dataSource">
3.<beanclass="org.enhydra.jdbc.standard.StandardXADataSource"destroy-method="shutdown">
4.<propertyname="transactionManager"ref="jotm"/>
5.<propertyname="driverName"value="com.mysql.jdbc.Driver"/>
6.<propertyname="url"value="jdbc:MySQL://localhost:3306/test"/>
7.</bean>
8.</property>
9.<propertyname="user"value="root"/>
10.<propertyname="password"value="admin"/>
11.</bean>
12.
13.<beanid="ds2"class="org.enhydra.jdbc.pool.StandardXAPoolDataSource"destroy-method="shutdown">
14.<propertyname="dataSource">
15.<beanclass="org.enhydra.jdbc.standard.StandardXADataSource"destroy-method="shutdown">
16.<propertyname="transactionManager"ref="jotm"/>
17.<propertyname="driverName"value="com.mysql.jdbc.Driver"/>
18.<propertyname="url"value="jdbc:MySQL://localhost:3306/test2"/>
19.</bean>
20.</property>
21.<propertyname="user"value="root"/>
22.<propertyname="password"value="admin"/>
23.</bean>
<beanid="ds1"class="org.enhydra.jdbc.pool.StandardXAPoolDataSource"destroy-method="shutdown">
<propertyname="dataSource">
<beanclass="org.enhydra.jdbc.standard.StandardXADataSource"destroy-method="shutdown">
<propertyname="transactionManager"ref="jotm"/>
<propertyname="driverName"value="com.mysql.jdbc.Driver"/>
<propertyname="url"value="jdbc:MySQL://localhost:3306/test"/>
</bean>
</property>
<propertyname="user"value="root"/>
<propertyname="password"value="admin"/>
</bean>
<beanid="ds2"class="org.enhydra.jdbc.pool.StandardXAPoolDataSource"destroy-method="shutdown">
<propertyname="dataSource">
<beanclass="org.enhydra.jdbc.standard.StandardXADataSource"destroy-method="shutdown">
<propertyname="transactionManager"ref="jotm"/>
<propertyname="driverName"value="com.mysql.jdbc.Driver"/>
<propertyname="url"value="jdbc:MySQL://localhost:3306/test2"/>
</bean>
</property>
<propertyname="user"value="root"/>
<propertyname="password"value="admin"/>
</bean>
这里配置的两个数据源都连接到本地的mysql,实际上可以连接到不同的dbserver和不同类型的数据库,已经经过测试,这里为了方便,在本地建立了两个不同的数据库(test,test2)做测试。
随后的配置基本上和普通的Spring上下文配置相同了,根据不同的数据源配置两个jdbcTemplate,两个dao分别引用不同的jdbcTemplate,将两个dao注入到UserService中,最后将service纳入事务管理,并在事务代理配置中配置回滚规则,意思为如遇异常,则强制回滚内容。配置如下所示
Java代码
1.<propertyname="transactionAttributes">
2.<props>
3.<propkey="insert*">PROPAGATION_REQUIRED,-Exception</prop>
4.</props>
5.</property>
<propertyname="transactionAttributes">
<props>
<propkey="insert*">PROPAGATION_REQUIRED,-Exception</prop>
</props>
</property>
这样,一个使用JOTMJTA事务的简单应用算大致成型了,最后,写一个JUnit,来测试一下结果
TestXa.java
Java代码
1.packagecom.xa;
2.
3.importorg.springframework.context.ApplicationContext;
4.importorg.springframework.test.AbstractDependencyInjectionSpringContextTests;
5.
6.importcom.xa.service.UserService;
7.
8.publicclassTestXaextendsAbstractDependencyInjectionSpringContextTests
9.{
10.protectedString[]getConfigLocations(){
11.returnnewString[]{"classpath:ApplicationContext-jotm.xml"};
12.}
13.
14.publicvoidtestInsertBothDatabase(){
15.ApplicationContextctx=this.getApplicationContext();
16.UserServiceut=(UserService)ctx.getBean("userTest");
17.try{
18.ut.insertBothDatabase("1",null);
19.}
20.catch(Exceptione){
21.e.printStackTrace();
22.}
23.}
24.}
packagecom.xa;
importorg.springframework.context.ApplicationContext;
importorg.springframework.test.AbstractDependencyInjectionSpringContextTests;
importcom.xa.service.UserService;
publicclassTestXaextendsAbstractDependencyInjectionSpringContextTests
{
protectedString[]getConfigLocations(){
returnnewString[]{"classpath:ApplicationContext-jotm.xml"};
}
publicvoidtestInsertBothDatabase(){
ApplicationContextctx=this.getApplicationContext();
UserServiceut=(UserService)ctx.getBean("userTest");
try{
ut.insertBothDatabase("1",null);
}
catch(Exceptione){
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中的配置和使用。
简单的应用已经建立,随后下一节将说明如何配置JOTM以及在Spring中的相关配置.
前面简单介绍了JOTM如何在Spring中配置,并如何使用它的JTA事务,本节将介绍Atomikos如何与Spring集成,并使用它的JTA事务。
Atomikos,是一个基于Java的开源事务管理器,提供了事务管理和连接池,不需要应用服务器支持,支持JDBC和JMS事务,能提供对Spring,Hibernate的集成,有兴趣多了解的可以直接参考说明文当,有详细的介绍和使用说明。
Atomikos可以到http://www.atomikos.com/products.html#ate下载,最新版本是3.2.0
首先,仍然是一个相关的配置文件,用来设置相关的环境,打开解压缩后Atomikos根目录,拷贝jta.properties到classpath中,并修改为如下内容
jta.properties
Java代码
1.com.atomikos.icatch.service=com.atomikos.icatch.standalone.UserTransactionServiceFactory
2.com.atomikos.icatch.console_file_name=tm.out
3.com.atomikos.icatch.log_base_name=tmlog
4.com.atomikos.icatch.tm_unique_name=com.atomikos.spring.jdbc.tm
5.com.atomikos.icatch.console_log_level=INFO
com.atomikos.icatch.service=com.atomikos.icatch.standalone.UserTransactionServiceFactory
com.atomikos.icatch.console_file_name=tm.out
com.atomikos.icatch.log_base_name=tmlog
com.atomikos.icatch.tm_unique_name=com.atomikos.spring.jdbc.tm
com.atomikos.icatch.console_log_level=INFO
随后,是Spring上下文配置,建立一个名为ApplicationContext-atomikos.xml的Spring上下文
ApplicationContext-atomikos.xml
Java代码
1.<?xmlversion="1.0"encoding="UTF-8"?>
2.<beansxmlns="http://www.springframework.org/schema/beans"
3.xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4.xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
5.<!--XADatasource-->
6.<beanid="datasource1"class="com.atomikos.jdbc.SimpleDataSourceBean"init-method="init"destroy-method="close">
7.<propertyname="uniqueResourceName">
8.<value>mysql/main</value>
9.</property>
10.<propertyname="xaDataSourceClassName">
11.<value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value>
12.</property>
13.<propertyname="xaDataSourceProperties">
14.<value>URL=jdbc:mysql://localhost:3306/test;user=root;password=admin</value>
15.</property>
16.<propertyname="exclusiveConnectionMode">
17.<value>true</value>
18.</property>
19.<propertyname="connectionPoolSize">
20.<value>3</value>
21.</property>
22.<propertyname="validatingQuery">
23.<value>SELECT1</value>
24.</property>
25.</bean>
26.
27.<beanid="datasource2"class="com.atomikos.jdbc.SimpleDataSourceBean"init-method="init"destroy-method="close">
28.<propertyname="uniqueResourceName">
29.<value>mysql/news</value>
30.</property>
31.<propertyname="xaDataSourceClassName">
32.<value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value>
33.</property>
34.<propertyname="xaDataSourceProperties">
35.<value>URL=jdbc:mysql://localhost:3306/test2;user=root;password=admin</value>
36.</property>
37.<propertyname="exclusiveConnectionMode">
38.<value>true</value>
39.</property>
40.<propertyname="connectionPoolSize">
41.<value>3</value>
42.</property>
43.<propertyname="validatingQuery">
44.<value>SELECT1</value>
45.</property>
46.</bean>
47.
48.<beanid="template1"class="org.springframework.jdbc.core.JdbcTemplate">
49.<propertyname="dataSource"ref="datasource1"/>
50.</bean>
51.
52.<beanid="template2"class="org.springframework.jdbc.core.JdbcTemplate">
53.<propertyname="dataSource"ref="datasource2"/>
54.</bean>
55.
56.<beanid="atomikosTransactionManager"class="com.atomikos.icatch.jta.UserTransactionManager"init-method="init"destroy-method="close">
57.<propertyname="forceShutdown"><value>true</value></property>
58.</bean>
59.
60.<beanid="atomikosUserTransaction"class="com.atomikos.icatch.jta.UserTransactionImp">
61.<propertyname="transactionTimeout"value="300"/>
62.</bean>
63.
64.<beanid="springTransactionManager"class="org.springframework.transaction.jta.JtaTransactionManager">
65.<propertyname="transactionManager"><refbean="atomikosTransactionManager"/></property>
66.<propertyname="userTransaction"><refbean="atomikosUserTransaction"/></property>
67.</bean>
68.
69.<beanid="dao1"class="com.xa.dao.UserDao1">
70.<propertyname="jdbcTemplate">
71.<refbean="template1"></ref>
72.</property>
73.</bean>
74.
75.<beanid="dao2"class="com.xa.dao.UserDao2">
76.<propertyname="jdbcTemplate">
77.<refbean="template2"></ref>
78.</property>
79.</bean>
80.<!---->
81.<beanid="userServiceTarget"class="com.xa.service.UserServiceImpl">
82.<propertyname="dao1"ref="dao1"/>
83.<propertyname="dao2"ref="dao2"/>
84.</bean>
85.
86.<beanid="userTest"class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
87.<propertyname="transactionManager">
88.<refbean="springTransactionManager"/>
89.</property>
90.<propertyname="target">
91.<refbean="userServiceTarget"/>
92.</property>
93.<propertyname="transactionAttributes">
94.<props>
95.<propkey="insert*">PROPAGATION_REQUIRED,-Exception</prop>
96.</props>
97.</property>
98.</bean>
99.</beans>
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!--XADatasource-->
<beanid="datasource1"class="com.atomikos.jdbc.SimpleDataSourceBean"init-method="init"destroy-method="close">
<propertyname="uniqueResourceName">
<value>mysql/main</value>
</property>
<propertyname="xaDataSourceClassName">
<value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value>
</property>
<propertyname="xaDataSourceProperties">
<value>URL=jdbc:mysql://localhost:3306/test;user=root;password=admin</value>
</property>
<propertyname="exclusiveConnectionMode">
<value>true</value>
</property>
<propertyname="connectionPoolSize">
<value>3</value>
</property>
<propertyname="validatingQuery">
<value>SELECT1</value>
</property>
</bean>
<beanid="datasource2"class="com.atomikos.jdbc.SimpleDataSourceBean"init-method="init"destroy-method="close">
<propertyname="uniqueResourceName">
<value>mysql/news</value>
</property>
<propertyname="xaDataSourceClassName">
<value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value>
</property>
<propertyname="xaDataSourceProperties">
<value>URL=jdbc:mysql://localhost:3306/test2;user=root;password=admin</value>
</property>
<propertyname="exclusiveConnectionMode">
<value>true</value>
</property>
<propertyname="connectionPoolSize">
<value>3</value>
</property>
<propertyname="validatingQuery">
<value>SELECT1</value>
</property>
</bean>
<beanid="template1"class="org.springframework.jdbc.core.JdbcTemplate">
<propertyname="dataSource"ref="datasource1"/>
</bean>
<beanid="template2"class="org.springframework.jdbc.core.JdbcTemplate">
<propertyname="dataSource"ref="datasource2"/>
</bean>
<beanid="atomikosTransactionManager"class="com.atomikos.icatch.jta.UserTransactionManager"init-method="init"destroy-method="close">
<propertyname="forceShutdown"><value>true</value></property>
</bean>
<beanid="atomikosUserTransaction"class="com.atomikos.icatch.jta.UserTransactionImp">
<propertyname="transactionTimeout"value="300"/>
</bean>
<beanid="springTransactionManager"class="org.springframework.transaction.jta.JtaTransactionManager">
<propertyname="transactionManager"><refbean="atomikosTransactionManager"/></property>
<propertyname="userTransaction"><refbean="atomikosUserTransaction"/></property>
</bean>
<beanid="dao1"class="com.xa.dao.UserDao1">
<propertyname="jdbcTemplate">
<refbean="template1"></ref>
</property>
</bean>
<beanid="dao2"class="com.xa.dao.UserDao2">
<propertyname="jdbcTemplate">
<refbean="template2"></ref>
</property>
</bean>
<!---->
<beanid="userServiceTarget"class="com.xa.service.UserServiceImpl">
<propertyname="dao1"ref="dao1"/>
<propertyname="dao2"ref="dao2"/>
</bean>
<beanid="userTest"class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<propertyname="transactionManager">
<refbean="springTransactionManager"/>
</property>
<propertyname="target">
<refbean="userServiceTarget"/>
</property>
<propertyname="transactionAttributes">
<props>
<propkey="insert*">PROPAGATION_REQUIRED,-Exception</prop>
</props>
</property>
</bean>
</beans>
看上下文中的代码,很大部分和JOTM的相似,atomikos使用com.atomikos.jdbc.SimpleDataSourceBean类来创建datasourcebean,这里需要特别注意
Java代码
1.<propertyname="uniqueResourceName">
2.<value>mysql/main</value>
3.</property>
<propertyname="uniqueResourceName">
<value>mysql/main</value>
</property>
Java代码
1.<propertyname="uniqueResourceName">
2.<value>mysql/news</value>
3.</property>
<propertyname="uniqueResourceName">
<value>mysql/news</value>
</property>
上面的代码为每个Datasource配置了一个唯一的名称,名称不允许相同,否则将会使用同一个datasource,而不会出现异常。
另外在配置文件中定义了Atomikos的事务管理器,并在Spring的JtaTransactionManager中引用
Java代码
1.<beanid="atomikosTransactionManager"class="com.atomikos.icatch.jta.UserTransactionManager"init-method="init"destroy-method="close">
2.<propertyname="forceShutdown"><value>true</value></property>
3.</bean>
4.
5.<beanid="atomikosUserTransaction"class="com.atomikos.icatch.jta.UserTransactionImp">
6.<propertyname="transactionTimeout"value="300"/>
7.</bean>
8.
9.<beanid="springTransactionManager"class="org.springframework.transaction.jta.JtaTransactionManager">
10.<propertyname="transactionManager"><refbean="atomikosTransactionManager"/></property>
11.<propertyname="userTransaction"><refbean="atomikosUserTransaction"/></property>
12.</bean>
<beanid="atomikosTransactionManager"class="com.atomikos.icatch.jta.UserTransactionManager"init-method="init"destroy-method="close">
<propertyname="forceShutdown"><value>true</value></property>
</bean>
<beanid="atomikosUserTransaction"class="com.atomikos.icatch.jta.UserTransactionImp">
<propertyname="transactionTimeout"value="300"/>
</bean>
<beanid="springTransactionManager"class="org.springframework.transaction.jta.JtaTransactionManager">
<propertyname="transactionManager"><refbean="atomikosTransactionManager"/></property>
<propertyname="userTransaction"><refbean="atomikosUserTransaction"/></property>
</bean>
其它的代码没什么特别,和JOTM配置基本相同,JdbcTemplate,DAO,Service,事务代理等等....
仍然使用TestXa.java,修改成引用ApplicationContext-atomikos.xml的上下文配置
Java代码
1.protectedString[]getConfigLocations(){
2.//returnnewString[]{"classpath:ApplicationContext-jotm.xml"};
3.returnnewString[]{"classpath:ApplicationContext-atomikos.xml"};
4.}
protectedString[]getConfigLocations(){
//returnnewString[]{"classpath:ApplicationContext-jotm.xml"};
returnnewString[]{"classpath:ApplicationContext-atomikos.xml"};
}
运行test,然后查看数据库状态,
然后修改方法参数使其不违反数据库约束,再运行Test观察数据库:)。