原文:http://www.ite/topic/122700

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配置文件如下:

xml 代码
  1. <!-- 第一个数据库 -->
  2. < bean   id = "dataSource"   class = "com.atomikos.jdbc.SimpleDataSourceBean"   init-method = "init"  destroy-method = "close" >
  3. < property   name = "uniqueResourceName" >
  4. < value > mysql/main </ value >
  5. </ property >
  6. < property   name = "xaDataSourceClassName" >
  7. <!-- 使用Mysql XADataSource(mysql>=5.0, Connector/J>=5.0才可以支持XADatasource)-->
  8. < value > com.mysql.jdbc.jdbc2.optional.MysqlXADataSource </ value >
  9. </ property >
  10. < property   name = "xaDataSourceProperties" >
  11. < value > URL =${jdbc.url}; user =${jdbc.username}; password =${jdbc.password} </ value >
  12. </ property >
  13. < property   name = "exclusiveConnectionMode" >
  14. < value > true </ value >
  15. </ property >
  16. < property   name = "connectionPoolSize" >
  17. < value > 3 </ value >
  18. </ property >
  19. < property   name = "validatingQuery" >
  20. < value > SELECT 1 </ value >
  21. </ property >
  22. </ bean >
  23. <!-- 第二个数据库 -->
  24. < bean   id = "dataSourceB"   class = "com.atomikos.jdbc.SimpleDataSourceBean"   init-method = "init"   destroy-method = "close" >
  25. < property   name = "uniqueResourceName" >
  26. < value > mysql/news </ value >
  27. </ property >
  28. < property   name = "xaDataSourceClassName" >
  29. <!-- 使用Mysql XADataSource(mysql>=5.0, Connector/J>=5.0才可以支持XADatasource)-->
  30. < value > com.mysql.jdbc.jdbc2.optional.MysqlXADataSource </ value >
  31. </ property >
  32. < property   name = "xaDataSourceProperties" >
  33. < value > URL =${jdbc.url.b}; user =${jdbc.username.b}; password =${jdbc.password.b} </ value >
  34. </ property >
  35. < property   name = "exclusiveConnectionMode" >
  36. < value > true </ value >
  37. </ property >
  38. < property   name = "connectionPoolSize" >
  39. < value > 3 </ value >
  40. </ property >
  41. < property   name = "validatingQuery" >
  42. < value > SELECT 1 </ value >
  43. </ property >
  44. </ bean >
  45. < bean   id = "lobHandler"   class = "org.springframework.jdbc.support.lob.DefaultLobHandler"   />
  46. <!-- 第一个数据库的sqlMapClient -->
  47. < bean   id = "sqlMapClient"   class = "org.springframework.orm.ibatis.SqlMapClientFactoryBean" >
  48. < property   name = "configLocation" >
  49. <!-- 包含第一个数据库表的map -->
  50. < value > classpath:/sqlmap-config.xml </ value >
  51. </ property >
  52. < property   name = "dataSource"   ref = "dataSource"   />
  53. < property   name = "lobHandler"   ref = "lobHandler"   />
  54. </ bean >
  55. <!-- 第二个数据库的sqlMapClient -->
  56. < bean   id = "sqlMapClientB"   class = "org.springframework.orm.ibatis.SqlMapClientFactoryBean" >
  57. < property   name = "configLocation" >
  58. <!-- 包含第一个数据库表的map -->
  59. < value > classpath:/sqlmap-configb.xml </ value >
  60. </ property >
  61. < property   name = "dataSource"   ref = "dataSourceB"   />
  62. < property   name = "lobHandler"   ref = "lobHandler"   />
  63. </ bean >
  64. <!-- Construct Atomikos UserTransactionManager, needed to configure Spring -->
  65. < bean   id = "atomikosTransactionManager"   class = "com.atomikos.icatch.jta.UserTransactionManager"   init-method = "init"
  66. destroy-method = "close" >
  67. <!--  when close is called, should we force transactions to terminate or not? -->
  68. < property   name = "forceShutdown" >
  69. < value > true </ value >
  70. </ property >
  71. </ bean >
  72. <!-- Also use Atomikos UserTransactionImp, needed to configure Spring  -->
  73. < bean   id = "atomikosUserTransaction"   class = "com.atomikos.icatch.jta.UserTransactionImp" >
  74. < property   name = "transactionTimeout"   value = "240"   />
  75. </ bean >
  76. <!-- Configure the Spring framework to use JTA transactions from Atomikos -->
  77. < bean   id = "transactionManager"   class = "org.springframework.transaction.jta.JtaTransactionManager" >
  78. < property   name = "transactionManager" >
  79. < ref   bean = "atomikosTransactionManager"   />
  80. </ property >
  81. < property   name = "userTransaction" >
  82. < ref   bean = "atomikosUserTransaction"   />
  83. </ property >
  84. </ bean >

事务的配置, 使用了spring2.0的语法,所以将namesapce也帖出来了.

xml 代码
  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. xmlns:aop = "http://www.springframework.org/schema/aop"
  5. xmlns:tx = "http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  7. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
  8. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
  9. default-autowire = "byName"   default-lazy-init = "true" >
  10. <!-- 支持 @AspectJ 标记-->
  11. < aop:aspectj-autoproxy />
  12. < aop:config   proxy-target-class = "true" >
  13. < aop:advisor   pointcut = "execution(* *Facade.*(..))"   advice-ref = "txAdvice" />
  14. < aop:advisor   pointcut = "execution(* *Manager.*(..))"   advice-ref = "txAdvice" />
  15. </ aop:config >
  16. < tx:advice   id = "txAdvice" >
  17. < tx:attributes >
  18. < tx:method   name = "get*"   read-only = "true" />
  19. < tx:method   name = "find*"   read-only = "true" />
  20. < tx:method   name = "has*"   read-only = "true" />
  21. < tx:method   name = "locate*"   read-only = "true" />
  22. < tx:method   name = "*" />
  23. </ tx:attributes >
  24. </ tx:advice >
  25. </ beans >

