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的更多相关文章

  1. Spring3.0+Hibernate+Atomikos集成构建JTA的分布式事务--解决多数据源跨库事务

    一.概念 分布式事务分布式事务是指事务的参与者.支持事务的服务器.资源服务器以及事务管理器分别位于不同的分布式系统的不同节点之上.简言之,同时操作多个数据库保持事务的统一,达到跨库事务的效果. JTA ...

  2. SSM使用AbstractRoutingDataSource后究竟如何解决跨库事务

    Setting: 绑定三个数据源(XA规范),将三个实例绑定到AbStractoutingDataSource的实例MultiDataSource(自定义的)对象中,mybatis  SqlSessi ...

  3. mysql 跨库事务XA

    前一段时间在工作中遇到了跨库事务问题,后来在网上查询了一下,现在做一下整理和总结. 1.首先要确保mysql开启XA事务支持 SHOW VARIABLES LIKE '%XA%' 如果innodb_s ...

  4. 【Java EE 学习 19】【使用过滤器实现全站压缩】【使用ThreadLocal模式解决跨DAO事务回滚问题】

    一.使用过滤器实现全站压缩 1.目标:对网站的所有JSP页面进行页面压缩,减少用户流量的使用.但是对图片和视频不进行压缩,因为图片和视频的压缩率很小,而且处理所需要的服务器资源很大. 2.实现原理: ...

  5. Java中的事务——JDBC事务和JTA事务

    Java中的事务——JDBC事务和JTA事务 转载:http://www.hollischuang.com/archives/1658 之前的事务介绍基本都是数据库层面的事务,本文来介绍一下J2EE中 ...

  6. Mysql高手系列 - 第27篇:mysql如何确保数据不丢失的?我们借鉴这种设计思想实现热点账户高并发设计及跨库转账问题

    Mysql系列的目标是:通过这个系列从入门到全面掌握一个高级开发所需要的全部技能. 欢迎大家加我微信itsoku一起交流java.算法.数据库相关技术. 这是Mysql系列第27篇. 本篇文章我们先来 ...

  7. 实现数据库的跨库join

    功能需求 首先要理解原始需求是什么,为什么要跨库join.举个简单的例子,在日志数据库log_db有一份充值记录表pay_log,里面的用户信息只有一个userid:而用户的详细信息放在主库main_ ...

  8. SQL Server 跨库同步数据

    最近有个需求是要跨库进行数据同步,两个数据库分布在两台物理计算机上,自动定期同步可以通过SQL Server代理作业来实现,但是前提是需要编写一个存储过程来实现同步逻辑处理.这里的存储过程用的不是op ...

  9. [转]Java中的事务

    这篇Java事务的说明不错,所以把它转过来收藏了. 原博文地址:http://blog.csdn.net/gyf4817/article/details/5362444 通常的观念认为,事务仅与数据库 ...

随机推荐

  1. xml构建

    <a target="_blank" href="http://wpa.qq.com/msgrd?v=3&uin=346252320&site=qq ...

  2. softmax,softmax loss和cross entropy的讲解

    1 softmax 我们知道卷积神经网络(CNN)在图像领域的应用已经非常广泛了,一般一个CNN网络主要包含卷积层,池化层(pooling),全连接层,损失层等.这一篇主要介绍全连接层和损失层的内容, ...

  3. HTTP响应状态码参考

    HTTP响应状态码参考: 1xx:信息 Continue 服务器仅接收到部分请求,但是一旦服务器并没有拒绝该请求,客户端应该继续发送其余的请求. Switching Protocols 服务器转换协议 ...

  4. Elasticsearch学习(3) spring boot整合Elasticsearch的原生方式

    前面我们已经介绍了spring boot整合Elasticsearch的jpa方式,这种方式虽然简便,但是依旧无法解决我们较为复杂的业务,所以原生的实现方式学习能够解决这些问题,而原生的学习方式也是E ...

  5. “全栈2019”Java第二十五章:流程控制语句中循环语句while

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  6. [Maven实战-许晓斌]-[第二章]-2.4设置HTTP代理

  7. [ActionScript 3.0] AS3 弹性运动

    package com.views { import flash.display.Sprite; import flash.events.Event; import flash.geom.Point; ...

  8. [Swift]扩展String类:extension String

    请参考本博客另一篇技术博文: <[Swift]字符串(String类.NSString类)常用操作> extension String { //获取字符串首字符 var first: St ...

  9. Python2和Python3共存,pip共存

    使用python开发,环境有Python2和 python3 两种,有时候需要两种环境切换使用,下面提供详细教程一份. 1.下载python3和python2 进入python官网,链接https:/ ...

  10. Unity---解决重新调整游戏分辨率后,再运行游戏还是和之前分辨率一样的问题

    经历 上次在Unity做了个小游戏,发布的时候忘了取消默认全屏了. 于是在Unity重新发布了一下,可是出来后分辨率还是默认全屏. 当时百思不得其解 原因 主要是因为当用Unity发布一个新游戏的时候 ...