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

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

第一步:配置ApplicationContext.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" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
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"
default-lazy-init="true"> <!-- spring atomikos 配置 开始-->
<!-- mysql数据源 -->
<bean id="mysqlDS" 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" />
<property name="xaProperties">
<props>
<prop key="user">userName</prop>
<prop key="password">password</prop>
<prop key="URL">jdbc\:mysql\://127.0.0.1\:3306/dataBaseName?autoReconnect\=true</prop>
</props>
</property>
<!-- 连接池里面连接的个数? -->
<property name="poolSize" value="3"/>
</bean> <!-- oracle数据源 -->
<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">userName</prop>
<prop key="password">password</prop>
<prop key="URL">jdbc\:oracle\:thin\:@127.0.0.1\:1521\:dataBaseName</prop>
</props>
</property>
<!-- 连接池里面连接的个数? -->
<property name="poolSize" value="3"/>
</bean> <!-- atomikos事务管理器 -->
<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="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
<property name="transactionTimeout" value="300" />
</bean> <!-- spring 事务管理器 -->
<bean id="springTransactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManager">
<ref bean="atomikosTransactionManager" />
</property>
<property name="userTransaction">
<ref bean="atomikosUserTransaction" />
</property>
</bean> <!-- spring 事务模板 我在项目当中用的是编程式事务-->
<bean id="transactionTemplate"
class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager">
<ref bean="springTransactionManager" />
</property>
</bean> <bean id="simpleJdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
<constructor-arg>
<ref bean="mysqlDS" />
</constructor-arg>
</bean> <bean id="simplejdbcTemplateOra" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
<constructor-arg>
<ref bean="oracleDS" />
</constructor-arg>
</bean> <!-- spring atomikos 配置 结束--> <!-- 接下来就是具体的Dao的配置 -->
<bean id="oracleJtaDao" class="com.dao.TerminalOracleJtaDao">
<property name="simplejdbcTemplateOra">
<ref bean="simplejdbcTemplateOra" />
</property>
<property name="transactionTemplate">
<ref bean="transactionTemplate" />
</property>
</bean> <bean id="myaqlJtaDao" class="com.dao.TerminalMyaqlJtaDao">
<property name="simpleJdbcTemplate">
<ref bean="simpleJdbcTemplate" />
</property>
<property name="transactionTemplate">
<ref bean="transactionTemplate" />
</property>
</bean>
</beans>

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

# SAMPLE PROPERTIES FILE FOR THE TRANSACTION SERVICE
# THIS FILE ILLUSTRATES THE DIFFERENT SETTINGS FOR THE TRANSACTION MANAGER
# UNCOMMENT THE ASSIGNMENTS TO OVERRIDE DEFAULT VALUES; # Required: factory implementation class of the transaction core.
# NOTE: there is no default for this, so it MUST be specified!
#
com.atomikos.icatch.service=com.atomikos.icatch.standalone.UserTransactionServiceFactory # Set base name of file where messages are output
# (also known as the 'console file').
#
com.atomikos.icatch.console_file_name = tm.out # Size limit (in bytes) for the console file;
# negative means unlimited.
#
# com.atomikos.icatch.console_file_limit=-1 # For size-limited console files, this option
# specifies a number of rotating files to
# maintain.
#
# com.atomikos.icatch.console_file_count=1 # Set the number of log writes between checkpoints
#
# com.atomikos.icatch.checkpoint_interval=500 # Set output directory where console file and other files are to be put
# make sure this directory exists!
#
# com.atomikos.icatch.output_dir = ./ # Set directory of log files; make sure this directory exists!
#
# com.atomikos.icatch.log_base_dir = ./ # Set base name of log file
# this name will be used as the first part of
# the system-generated log file name
#
com.atomikos.icatch.log_base_name = tmlog # Set the max number of active local transactions
# or -1 for unlimited.
#
# com.atomikos.icatch.max_actives = 50 # Set the default timeout (in milliseconds) for local transactions
#
# com.atomikos.icatch.default_jta_timeout = 10000 # Set the max timeout (in milliseconds) for local transactions
#
# com.atomikos.icatch.max_timeout = 300000 # The globally unique name of this transaction manager process
# override this value with a globally unique name
#
com.atomikos.icatch.tm_unique_name = tm # Do we want to use parallel subtransactions? JTA's default
# is NO for J2EE compatibility
#
# com.atomikos.icatch.serial_jta_transactions=true # If you want to do explicit resource registration then
# you need to set this value to false.
#
# com.atomikos.icatch.automatic_resource_registration=true # Set this to WARN, INFO or DEBUG to control the granularity
# of output to the console file.
#
com.atomikos.icatch.console_log_level=INFO # Do you want transaction logging to be enabled or not?
# If set to false, then no logging overhead will be done
# at the risk of losing data after restart or crash.
#
# com.atomikos.icatch.enable_logging=true # Should two-phase commit be done in (multi-)threaded mode or not?
# Set this to false if you want commits to be ordered according
# to the order in which resources are added to the transaction.
#
# NOTE: threads are reused on JDK 1.5 or higher.
# For JDK 1.4, thread reuse is enabled as soon as the
# concurrent backport is in the classpath - see
# http://mirrors.ibiblio.org/pub/mirrors/maven2/backport-util-concurrent/backport-util-concurrent/
#
# com.atomikos.icatch.threaded_2pc=false # Should shutdown of the VM trigger shutdown of the transaction core too?
#
# com.atomikos.icatch.force_shutdown_on_vm_exit=false

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

