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 通常的观念认为,事务仅与数据库 ...
随机推荐
- Java50道经典习题-程序3 打印水仙花数
题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身.例如:153是一个"水仙花数",因为153=1 ...
- redis详细说明
# By default Redis does not run as a daemon. Use 'yes' if you need it.# Note that Redis will write a ...
- C# 读Autofac源码笔记(1)
最近在看Autofac的源码. Autofac据说是.net中最快的IOC框架,具体没有实验,于是看看Autofac具体是怎样实例化实体. image.png 如上图所示,Autofac使用的是表 ...
- 一文解锁BILIBILI、ACFUN等弹幕网站是如何审核视频
欢迎访问网易云社区,了解更多网易技术产品运营经验. 人工审核对于A站和B站都是很重要的. AcFun稿件审核通行标准(V1.1) 表明,要审核的东西非常多,除了内容本身的合规性.调性,还有画质音质.撞 ...
- Ubuntu16.04中把默认JAVA设置为Oracle的JDK!
系统当中已经存在了OpenJDK,默认的JDK是它,并不是Oracle的JDK,执行下面操作就可以把Oracle的JDK设置为默认的了! 首先假设我们已经把Oracle的JDK安装和配置好了,但是就是 ...
- 2.ECMAScript 5.0
JS的引入方式 内接式 <script type="text/javascript"> </script> 外接式 <!--相当于引入了某个模块--& ...
- 17、xtrabackup 常用备份功能与选项
并行备份 > innobackupex -p123123 --parallel=8 /backup 节流备份 > innobackupex -p123123 --throttle=200 ...
- 【maven】---聚合和继承
前言 自从我知道写maven实战这本书的作者长得随心所欲后,我再拿起这本书真心的不想看前言了.下面分享一下maven中的所谓的聚合和继承. 内容 下文中的子本指的是:多个maven项目. 父本指的是: ...
- 洛谷P5265 【模板】多项式反三角函数
题面 传送门 题解 我数学好像学得太差了 据说根据反三角函数求导公式 \[{d\over dx}\arcsin x={1\over \sqrt{1-x^2}}\] \[{d\over dx}\arct ...
- 洛谷P3613 睡觉困难综合征
传送门 题解 人生第一道由乃…… 做这题之前应该先去把这一题给切掉->这里 我的题解->这里 然后先膜一波zsy大佬和flashhu大佬 大体思路就是先吧全0和全1的都跑答案,然后按位贪心 ...