这样配置以后就可以使用分布式事务,测试中出现异常时事务也自动提交。和JOTM相比Atomikos Transactions Essentials更加稳定,它原来是商业项目,现在开源了。象mysql一样卖服务支持的。而且论坛页比较活跃,有问题很快可以解决。

使用Atomikos Transactions Essentials实现多数据源JTA分布式事务--转载的更多相关文章

  1. Springboot+Atomikos+Jpa+Mysql实现JTA分布式事务

    1 前言 之前整理了一个spring+jotm实现的分布式事务实现,但是听说spring3.X后不再支持jotm了,jotm也有好几年没更新了,所以今天整理springboot+Atomikos+jp ...

  2. springboot整合多数据源解决分布式事务

    一.前言        springboot整合多数据源解决分布式事务.             1.多数据源采用分包策略              2.全局分布式事务管理:jta-atomikos. ...

  3. atomikos实现多数据源支持分布式事务管理(spring、tomcat、JTA)

    原文链接:http://iteye.blog.163.com/blog/static/1863080962012102945116222/   Atomikos TransactionsEssenti ...

  4. Spring 3.0 + Atomikos构建jta分布式事务

    Spring3.0已经不再支持jtom了,不过我们可以用第三方开源软件atomikos(http://www.atomikos.com/)来实现.Atomikos是目前在分布式事务管理中做得相当不错的 ...

  5. JTA 分布式事务

    什么是JTA - 2009-07-25 18:31:06|  分类: 技术文章|举报|字号 订阅     什么是JTA? Java Transaction API(Java事务API) (JTA)Ja ...

  6. SpringCloud微服务实战——搭建企业级开发框架(二十七):集成多数据源+Seata分布式事务+读写分离+分库分表

    读写分离:为了确保数据库产品的稳定性,很多数据库拥有双机热备功能.也就是,第一台数据库服务器,是对外提供增删改业务的生产服务器:第二台数据库服务器,主要进行读的操作. 目前有多种方式实现读写分离,一种 ...

  7. Springboot + Atomikos + Druid + Mysql 实现JTA分布式事务

    DataSource 配置 package com.cheng.dynamic.config; import java.util.Properties; import javax.sql.DataSo ...

  8. spring+jotm+ibatis+mysql实现JTA分布式事务

    1 环境 1.1 软件环境  spring-framework-2.5.6.SEC01-with-dependencies.zip ibatis-2.3.4 ow2-jotm-dist-2.1.4-b ...

  9. SpringBoot整合mybatis多数据源,支持分布式事务

    编码工具:IDEA SpringBoot版本:2.0.1 JDK版本:1.8 1.使用IDEA构建一个Maven工程 ,添加依赖: <?xml version="1.0" e ...

随机推荐

  1. 【转】C/C++中可变参数函数的实现

    转自:http://www.cnblogs.com/cylee025/archive/2011/05/23/2054792.html 在C语言的stdarg.h头文件中提供了三个函数va_start, ...

  2. 《深入Java虚拟机学习笔记》- 第12章 整数运算

    Java虚拟机提供几种进行整数算术运算的操作码,他们执行基于int和long类型的运算.当byte.short和char类型值参与算术运算时,首先会将它们转换为int类型.这些操作码都不会抛出异常,溢 ...

  3. PHP 判断协议是否为HTTPS

    if ($_SERVER['HTTPS'] != "on") { echo "This is not HTTPS"; }else{ echo "Thi ...

  4. 树-红黑树(R-B Tree)

    红黑树概念 特殊的二叉查找树,每个节点上都有存储位表示节点的颜色是红(Red)或黑(Black).时间复杂度是O(lgn),效率高. 特性: (1)每个节点或者是黑色,或者是红色. (2)根节点是黑色 ...

  5. 简单的activemq的封装和使用

    天空中飘着小雨,实在是适合写代码的时节. 1 package ch02.chat; import java.io.Serializable; import javax.jms.Connection; ...

  6. Storm因机器断电等,启动supervisor异常

    Storm因机器断电等,启动supervisor错误 因机器断电或其他异常导致的supervisor意外终止,再次启动时报错: 2014-08-13 10:36:03 b.s.event [ERROR ...

  7. 矩阵取数游戏 NOIP 2007

    2016-05-31 17:26:45 题目链接: NOIP 2007 矩阵取数游戏(Codevs) 题目大意: 给定一个矩阵,每次在每一行的行首或者行尾取一个数乘上2^次数,求取完最多获得的分数 解 ...

  8. 【C语言】-数据输入-scanf( )和getchar( )

    格式化输入函数scanf( ) scanf( )功能: 按照指定的格式读入键盘上输入的若干个任意类型的数据,存入到argument参数所指向的内存单元,函数返回值为读入并赋给argument的数据个数 ...

  9. 转载SSIS中的容器和数据流—举例说明数据转换任务

    在上一个随笔中我们熟悉了数据流任务,现在来做一个例子,通过实践学习这些介绍的内容.这个例子从AdventureWorks数据库中取得数据,然后对数据进行聚合,排序,计算产生新列操作并输入到一个.csv ...

  10. 如何使用Keil仿真环境查看CPU类型字长?【worldsing笔记】

    笔者上次写了如何用IAR查CPU的字长和数据类长度的方法:点击这里查看 今天试着在Keil MDK 5.0 里查看CPU的字长和数据类长度,打开一个已有的工程,编译并进入Debug,如图1.1所示: ...