Spring3.0已经不再支持jtom了,不过我们可以用第三方开源软件atomikos(http://www.atomikos.com/)来实现。Atomikos是目前在分布式事务管理中做得相当不错的开源软件。有10年以上的经验,Atomikos保障您的关键事务和 防止昂贵的数据丢失在发生系统故障或事故中。Atomikos支持XA(全局事务)和NON-XA(非全局事务),NON-XA效率高于XA.本文主要是讲XA事件,因为要在不同的数据库中操作多张表。

接下来说一下怎么和spring3.0结合使用。

第一步:配置ApplicationContext.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" xmlns:jee="http://www.springframework.org/schema/jee"
  4. xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
  6. default-lazy-init="true">
  7.  
  8. <!-- spring atomikos 配置 开始-->
  9. <!-- mysql数据源 -->
  10. <bean id="mysqlDS" class="com.atomikos.jdbc.AtomikosDataSourceBean"
  11. init-method="init" destroy-method="close">
  12. <description>mysql xa datasource</description>
  13. <property name="uniqueResourceName">
  14. <value>mysql_ds</value>
  15. </property>
  16. <property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
  17. <property name="xaProperties">
  18. <props>
  19. <prop key="user">userName</prop>
  20. <prop key="password">password</prop>
  21. <prop key="URL">jdbc\:mysql\://127.0.0.1\:3306/dataBaseName?autoReconnect\=true</prop>
  22. </props>
  23. </property>
  24. <!-- 连接池里面连接的个数? -->
  25. <property name="poolSize" value="3"/>
  26. </bean>
  27.  
  28. <!-- oracle数据源 -->
  29. <bean id="oracleDS" class="com.atomikos.jdbc.AtomikosDataSourceBean"
  30. init-method="init" destroy-method="close">
  31. <description>oracle xa datasource</description>
  32. <property name="uniqueResourceName">
  33. <value>oracle_ds</value>
  34. </property>
  35. <property name="xaDataSourceClassName">
  36. <value>oracle.jdbc.xa.client.OracleXADataSource</value>
  37. </property>
  38. <property name="xaProperties">
  39. <props>
  40. <prop key="user">userName</prop>
  41. <prop key="password">password</prop>
  42. <prop key="URL">jdbc\:oracle\:thin\:@127.0.0.1\:1521\:dataBaseName</prop>
  43. </props>
  44. </property>
  45. <!-- 连接池里面连接的个数? -->
  46. <property name="poolSize" value="3"/>
  47. </bean>
  48.  
  49. <!-- atomikos事务管理器 -->
  50. <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
  51. init-method="init" destroy-method="close">
  52. <description>UserTransactionManager</description>
  53. <property name="forceShutdown">
  54. <value>true</value>
  55. </property>
  56. </bean>
  57.  
  58. <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
  59. <property name="transactionTimeout" value="300" />
  60. </bean>
  61.  
  62. <!-- spring 事务管理器 -->
  63. <bean id="springTransactionManager"
  64. class="org.springframework.transaction.jta.JtaTransactionManager">
  65. <property name="transactionManager">
  66. <ref bean="atomikosTransactionManager" />
  67. </property>
  68. <property name="userTransaction">
  69. <ref bean="atomikosUserTransaction" />
  70. </property>
  71. </bean>
  72.  
  73. <!-- spring 事务模板 我在项目当中用的是编程式事务-->
  74. <bean id="transactionTemplate"
  75. class="org.springframework.transaction.support.TransactionTemplate">
  76. <property name="transactionManager">
  77. <ref bean="springTransactionManager" />
  78. </property>
  79. </bean>
  80.  
  81. <bean id="simpleJdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
  82. <constructor-arg>
  83. <ref bean="mysqlDS" />
  84. </constructor-arg>
  85. </bean>
  86.  
  87. <bean id="simplejdbcTemplateOra" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
  88. <constructor-arg>
  89. <ref bean="oracleDS" />
  90. </constructor-arg>
  91. </bean>
  92.  
  93. <!-- spring atomikos 配置 结束-->
  94.  
  95. <!-- 接下来就是具体的Dao的配置 -->
  96. <bean id="oracleJtaDao" class="com.dao.TerminalOracleJtaDao">
  97. <property name="simplejdbcTemplateOra">
  98. <ref bean="simplejdbcTemplateOra" />
  99. </property>
  100. <property name="transactionTemplate">
  101. <ref bean="transactionTemplate" />
  102. </property>
  103. </bean>
  104.  
  105. <bean id="myaqlJtaDao" class="com.dao.TerminalMyaqlJtaDao">
  106. <property name="simpleJdbcTemplate">
  107. <ref bean="simpleJdbcTemplate" />
  108. </property>
  109. <property name="transactionTemplate">
  110. <ref bean="transactionTemplate" />
  111. </property>
  112. </bean>
  113. </beans>

第二步:配置jta.properties文件,此文件是必须的。主是是设置atomikos启动的一些参数,比如日志的输出级别,日志文件的名称等

  1. # SAMPLE PROPERTIES FILE FOR THE TRANSACTION SERVICE
  2. # THIS FILE ILLUSTRATES THE DIFFERENT SETTINGS FOR THE TRANSACTION MANAGER
  3. # UNCOMMENT THE ASSIGNMENTS TO OVERRIDE DEFAULT VALUES;
  4.  
  5. # Required: factory implementation class of the transaction core.
  6. # NOTE: there is no default for this, so it MUST be specified!
  7. #
  8. com.atomikos.icatch.service=com.atomikos.icatch.standalone.UserTransactionServiceFactory
  9.  
  10. # Set base name of file where messages are output
  11. # (also known as the 'console file').
  12. #
  13. com.atomikos.icatch.console_file_name = tm.out
  14.  
  15. # Size limit (in bytes) for the console file;
  16. # negative means unlimited.
  17. #
  18. # com.atomikos.icatch.console_file_limit=-1
  19.  
  20. # For size-limited console files, this option
  21. # specifies a number of rotating files to
  22. # maintain.
  23. #
  24. # com.atomikos.icatch.console_file_count=1
  25.  
  26. # Set the number of log writes between checkpoints
  27. #
  28. # com.atomikos.icatch.checkpoint_interval=500
  29.  
  30. # Set output directory where console file and other files are to be put
  31. # make sure this directory exists!
  32. #
  33. # com.atomikos.icatch.output_dir = ./
  34.  
  35. # Set directory of log files; make sure this directory exists!
  36. #
  37. # com.atomikos.icatch.log_base_dir = ./
  38.  
  39. # Set base name of log file
  40. # this name will be used as the first part of
  41. # the system-generated log file name
  42. #
  43. com.atomikos.icatch.log_base_name = tmlog
  44.  
  45. # Set the max number of active local transactions
  46. # or -1 for unlimited.
  47. #
  48. # com.atomikos.icatch.max_actives = 50
  49.  
  50. # Set the default timeout (in milliseconds) for local transactions
  51. #
  52. # com.atomikos.icatch.default_jta_timeout = 10000
  53.  
  54. # Set the max timeout (in milliseconds) for local transactions
  55. #
  56. # com.atomikos.icatch.max_timeout = 300000
  57.  
  58. # The globally unique name of this transaction manager process
  59. # override this value with a globally unique name
  60. #
  61. com.atomikos.icatch.tm_unique_name = tm
  62.  
  63. # Do we want to use parallel subtransactions? JTA's default
  64. # is NO for J2EE compatibility
  65. #
  66. # com.atomikos.icatch.serial_jta_transactions=true
  67.  
  68. # If you want to do explicit resource registration then
  69. # you need to set this value to false.
  70. #
  71. # com.atomikos.icatch.automatic_resource_registration=true
  72.  
  73. # Set this to WARN, INFO or DEBUG to control the granularity
  74. # of output to the console file.
  75. #
  76. com.atomikos.icatch.console_log_level=INFO
  77.  
  78. # Do you want transaction logging to be enabled or not?
  79. # If set to false, then no logging overhead will be done
  80. # at the risk of losing data after restart or crash.
  81. #
  82. # com.atomikos.icatch.enable_logging=true
  83.  
  84. # Should two-phase commit be done in (multi-)threaded mode or not?
  85. # Set this to false if you want commits to be ordered according
  86. # to the order in which resources are added to the transaction.
  87. #
  88. # NOTE: threads are reused on JDK 1.5 or higher.
  89. # For JDK 1.4, thread reuse is enabled as soon as the
  90. # concurrent backport is in the classpath - see
  91. # http://mirrors.ibiblio.org/pub/mirrors/maven2/backport-util-concurrent/backport-util-concurrent/
  92. #
  93. # com.atomikos.icatch.threaded_2pc=false
  94.  
  95. # Should shutdown of the VM trigger shutdown of the transaction core too?
  96. #
  97. # com.atomikos.icatch.force_shutdown_on_vm_exit=false

第三步:需要分布事务处理的地方手动开启事务

  1. TransactionTemplate.getTransactionTemplate().execute(new TransactionCallback(){
  2. public Object doInTransaction(TransactionStatus status){
  3. boolean flag = true;
  4. try {
  5.  
  6. } catche (Exception e){
  7. flag = false;
  8. } finally {
  9. if (!flag) {
  10. status.status.setRollbackOnly();
  11. }
  12. }
  13. }
  14. })

参考:http://www.tuicool.com/articles/Qziu2i

http://www.blogjava.net/pdw2009/archive/2008/05/25/202725.html

http://www.cnblogs.com/davenkin/archive/2013/03/19/java-tranaction-8.html

关于jotm的分布式案例请参见:http://my.oschina.net/xianggao/blog/543790?fromerr=UzOHWQjT

Spring 3.0 + Atomikos构建jta分布式事务的更多相关文章

  1. spring3.0+Atomikos 构建jta的分布式事务 -- NO

    摘自: http://gongjiayun.iteye.com/blog/1570111 spring3.0+Atomikos 构建jta的分布式事务 spring3.0已经不再支持jtom了,不过我 ...

  2. spring3.0+Atomikos 构建jta的分布式事务

    摘自: http://gongjiayun.iteye.com/blog/1570111 spring3.0+Atomikos 构建jta的分布式事务 spring3.0已经不再支持jtom了,不过我 ...

  3. Spring Boot2.0之多数据源分布式事务问题

    分布式事务解决方案的问题, 分布式事务产生的原因: 多个不同的服务连接不同的数据源 ,做分布式事务的管理. 这种情况是连接两个数据源的情况,然后事务管理器是这样的 只管理了test02的这端业务代码. ...

  4. 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 ...

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

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

  6. 使用Atomikos Transactions Essentials实现多数据源JTA分布式事务--转载

    原文:http://www.ite/topic/122700 9.17 update:使用NonXADataSourceBean. Mysql在5.0版本和Connecter/J5.0版本后提供了XA ...

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

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

  8. JTA 分布式事务

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

  9. spring boot:shardingsphere+druid整合seata分布式事务(spring boot 2.3.3)

    一,shardingshpere为什么要整合seata? 分库分表是数据库扩展中最常用的处理方法, shardingshpere作为使用最广泛的分表中间件, 如果不支持分布式事务,则它的数据一致性就会 ...

随机推荐

  1. poj2079

    根据凸包的单峰性质,穷举第一个顶点然后先更新第三个顶点,再更新第二个顶点 ..] of longint; ans,n,t,k,i,j:longint; function cross(i,j,k:lon ...

  2. Win系统下制作U盘CLOVER引导+安装原版Mavericks10.9

    啃苹果有一段时间了,之前一直用白苹果,但是白苹果配置有所限制,对于我搞音频的人来讲,显得有点拖沓.所以研究了将近2年的黑苹果,最近心血来潮给大家一个比较傻瓜式的教程,首先强调一点,黑苹果是需要折腾的, ...

  3. 【 D3.js 进阶系列 — 6.2 】 饼状图的拖拽

    本文讲解稍微复杂一些的拖拽应用,即拖拽饼图的各部分. 在[入门 - 第 9.1 章]讲解了如何制作饼状图.饼状图的各部分是用具有宽度的弧线来表示的.在与用户进行交互的时候,如果每一部分都能拖拽,是很有 ...

  4. 【转】Ubuntu更改语言环境设置

    原文网址:http://studiogang.blog.51cto.com/505887/385199 上午装了下Ubuntu 10.4,默认安装时选择的语言是english的,结果由于英语水平太次, ...

  5. HDU-1700 Points on Cycle

    这题的俩种方法都是看别人的代码,方法可以学习学习,要多看看.. 几何题用到向量.. Points on Cycle Time Limit: 1000/1000 MS (Java/Others)     ...

  6. 从 NavMesh 网格寻路回归到 Grid 网格寻路。

    上一个项目的寻路方案是客户端和服务器都采用了 NavMesh 作为解决方案,当时的那几篇文章(一,二,三)是很多网友留言和后台发消息询问最多的,看来这个方案有着广泛的需求.但因为是商业项目,我无法贴出 ...

  7. HDU 4622 Reincarnation(SAM)

    Problem Description Now you are back,and have a task to do:Given you a string s consist of lower-cas ...

  8. 如何通过Visual Studio发布Azure应用程序

    发布 Azure 云服务 使用 Azure Tools for Visual Studio,可以直接从 Visual Studio 将云服务发布到 Azure. 在发布 Azure 云服务之前,必须已 ...

  9. Esper系列(一)初探

    Esper介绍 Esper是一个Java开发并且开源的轻量级和可扩展的事件流处理和复合事件处理引擎,并提供了定制的事件处理语言(EPL). 应用场景 某个用户在请求登录服务时,n秒内连续m次未登录成功 ...

  10. A list of base boxes for Vagrant - Vagrantbox.es

    Create image server with nginx + lua (Openresty) + graphicsmagick (Part I) | Ian's PhotograPhy Blog ...