TransactionTemplate.getTransactionTemplate().execute(new TransactionCallback(){
public Object doInTransaction(TransactionStatus status){
boolean flag = true;
try { } catche (Exception e){
flag = false;
} finally {
if (!flag) {
status.status.setRollbackOnly();
}
}
}
})

参考: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. java 页面url传值中文乱码的解决方法

    parent.window.location.href 和 iframe中src的乱码问题.要在这两个url地址中传中文,必须加编码,然后再解码.编码:encodeURI(encodeURI(&quo ...

  2. chrome的timeline的问题?

    如图:Stalled.RequestSent等所表达的是什么意思呢? Stalled是浏览器得到要发出这个请求的指令,到请求可以发出的等待时间,一般是代理协商.以及等待可复用的TCP连接释放的时间,不 ...

  3. 如何使用eclipse进行嵌入式Linux的开发

    如何使用eclipse进行嵌入式Linux的开发 作者:曾宏安,华清远见嵌入式学院高级讲师. 如何使用eclipse进行嵌入式Linux的开发 习惯了在windows环境下开发的程序员在转到Linux ...

  4. android中如何实现离线缓存

    离线缓存就是在网络畅通的情况下将从服务器收到的数据保存到本地,当网络断开之后直接读取本地文件中的数据. 将网络数据保存到本地: 你可以自己写一个保存数据成本地文件的方法,保存在android系统的任意 ...

  5. 使用Windows Azure创建Windows系统虚拟机-下

    如何在创建虚拟机之后登录虚拟机 这部分将展示如何登录到虚拟机,所以你可以管理它的设置和你会上面运行的应用程序. 注意: 对于要求和故障排除技巧,请参阅“使用RDP或SSH连接到Azure虚拟机”( C ...

  6. PrintDbGrideh 打印数据

    PrintDbGrideh1.BeforeGridText.Clear;//添加 PrintDbGrideh1.BeforeGridText.Add( '订单 '); PrintDBGridEh1.P ...

  7. 泰泽新闻:英特尔三星双否认泰泽Tizen系统已死

    7月8日 据媒体TizenExperts报道,关于“Tizen系统跳票”的传闻已经遭到了英特尔和三星否认. 此前传闻三星自行研制的智能手机Tizen操作系统流产,但如今已经遭到了官方的否认. 英特尔三 ...

  8. HW3.24

    public class Solution { public static void main(String[] args) { int number = (int)(Math.random() * ...

  9. 杭电HDOJ--ACM1002(JAVA解题,运用BigInteger)(自定义MBigInteger 简单实现大数处理----完善后可以实现百亿计算器)

    转载声明:原文转自http://www.cnblogs.com/xiezie/p/5501901.html     JAVA解题:   import java.util.*; import java. ...

  10. hdu5593--ZYB's Tree(树形dp)

    问题描述 ZYB有一颗N个节点的树,现在他希望你对于每一个点,求出离每个点距离不超过KK的点的个数. 两个点(x,y)在树上的距离定义为两个点树上最短路径经过的边数, 为了节约读入和输出的时间,我们采 ...