使用Atomikos Transactions Essentials实现多数据源JTA分布式事务--转载
原文: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配置文件如下:
- <!-- 第一个数据库 -->
- < 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一样卖服务支持的。而且论坛页比较活跃,有问题很快可以解决。
使用Atomikos Transactions Essentials实现多数据源JTA分布式事务--转载的更多相关文章
- Springboot+Atomikos+Jpa+Mysql实现JTA分布式事务
1 前言 之前整理了一个spring+jotm实现的分布式事务实现,但是听说spring3.X后不再支持jotm了,jotm也有好几年没更新了,所以今天整理springboot+Atomikos+jp ...
- springboot整合多数据源解决分布式事务
一.前言 springboot整合多数据源解决分布式事务. 1.多数据源采用分包策略 2.全局分布式事务管理:jta-atomikos. ...
- atomikos实现多数据源支持分布式事务管理(spring、tomcat、JTA)
原文链接:http://iteye.blog.163.com/blog/static/1863080962012102945116222/ Atomikos TransactionsEssenti ...
- Spring 3.0 + Atomikos构建jta分布式事务
Spring3.0已经不再支持jtom了,不过我们可以用第三方开源软件atomikos(http://www.atomikos.com/)来实现.Atomikos是目前在分布式事务管理中做得相当不错的 ...
- JTA 分布式事务
什么是JTA - 2009-07-25 18:31:06| 分类: 技术文章|举报|字号 订阅 什么是JTA? Java Transaction API(Java事务API) (JTA)Ja ...
- SpringCloud微服务实战——搭建企业级开发框架(二十七):集成多数据源+Seata分布式事务+读写分离+分库分表
读写分离:为了确保数据库产品的稳定性,很多数据库拥有双机热备功能.也就是,第一台数据库服务器,是对外提供增删改业务的生产服务器:第二台数据库服务器,主要进行读的操作. 目前有多种方式实现读写分离,一种 ...
- Springboot + Atomikos + Druid + Mysql 实现JTA分布式事务
DataSource 配置 package com.cheng.dynamic.config; import java.util.Properties; import javax.sql.DataSo ...
- 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 ...
- SpringBoot整合mybatis多数据源,支持分布式事务
编码工具:IDEA SpringBoot版本:2.0.1 JDK版本:1.8 1.使用IDEA构建一个Maven工程 ,添加依赖: <?xml version="1.0" e ...
随机推荐
- 学习面试题Day02
1.Java的引用和C++的指针有什么区别? 1.类型:引用其值为地址的数据元素,Java封装了的地址,可以转成字符串查看,长度可以不必关心.C++指针是一个装地址的变量,长度一般是计算机字长,可以认 ...
- Retrofit – Java(Android) 的REST 接口封装类库
Retrofit 和Java领域的ORM概念类似, ORM把结构化数据转换为Java对象,而Retrofit 把REST API返回的数据转化为Java对象方便操作.同时还封装了网络代码的调用. 例如 ...
- mac远程连接 win8
首先 打开组策略 .windows8 下 按win+R 可以唤出运行,输入[gpedit.msc]按下 回车键 选择[计算机配置]>[管理模板]>[Windows组件]>[远程桌面服 ...
- case中定义变量
1.c语言switch/case中定义变量 case 0: int b = 0 ; printf("0");break; case 1: printf("1" ...
- [LeetCode]Linked List Cycle II解法学习
问题描述如下: Given a linked list, return the node where the cycle begins. If there is no cycle, return nu ...
- 【转载】awk简介
[转载自]http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2858470.html 简介 awk是一个强大的文本分析工具,相对于grep的查找, ...
- rpc,客户端与NameNode通信的过程
远程过程:java进程.即一个java进程调用另外一个java进程中对象的方法. 调用方称作客户端(client),被调用方称作服务端(server).rpc的通信在java中表现为客户端去调用服务端 ...
- Java网络编程(URL&URLConnection)
package cn.itcast.net.p2.ie_server; import java.io.IOException; import java.io.InputStream; import j ...
- 单机c/s软件如何让老板在异地看销售营业报表
单机软件,让人的感觉就只能在本地使用. 单机版c/s软件,数据存放在本机上,老板想要查看销售报表的话,需要跑到公司的那台电脑上才能查看,这对于在外面四处跑业务的老板来说,基本上是不可能做到的.但每天的 ...
- SQL Server 2016原生支持JSON
转载原地址: http://www.cnblogs.com/lyhabc/p/4747694.html SQL Server 2005 开始支持 XML 数据类型,提供原生的 XML数据类型.XML ...