使用Atomikos Transactions Essentials实现多数据源JTA分布式事务
来源:互联网 发布:配音秀同类软件 编辑:程序博客网 时间:2024/05/29 11:08
9.17 update:使用NonXADataSourceBean. Mysql在5.0版本和Connecter/J5.0版本后提供了XADatasource支持,如果使用了支持XADatasouce版本,可以参考2楼补充.
最近做的project中遇到要将数据库中的表分布到两台不同的服务器上的Mysql5.0中,project主要使用spring+ibatis。因此需要JTA的支持,但是tomcat不支持,所以就搜索开源的JTA实现。
最开始使用的是JOTM,但是使用中不能自动rollback,无论什么情况都commit。然后看到infoq上一篇文章提到Atomikos Transactions Essentials,Atomikos Transactions Essentials 3.0是Atomikos 开发的核心事务引擎,支持JDBC 以及JMS 的JTA/XA 事务。易于部署,轻量级,同时支持JDBC 以及JMS 。
Atomikos Transactions Essentials现在的版本是3.1.7,可以在http://www.atomikos.com/Main/TransactionsEssentialsDownloadForm 下载,在发布包里的examples文件夹下面有些例子,非常实用,我在使用中参考里面的例子很容易配置成功。先将发布包里面dist目录下的atomikos-util.jar,transactions.jar,transactions-api.jar,transactions-jta.jar copy到项目lib里面, 如果使用hibernate则需要将另外两个hibernate相关的jar页copy到项目里面,spring配置文件如下:
- <!-- 第一个数据库 -->
- < bean id = "dataSource" class = "com.atomikos.jdbc.SimpleDataSourceBean" init-method = "init" destroy-method = "close" >
- < property name = "uniqueResourceName" >
- < value > mysql/main </ value >
- </ property >
- < property name = "xaDataSourceClassName" >
- <!-- 使用Mysql XADataSource(mysql>=5.0, Connector/J>=5.0才可以支持XADatasource)-->
- < value > com.mysql.jdbc.jdbc2.optional.MysqlXADataSource </ value >
- </ property >
- < property name = "xaDataSourceProperties" >
- < value > URL =${jdbc.url}; user =${jdbc.username}; password =${jdbc.password} </ 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 = "dataSourceB" class = "com.atomikos.jdbc.SimpleDataSourceBean" init-method = "init" destroy-method = "close" >
- < property name = "uniqueResourceName" >
- < value > mysql/news </ value >
- </ property >
- < property name = "xaDataSourceClassName" >
- <!-- 使用Mysql XADataSource(mysql>=5.0, Connector/J>=5.0才可以支持XADatasource)-->
- < value > com.mysql.jdbc.jdbc2.optional.MysqlXADataSource </ value >
- </ property >
- < property name = "xaDataSourceProperties" >
- < value > URL =${jdbc.url.b}; user =${jdbc.username.b}; password =${jdbc.password.b} </ 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 = "lobHandler" class = "org.springframework.jdbc.support.lob.DefaultLobHandler" />
- <!-- 第一个数据库的sqlMapClient -->
- < bean id = "sqlMapClient" class = "org.springframework.orm.ibatis.SqlMapClientFactoryBean" >
- < property name = "configLocation" >
- <!-- 包含第一个数据库表的map -->
- < value > classpath:/sqlmap-config.xml </ value >
- </ property >
- < property name = "dataSource" ref = "dataSource" />
- < property name = "lobHandler" ref = "lobHandler" />
- </ bean >
- <!-- 第二个数据库的sqlMapClient -->
- < bean id = "sqlMapClientB" class = "org.springframework.orm.ibatis.SqlMapClientFactoryBean" >
- < property name = "configLocation" >
- <!-- 包含第一个数据库表的map -->
- < value > classpath:/sqlmap-configb.xml </ value >
- </ property >
- < property name = "dataSource" ref = "dataSourceB" />
- < property name = "lobHandler" ref = "lobHandler" />
- </ bean >
- <!-- Construct Atomikos UserTransactionManager, needed to configure Spring -->
- < bean id = "atomikosTransactionManager" class = "com.atomikos.icatch.jta.UserTransactionManager" init-method = "init"
- destroy-method = "close" >
- <!-- when close is called, should we force transactions to terminate or not? -->
- < property name = "forceShutdown" >
- < value > true </ value >
- </ property >
- </ bean >
- <!-- Also use Atomikos UserTransactionImp, needed to configure Spring -->
- < bean id = "atomikosUserTransaction" class = "com.atomikos.icatch.jta.UserTransactionImp" >
- < property name = "transactionTimeout" value = "240" />
- </ bean >
- <!-- Configure the Spring framework to use JTA transactions from Atomikos -->
- < bean id = "transactionManager" class = "org.springframework.transaction.jta.JtaTransactionManager" >
- < property name = "transactionManager" >
- < ref bean = "atomikosTransactionManager" />
- </ property >
- < property name = "userTransaction" >
- < ref bean = "atomikosUserTransaction" />
- </ property >
- </ bean >
事务的配置, 使用了spring2.0的语法,所以将namesapce也帖出来了.
- <? 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:tx = "http://www.springframework.org/schema/tx"
- xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
- default-autowire = "byName" default-lazy-init = "true" >
- <!-- 支持 @AspectJ 标记-->
- < aop:aspectj-autoproxy />
- < aop:config proxy-target-class = "true" >
- < aop:advisor pointcut = "execution(* *Facade.*(..))" advice-ref = "txAdvice" />
- < aop:advisor pointcut = "execution(* *Manager.*(..))" advice-ref = "txAdvice" />
- </ aop:config >
- < tx:advice id = "txAdvice" >
- < tx:attributes >
- < tx:method name = "get*" read-only = "true" />
- < tx:method name = "find*" read-only = "true" />
- < tx:method name = "has*" read-only = "true" />
- < tx:method name = "locate*" read-only = "true" />
- < tx:method name = "*" />
- </ tx:attributes >
- </ tx:advice >
- </ beans >
这样配置以后就可以使用分布式事务,测试中出现异常时事务也自动提交。和JOTM相比Atomikos Transactions Essentials更加稳定,它原来是商业项目,现在开源了。象mysql一样卖服务支持的。而且论坛页比较活跃,有问题很快可以解决。
<!-- 第一个数据库 --><bean id="dataSource" class="com.atomikos.jdbc.SimpleDataSourceBean" init-method="init" destroy-method="close"><property name="uniqueResourceName"><value>mysql/main</value></property><property name="xaDataSourceClassName"><!-- 使用Mysql XADataSource(mysql>=5.0, Connector/J>=5.0才可以支持XADatasource)--><value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value></property><property name="xaDataSourceProperties"><value>URL=${jdbc.url};user=${jdbc.username};password=${jdbc.password}</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="dataSourceB" class="com.atomikos.jdbc.SimpleDataSourceBean" init-method="init" destroy-method="close"><property name="uniqueResourceName"><value>mysql/news</value></property><property name="xaDataSourceClassName"><!-- 使用Mysql XADataSource(mysql>=5.0, Connector/J>=5.0才可以支持XADatasource)--><value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value></property><property name="xaDataSourceProperties"><value>URL=${jdbc.url.b};user=${jdbc.username.b};password=${jdbc.password.b}</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>
已经修改好
错,错误信息如下:
Exception in thread "Thread-14" java.lang.NullPointerException
at com.atomikos.icatch.system.Configuration.removeResource(Unknown Source)
at com.atomikos.jdbc.JtaDataSourceImp.close(Unknown Source)
at com.atomikos.jdbc.DataSourceShutdownHook.run(Unknown Source)
Exception in thread "Thread-6" java.lang.NullPointerException
at com.atomikos.icatch.system.Configuration.removeResource(Unknown Source)
at com.atomikos.jdbc.JtaDataSourceImp.close(Unknown Source)
at com.atomikos.jdbc.DataSourceShutdownHook.run(Unknown Source)
Unknown Source倒底是什么指资源?
C3P0不支持XADatasource.
而且使用数据库的时候也要使用数据库的XA驱动
[17:03:01.375] at com.atomikos.icatch.standalone.UserTransactionServiceImp.init(Unknown Source)
[17:03:01.375] at com.atomikos.icatch.config.UserTransactionServiceImp.init(Unknown Source)
[17:03:01.375] at com.atomikos.icatch.jta.UserTransactionManager.checkSetup(Unknown Source)
[17:03:01.375] at com.atomikos.icatch.jta.UserTransactionManager.init(Unknown Source)
[17:03:01.375] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[17:03:01.375] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[17:03:01.375] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
[17:03:01.375] at java.lang.reflect.Method.invoke(Method.java:597)
[17:03:01.375] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1240)
[17:03:01.375] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1205)
[17:03:01.375] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1171)
[17:03:01.375] ... 90 more
这个错误难道各位都没遇到?奇了怪!这个问题在3.1之前是个bug,作者后来说要修复,但是我在3.2,3.3中都碰到了
[17:03:01.375] at com.atomikos.icatch.standalone.UserTransactionServiceImp.init(Unknown Source)
[17:03:01.375] at com.atomikos.icatch.config.UserTransactionServiceImp.init(Unknown Source)
[17:03:01.375] at com.atomikos.icatch.jta.UserTransactionManager.checkSetup(Unknown Source)
[17:03:01.375] at com.atomikos.icatch.jta.UserTransactionManager.init(Unknown Source)
[17:03:01.375] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[17:03:01.375] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[17:03:01.375] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
[17:03:01.375] at java.lang.reflect.Method.invoke(Method.java:597)
[17:03:01.375] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1240)
[17:03:01.375] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1205)
[17:03:01.375] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1171)
[17:03:01.375] ... 90 more
这个错误难道各位都没遇到?奇了怪!这个问题在3.1之前是个bug,作者后来说要修复,但是我在3.2,3.3中都碰到了
Atomikos分两个:一个是开源的TransactionEssentials,一个是商业的ExtremeTransactions。
TransactionEssentials的主要特征:JTA/XA 事务管理 —— 提供事务管理和连接池不需要应用服务器 —— TransactionEssentials可以在任何Java EE应用服务器中运行,也就是不依赖于任何应用服务器开源 —— TransactionEssentials是遵守Apache版本2许可的开源软件专注于JDBC/JMS —— 支持所有XA资源,但是资源池和消息监听是专供JDBC和JMS的与Spring 和 Hibernate 集成 —— 提供了描述如何与Spring和Hibernate集成的文档
一、环境
spring 2
ibatis2
AtomikosTransactionsEssentials-3.7.0 下载地址:http://www.atomikos.com/Main/InstallingTransactionsEssentials
MySQL-5.1 :数据库引擎为InnoDB,只有这样才能支持事务
JDK1.6
Oracle10
二 jar包
Atomikos jar必须包 transactions-jdbc.jar
transactions-jta.jar
transactions.jar
atomikos-util.jar
transactions-api.jar
ibatis,spring
只要符合二者集成jar组合即可,无额外jar
三、配置
Oracle用户授权:(视情况而定,如果程序出现如下异常则需要加上1672 [main] WARN atomikos - ERROR IN RECOVERY com.atomikos.datasource.ResourceException: Error in recovery)
grant select on sys.dba_pending_transactions to orcl;
grant select on sys.pending_trans$ to orcl;
grant select on sys.dba_2pc_pending to orcl;
grant execute on sys.dbms_system to orcl;
jta.properties 配置文件,即src/ 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配置文件
<?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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd "><!-- 事物管理器 --><bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp"> <description>UserTransactionImp</description> <property name="transactionTimeout" value="300"/> </bean> <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close"> <description>UserTransactionManager</description> <property name="forceShutdown"> <value>true</value> </property> </bean> <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"> <description>JtaTransactionManager</description> <property name="transactionManager"> <ref bean="atomikosTransactionManager" /> </property> <property name="userTransaction"> <ref bean="atomikosUserTransaction" /> </property> <property name="allowCustomIsolationLevels" value="true"/> <!—必须设置,否则程序出现异常 JtaTransactionManager does not support custom isolation levels by default --><!—oracle数据源定义 --> </bean> <bean id="oracleDS" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close"> <description>oracle xa datasource</description> <property name="uniqueResourceName"> <value>oracle_ds</value> </property> <property name="xaDataSourceClassName"> <value>oracle.jdbc.xa.client.OracleXADataSource</value> </property> <property name="xaProperties"> <props> <prop key="user">orcl</prop> <prop key="password">123</prop> <prop key="URL">jdbc:oracle:thin:@ localhost:1521:orcl</prop> </props> </property> <property name="testQuery"> <value>select 1 from dual</value> <!—尽力加上,不然会出现告警 --> </property> </bean> <!— mysql数据源定义 --> <bean id="dataSourceoracle2" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close"> <description>mysql xa datasource</description> <property name="uniqueResourceName"> <value>mysql_ds</value> </property> <property name="xaDataSourceClassName"> <value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value> </property> <property name="xaProperties"> <props> <prop key="user">root</prop> <prop key="password">123</prop> <prop key="URL">jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8</prop> </props> </property> <property name="testQuery"> <value>select 1</value><!—尽力加上,不然会出现告警 --> </property> </bean><!-- ibatis配置源 --> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation" value="classpath:sql-moredata-config.xml" /> <property name="dataSource" ref="oracleDS" /> </bean> <!-- ibatis配置源2 --> <bean id="sqlMapClient2" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation" value="classpath:sql-moredata2-config.xml" /> <property name="dataSource" ref="dataSourceoracle2" /> </bean><bean id="testDaoR" class="com.test.moredata.TestDAOImpR"> <property name="sqlMapClient" ref="sqlMapClient"></property> </bean><bean id="testDao" class="com.test.moredata.TestDAOImp"> <property name="sqlMapClient" ref="sqlMapClient2"></property> </bean> <bean id="testService" class="com.test.moredata.TestService"> <property name="testDao" ref="testDao"></property> <property name="testDaoR" ref="testDaoR"></property> </bean><!— aop配置 --> <tx:advice id="transactionManagerAdivice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" isolation="READ_COMMITTED" propagation="REQUIRED" rollback-for="java.lang.RuntionException" /> </tx:attributes> </tx:advice><aop:config> <aop:pointcut expression="execution(* com.test.moredata.TestService.*(..))" id="tsServicePc" /> <aop:advisor advice-ref="transactionManagerAdivice" pointcut-ref="tsServicePc" /> </aop:config>
Ok主要的配置就是这样,下面就是自己写下dao,service等
- 使用Atomikos Transactions Essentials实现多数据源JTA分布式事务
- 使用Atomikos Transactions Essentials实现多数据源JTA分布式事务
- 使用Atomikos Transactions Essentials实现多数据源JTA分布式事务
- spring atomikos 实现多数据源 分布式事务
- atomikos实现多数据源支持分布式事务管理(spring、tomcat、JTA)
- 分布式事务JTA实现之:SSM+ATOMIKOS
- Springboot+atomikos+jta实现分布式事务统一管理
- 多数据源 atomikos 打造Java分布式事务
- Spring Boot Druid 多数据源 Atomikos 分布式事务
- JTA分布式事务实战(atomikos)
- Spring多数据源分布式事务管理/springmvc+spring+atomikos[jta]+druid+mybatis
- Spring多数据源分布式事务管理/springmvc+spring+atomikos[jta]+druid+mybatis
- Spring多数据源分布式事务管理/springmvc+spring+atomikos[jta]+druid+mybatis
- java分布式事务(JTA)实现 jotm和atomikos
- java分布式事务(JTA)实现 jotm和atomikos
- Postgresql 分布式事务JTA实现Atomikos与Spring集成实践
- Spring JTA接口+Atomikos实现申明式分布式事务控制
- Spring3.0+Hibernate+Atomikos集成构建JTA的分布式事务--解决多数据源跨库事务
- POJ 2635 The Embarrassed Cryptographer
- pache Avro 与 Thrift 比较
- 启用Cookiee可以实现免登陆大大方便了WEB项目的调试
- 【健康提示】吃鱼过多等于吃毒药
- 超级实用!无需洗牙,教你5分钟消除牙垢!!
- 使用Atomikos Transactions Essentials实现多数据源JTA分布式事务
- css3动画特性
- POJ 1845 Sumdiv
- C/C++中的日期和时间 TIME_T与STRUCT TM转换
- 态度决定成果--计原
- ubuntu无线驱动安装
- 四款有名的开源海量数据采集工具
- Android eclipse中程序调试
- Start the Android