前面简单介绍了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
- 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
- <?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">
- <!-- XA Datasource -->
- <bean id="datasource1" class="com.atomikos.jdbc.SimpleDataSourceBean" init-method="init" destroy-method="close">
- <property name="uniqueResourceName">
- <value>mysql/main</value>
- </property>
- <property name="xaDataSourceClassName">
- <value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value>
- </property>
- <property name="xaDataSourceProperties">
- <value>URL=jdbc:mysql://localhost:3306/test;user=root;password=admin</value>
- </property>
- <property name="exclusiveConnectionMode">
- <value>true</value>
- </property>
- <property name="connectionPoolSize">
- <value>3</value>
- </property>
- <property name="validatingQuery">
- <value>SELECT 1</value>
- </property>
- </bean>
- <bean id="datasource2" class="com.atomikos.jdbc.SimpleDataSourceBean" init-method="init" destroy-method="close">
- <property name="uniqueResourceName">
- <value>mysql/news</value>
- </property>
- <property name="xaDataSourceClassName">
- <value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value>
- </property>
- <property name="xaDataSourceProperties">
- <value>URL=jdbc:mysql://localhost:3306/test2;user=root;password=admin</value>
- </property>
- <property name="exclusiveConnectionMode">
- <value>true</value>
- </property>
- <property name="connectionPoolSize">
- <value>3</value>
- </property>
- <property name="validatingQuery">
- <value>SELECT 1</value>
- </property>
- </bean>
- <bean id="template1" class="org.springframework.jdbc.core.JdbcTemplate">
- <property name="dataSource" ref="datasource1" />
- </bean>
- <bean id="template2" class="org.springframework.jdbc.core.JdbcTemplate">
- <property name="dataSource" ref="datasource2" />
- </bean>
- <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close">
- <property name="forceShutdown"><value>true</value></property>
- </bean>
- <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
- <property name="transactionTimeout" value="300"/>
- </bean>
- <bean id="springTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
- <property name="transactionManager"><ref bean="atomikosTransactionManager" /></property>
- <property name="userTransaction"><ref bean="atomikosUserTransaction" /></property>
- </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="springTransactionManager"/>
- </property>
- <property name="target">
- <ref bean="userServiceTarget"/>
- </property>
- <property name="transactionAttributes">
- <props>
- <prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>
- </props>
- </property>
- </bean>
- </beans>
看上下文中的代码,很大部分和JOTM的相似,atomikos使用com.atomikos.jdbc.SimpleDataSourceBean类来创建datasource bean,这里需要特别注意
- <property name="uniqueResourceName">
- <value>mysql/main</value>
- </property>
- <property name="uniqueResourceName">
- <value>mysql/news</value>
- </property>
上面的代码为每个Datasource配置了一个唯一的名称,名称不允许相同,否则将会使用同一个datasource,而不会出现异常。
另外在配置文件中定义了Atomikos的事务管理器,并在Spring的JtaTransactionManager中引用
- <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close">
- <property name="forceShutdown"><value>true</value></property>
- </bean>
- <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
- <property name="transactionTimeout" value="300"/>
- </bean>
- <bean id="springTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
- <property name="transactionManager"><ref bean="atomikosTransactionManager" /></property>
- <property name="userTransaction"><ref bean="atomikosUserTransaction" /></property>
- </bean>
其它的代码没什么特别,和JOTM配置基本相同,JdbcTemplate,DAO,Service,事务代理等等....
仍然使用TestXa.java,修改成引用ApplicationContext-atomikos.xml的上下文配置
- protected String[] getConfigLocations() {
- //return new String[] { "classpath:ApplicationContext-jotm.xml" };
- return new String[] { "classpath:ApplicationContext-atomikos.xml" };
- }
运行test,然后查看数据库状态,
然后修改方法参数使其不违反数据库约束,再运行Test观察数据库 :)。