java跨库事务Atomikos
1:引入额外的jar
<dependency>
<groupId>com.atomikos</groupId>
<artifactId>transactions-jdbc</artifactId>
<version>4.0.6</version>
</dependency>
<dependency>
<groupId>com.atomikos</groupId>
<artifactId>transactions-jta</artifactId>
<version>4.0.6</version>
</dependency>
<dependency>
<groupId>com.atomikos</groupId>
<artifactId>transactions-api</artifactId>
<version>4.0.6</version>
</dependency>
<dependency>
<groupId>com.atomikos</groupId>
<artifactId>transactions</artifactId>
<version>4.0.6</version>
</dependency>
<dependency>
<groupId>com.atomikos</groupId>
<artifactId>transactions-jms</artifactId>
<version>4.0.6</version>
</dependency>
<dependency>
<groupId>com.atomikos</groupId>
<artifactId>atomikos-util</artifactId>
<version>4.0.6</version>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
2:配制文件
#mysql
index.jdbc.driverClassName=com.mysql.jdbc.jdbc2.optional.MysqlXADataSource
index.jdbc.url=jdbc:mysql://*.*.*.*:4316/k12_fee_tabindex?serverTimezone=UTC&useSSL=false&characterEncoding=UTF8
index.jdbc.username=myhuiqu
index.jdbc.password=Huiqu.com@123 merch.jdbc.driverClassName=com.mysql.jdbc.jdbc2.optional.MysqlXADataSource
merch.jdbc.url=jdbc:mysql://1.1.1.1.1:4316/k12_fee?serverTimezone=UTC&useSSL=false&characterEncoding=UTF8
merch.jdbc.username=myhuiqu
merch.jdbc.password=Huiqu.com@123 user.jdbc.driverClassName=com.mysql.jdbc.jdbc2.optional.MysqlXADataSource
user.jdbc.url=jdbc:mysql://1.1.1.1.1:4316/k12_fee_userclient?serverTimezone=UTC&useSSL=false&characterEncoding=UTF8
user.jdbc.username=myhuiqu
user.jdbc.password=Huiqu.com@123 #active
brokerURL=tcp://1.1.1.1:61616
userName=admin
password=abc123
3:数据源配制
spring-index-orm.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"
default-lazy-init="false"> <context:annotation-config /> <context:property-placeholder location="classpath*:/**/*.properties"
ignore-unresolvable="true" /> <context:component-scan base-package="fbs.demo">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan> <bean id="k12_index_dataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close">
<property name="uniqueResourceName" value="ds1"/>
<property name="xaDataSourceClassName" value="${index.jdbc.driverClassName}"/>
<property name="xaProperties">
<props>
<prop key="url">${index.jdbc.url}</prop>
<prop key="user">${index.jdbc.username}</prop>
<prop key="password">${index.jdbc.password}</prop>
</props>
</property>
<property name="minPoolSize" value="10" />
<property name="maxPoolSize" value="100" />
<property name="borrowConnectionTimeout" value="30" />
<property name="testQuery" value="select 1" />
<property name="maintenanceInterval" value="60" />
</bean> <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManager">
<bean class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close">
<property name="forceShutdown" value="true"/>
</bean>
</property>
<property name="userTransaction">
<bean class="com.atomikos.icatch.jta.UserTransactionImp"/>
</property>
</bean> <tx:annotation-driven/> <bean id="indexJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="k12_index_dataSource" />
</bean> </beans>
spring-merch-orm.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"
default-lazy-init="false"> <context:annotation-config /> <context:property-placeholder location="classpath*:/**/*.properties"
ignore-unresolvable="true" /> <context:component-scan base-package="fbs.demo">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan> <bean id="k12_merch_dataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close">
<property name="uniqueResourceName" value="ds2"/>
<property name="xaDataSourceClassName" value="${merch.jdbc.driverClassName}"/>
<property name="xaProperties">
<props>
<prop key="url">${merch.jdbc.url}</prop>
<prop key="user">${merch.jdbc.username}</prop>
<prop key="password">${merch.jdbc.password}</prop>
</props>
</property>
<property name="minPoolSize" value="10" />
<property name="maxPoolSize" value="100" />
<property name="borrowConnectionTimeout" value="30" />
<property name="testQuery" value="select 1" />
<property name="maintenanceInterval" value="60" />
</bean> <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManager">
<bean class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close">
<property name="forceShutdown" value="true"/>
</bean>
</property>
<property name="userTransaction">
<bean class="com.atomikos.icatch.jta.UserTransactionImp"/>
</property>
</bean> <tx:annotation-driven /> <bean id="merchJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="k12_merch_dataSource" />
</bean> </beans>
spring-user-orm.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"
default-lazy-init="false"> <context:annotation-config /> <context:property-placeholder location="classpath*:/**/*.properties"
ignore-unresolvable="true" /> <context:component-scan base-package="fbs.demo">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan> <bean id="k12_user_dataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close">
<property name="uniqueResourceName" value="ds3"/>
<property name="xaDataSourceClassName" value="${user.jdbc.driverClassName}"/>
<property name="xaProperties">
<props>
<prop key="url">${user.jdbc.url}</prop>
<prop key="user">${user.jdbc.username}</prop>
<prop key="password">${user.jdbc.password}</prop>
</props>
</property>
<property name="minPoolSize" value="10" />
<property name="maxPoolSize" value="100" />
<property name="borrowConnectionTimeout" value="30" />
<property name="testQuery" value="select 1" />
<property name="maintenanceInterval" value="60" />
</bean> <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManager">
<bean class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close">
<property name="forceShutdown" value="true"/>
</bean>
</property>
<property name="userTransaction">
<bean class="com.atomikos.icatch.jta.UserTransactionImp"/>
</property>
</bean> <tx:annotation-driven /> <bean id="userJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="k12_user_dataSource" />
</bean> </beans>
spring-jms.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:amq="http://activemq.apache.org/schema/core"
xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.8.0.xsd">
<context:property-placeholder location="classpath*:/**/*.properties" />
<jms:annotation-driven></jms:annotation-driven> <amq:connectionFactory id="amqConnectionFactory"
brokerURL="${brokerURL}" useAsyncSend="true" /> <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="connectionFactory"
class="org.springframework.jms.connection.CachingConnectionFactory">
<constructor-arg ref="amqConnectionFactory" />
<property name="sessionCacheSize" value="10" />
<property name="cacheConsumers" value="false"></property>
<property name="cacheProducers" value="false"></property>
</bean> <!-- 类型转换器 -->
<bean id="messageConverter"
class="org.springframework.jms.support.converter.SimpleMessageConverter" /> <!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 -->
<bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="messageConverter" ref="messageConverter" />
<!-- 非pub/sub模型(发布/订阅),false即队列模式 ,true为发布/订阅模式 -->
<property name="pubSubDomain" value="false" />
<property name="explicitQosEnabled" value="true" />
<!--发送模式,1:非持久化,2:持久化 -->
<property name="deliveryMode" value="2"></property>
<!--开启分布式事务 -->
<property name="sessionTransacted" value="true" />
</bean> <bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="messageConverter" ref="messageConverter" />
<!-- 非pub/sub模型(发布/订阅),false即队列模式 ,true为发布/订阅模式 -->
<property name="pubSubDomain" value="true" />
<property name="explicitQosEnabled" value="true" />
<!--发送模式,1:非持久化,2:持久化 -->
<property name="deliveryMode" value="2"></property>
<!--开启分布式事务 -->
<property name="sessionTransacted" value="true" />
</bean> <!-- 商户订单同步到用户 -->
<bean id="abc" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg>
<value>abc</value>
</constructor-arg>
</bean>
<!-- 消息接收监听器用于异步接收消息 -->
<!-- <bean id="merch2UserListener" class="k12.fee.listener.queue.Merch2UserListener"></bean>
<bean
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="merch2UserQueue" />
<property name="messageListener" ref="merch2UserListener" />
<property name="concurrency" value="50-100" />
<property name="sessionTransacted" value="true"></property>
</bean> --> </beans>
4:service层
package fbs.demo.service; import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import com.huiqu.common.tools.utils.CodeUtil; @Service
public class FbsDemo1Service {
@Resource
private JdbcTemplate indexJdbcTemplate;
@Resource
private JdbcTemplate merchJdbcTemplate;
@Resource
private Destination abc;
@Resource
private JmsTemplate jmsQueueTemplate; @Transactional
public boolean add() {
String id1 = CodeUtil.getUUID();
String id2 = "2121";
String sql1="INSERT INTO tabindex0 (order_num, id_number, schools_id) VALUES (?,?,?)";
String sql2="INSERT INTO k12_fee.testa (id, NAME) VALUES (?,?)"; jmsQueueTemplate.send(abc, new MessageCreator() {
// 以map形式发送,以map形式接收
@Override
public Message createMessage(Session session) throws JMSException {
MapMessage message = null; message = session.createMapMessage();
session.createObjectMessage();
message.setString("buyer_id", "11111111111111"); return message;
} }); merchJdbcTemplate.update(sql2,id2,"");
indexJdbcTemplate.update(sql1, id1,"232",11);
throw new RuntimeException("");
//return true;
} }
做到以上几步就可以回滚了
注:tomcat启动时出错
在每一个项目中都指定atomikos的文件名称,修改jta.properties文件中的 com.atomikos.icatch.console_file_name = rm.out
com.atomikos.icatch.log_base_name = rmlog.log
com.atomikos.icatch.log_base_dir = ${catalina.base}/logs 两个属性的值,保证每个项目的名称都不一样
java跨库事务Atomikos的更多相关文章
- Spring3.0+Hibernate+Atomikos集成构建JTA的分布式事务--解决多数据源跨库事务
一.概念 分布式事务分布式事务是指事务的参与者.支持事务的服务器.资源服务器以及事务管理器分别位于不同的分布式系统的不同节点之上.简言之,同时操作多个数据库保持事务的统一,达到跨库事务的效果. JTA ...
- SSM使用AbstractRoutingDataSource后究竟如何解决跨库事务
Setting: 绑定三个数据源(XA规范),将三个实例绑定到AbStractoutingDataSource的实例MultiDataSource(自定义的)对象中,mybatis SqlSessi ...
- mysql 跨库事务XA
前一段时间在工作中遇到了跨库事务问题,后来在网上查询了一下,现在做一下整理和总结. 1.首先要确保mysql开启XA事务支持 SHOW VARIABLES LIKE '%XA%' 如果innodb_s ...
- 【Java EE 学习 19】【使用过滤器实现全站压缩】【使用ThreadLocal模式解决跨DAO事务回滚问题】
一.使用过滤器实现全站压缩 1.目标:对网站的所有JSP页面进行页面压缩,减少用户流量的使用.但是对图片和视频不进行压缩,因为图片和视频的压缩率很小,而且处理所需要的服务器资源很大. 2.实现原理: ...
- Java中的事务——JDBC事务和JTA事务
Java中的事务——JDBC事务和JTA事务 转载:http://www.hollischuang.com/archives/1658 之前的事务介绍基本都是数据库层面的事务,本文来介绍一下J2EE中 ...
- Mysql高手系列 - 第27篇:mysql如何确保数据不丢失的?我们借鉴这种设计思想实现热点账户高并发设计及跨库转账问题
Mysql系列的目标是:通过这个系列从入门到全面掌握一个高级开发所需要的全部技能. 欢迎大家加我微信itsoku一起交流java.算法.数据库相关技术. 这是Mysql系列第27篇. 本篇文章我们先来 ...
- 实现数据库的跨库join
功能需求 首先要理解原始需求是什么,为什么要跨库join.举个简单的例子,在日志数据库log_db有一份充值记录表pay_log,里面的用户信息只有一个userid:而用户的详细信息放在主库main_ ...
- SQL Server 跨库同步数据
最近有个需求是要跨库进行数据同步,两个数据库分布在两台物理计算机上,自动定期同步可以通过SQL Server代理作业来实现,但是前提是需要编写一个存储过程来实现同步逻辑处理.这里的存储过程用的不是op ...
- [转]Java中的事务
这篇Java事务的说明不错,所以把它转过来收藏了. 原博文地址:http://blog.csdn.net/gyf4817/article/details/5362444 通常的观念认为,事务仅与数据库 ...
随机推荐
- 减少C盘空间占用的技巧
1.搜索C盘中大小大于某个值的文件:C:\Windows\SoftwareDistribution这个文件夹下很多大文件 2.搜索*.log文件 3.C:\Users\Guangshan\AppDat ...
- Arduino Nano 读取ADS1100实例
利用Arduino Nano的wire库可以很方便对ADS1100进行设置和读取转换后的数据. /* * Arduino reads ADS1100 I2C 16bit diff ADC */ /* ...
- 前端jq设置下拉框的,单选框,复选框的帖子
$(function(){ var sex=$("#sex").val(); var marriageStatus=$("#marriageStatus").v ...
- Arcgis Android 坐标转换
http://spatialreference.org/首先,在上面的网站查出现有的坐标srid,然后查出目标Srid. 参考api 示例代码 Point point = new Point(120. ...
- 快速搭建windows服务器的可视化运维环境
开发好的程序部署在服务器上,如何对服务器的基本指标进行监控呢?最近对一套工具进行了研究,可以快速搭建服务器监管环境,很是强大,最重要的是它还很酷炫. 原理:数据采集+时序数据库+可视化,下面记录一下搭 ...
- table数据跑马灯效果
1.使用marquee标签实现普通文本字符串跑马灯效果. <marquee behavior="scroll" scrollamount="3" styl ...
- Metabase 从 H2 迁移到 MySQL 踩坑指南
写在前面的话 首先如果你看到了这篇文章,可能你就已经指定 Metabase 是啥了,我这里还是简单的做个说明: Metabase is the easy, open source way for ev ...
- Windows10使用
1.常见问题 Win10 的操作中心如果不见了 Windows10电脑系统时间校准 实现windows与ubuntu的之间的复制与粘贴 安卓手机传递文件到Windows系统电脑 安卓手机高速传递文件到 ...
- img标签中onerror用法
<img src="/statics/bazi/images/150x100.jpg" alt="#" onerror="this.style. ...
- jquery展开收缩列表
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8& ...