假定在MySQL实例1上有表

create table person(
id int,
name varchar(32)
)

MySQL实例2上也有一张同样的表,现在从实例1中的 person 表中删除一条数据,并把这条数据插入到实例2的表中,这两个操作在同一个事务中,因为跨越了数据库实例,涉及到了分布式事务。

MySQL实现了分布式事务,查看数据库是否启用了 XA 事务:

show variables like 'innodb_support_xa';

MySQL 关于xa的命令:

xa start 'a';
sql 语句;
xa end 'a';
xa prepare 'a';
xa commit 'a';

与正常事务相比,XA 命令多了 prepare,询问是否准备好,事务管理器根据 prepare 返回的结果进行操作。

以上命令是分布式事务的操作方法,在一个命令行中输入上述命令,并不是真实的分布式事务。可以使用 JTA 来控制MySQL的 XA:

public class JTA_MySQL {

    public static void main(String[] args) {
XADataSource xaDs1 = JTA_MySQL.getDataSource(
"jdbc:mysql://172.30.60.126:3306/db_zhang", "root",
"root");
XAConnection xaCon1 = null;
XAResource xaRes1 = null;
Connection conn1 = null;
Statement stmt1 = null; XADataSource xaDs2 = JTA_MySQL.getDataSource(
"jdbc:mysql://172.30.60.124:3306/db_zhang", "root",
"root");
XAConnection xaCon2 = null;
XAResource xaRes2 = null;
Connection conn2 = null;
Statement stmt2 = null; int ret1 = 0;
int ret2 = 0; Xid xid1 = new MyXid(100, new byte[] { 0x01 }, new byte[] { 0x02 });
Xid xid2 = new MyXid(100, new byte[] { 0x01 }, new byte[] { 0x03 });
try {
xaCon1 = getXAConnetion(xaDs1);
conn1 = getConnection(xaCon1);
stmt1 = conn1.createStatement();
xaRes1 = xaCon1.getXAResource(); xaCon2 = getXAConnetion(xaDs2);
conn2 = getConnection(xaCon2);
stmt2 = conn2.createStatement();
xaRes2 = xaCon2.getXAResource(); xaRes1.start(xid1, XAResource.TMNOFLAGS);
stmt1.execute("delete from person where id=1");
xaRes1.end(xid1, XAResource.TMSUCCESS); xaRes2.start(xid2, XAResource.TMNOFLAGS);
stmt2.execute("insert into person select 1, 'zhang'");
xaRes2.end(xid2, XAResource.TMSUCCESS); ret1 = xaRes1.prepare(xid1);
ret2 = xaRes2.prepare(xid2); if (XAResource.XA_OK == ret1 && XAResource.XA_OK == ret2) {
xaRes1.commit(xid1, false);
xaRes2.commit(xid2, false);
System.out.println("提交分布式事务");
} else {
xaRes1.rollback(xid1);
xaRes2.rollback(xid2);
System.out.println("回退分布式事务");
}
} catch (SQLException e) {
e.printStackTrace();
} catch (XAException e) {
e.printStackTrace();
}
} private static XADataSource getDataSource(String url, String user,
String password) {
MysqlXADataSource dataSource = new MysqlXADataSource();
dataSource.setUrl(url);
dataSource.setUser(user);
dataSource.setPassword(password);
return dataSource;
} public static XAConnection getXAConnetion(XADataSource dataSource) {
XAConnection XAConn = null;
try {
XAConn = dataSource.getXAConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return XAConn;
} public static Connection getConnection(XAConnection XAConn) {
Connection conn = null;
try {
conn = XAConn.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
} public static void closeConnection(Connection conn) {
try {
conn.close();
} catch (SQLException e) {
System.out.println("连接关闭失败");
}
}
}

MyXid 类:

public class MyXid implements Xid {
private int formatId;
private byte[] globalTid;
private byte[] branchQ; public MyXid(int formatId, byte[] globalTid, byte[] branchQ) {
this.formatId = formatId;
this.globalTid = globalTid;
this.branchQ = branchQ;
} public byte[] getBranchQualifier() {
return this.branchQ;
} public int getFormatId() {
return formatId;
} public byte[] getGlobalTransactionId() {
return this.globalTid;
}
}

JTA 使用 MySQL 分布式事务的更多相关文章

  1. Mysql分布式事务

    关于Mysql分布式事务介绍,可参考:http://blog.csdn.net/luckyjiuyi/article/details/46955337 分为两个阶段:准备和执行阶段.有两个角色:事务的 ...

  2. mysql 分布式事务

    php + mysql 分布式事务 事务(Transaction)是访问并可能更新数据库中各种数据项的一个程序执行单元: 事务应该具有4个属性:原子性.一致性.隔离性.持续性 原子性(atomicit ...

  3. 详解Mysql分布式事务XA(跨数据库事务)

    详解Mysql分布式事务XA(跨数据库事务) 学习了:http://blog.csdn.net/soonfly/article/details/70677138 mysql执行XA事物的时候,mysq ...

  4. Spring+JTA+Atomikos+mybatis分布式事务管理

    我们平时的工作中用到的Spring事务管理是管理一个数据源的.但是如果对多个数据源进行事务管理该怎么办呢?我们可以用JTA和Atomikos结合Spring来实现一个分布式事务管理的功能.了解JTA可 ...

  5. php + mysql 分布式事务(转)

    事务(Transaction)是访问并可能更新数据库中各种数据项的一个程序执行单元: 事务应该具有4个属性:原子性.一致性.隔离性.持续性 原子性(atomicity).一个事务是一个不可分割的工作单 ...

  6. php + mysql 分布式事务

    事务(Transaction)是访问并可能更新数据库中各种数据项的一个程序执行单元: 事务应该具有4个属性:原子性.一致性.隔离性.持续性 原子性(atomicity).一个事务是一个不可分割的工作单 ...

  7. 了解一下Mysql分布式事务及优缺点、使用案例(php+mysql)

    在开发中,为了降低单点压力,通常会根据业务情况进行分表分库,将表分布在不同的库中(库可能分布在不同的机器上),但是一个业务场景可能会同时处理两个表的操作.在这种场景下,事务的提交会变得相对复杂,因为多 ...

  8. 分布式事务(三)mysql对XA协议的支持

    系列目录 分布式事务(一)原理概览 分布式事务(二)JTA规范 分布式事务(三)mysql对XA协议的支持 分布式事务(四)简单样例 分布式事务(五)源码详解 分布式事务(六)总结提高 引子 从Mys ...

  9. 【分布式事务】使用atomikos+jta解决分布式事务问题

    一.前言 分布式事务,这个问题困惑了小编很久,在3个月之前,就间断性的研究分布式事务.从MQ方面,数据库事务方面,jta方面.近期终于成功了,使用JTA解决了分布式事务问题.先写一下心得,后面的二级提 ...

随机推荐

  1. Python 安装 lxml 插件

    1.下载 lxml 地址:https://pypi.python.org/pypi/lxml/3.8.0#downloads 我用的是python 3.6,我下载了  lxml-3.8.0-cp36- ...

  2. 清除memcached缓存

    telnet localhost 11211 flush_all 最后要一定要关闭dos窗体,不然会导致memcached写值返回ture,但是实际上并没有写入值

  3. VMWare16安装windows7遇到的一些问题

    本人写这篇博客是为了记录了一些自己在使用VMware16安装Windows7时遇到的一些问题.本人使用的Windows7 ios镜像是小于4g的镜像. Windows7 ios的镜像地址为:https ...

  4. Matlab中的基本数据类型介绍

    Matlab中支持的数据类型包括: 逻辑(logical)字符(char)数值(numeric)元胞数组(cell)结构体(structure)表格(table)函数句柄(function handl ...

  5. 使用python读取yaml文件

    在做APP测试时,通常需要把参数存到一个字典变量中,这时可以将参数写入yaml文件中,再读取出来. 新建yaml文件(android_caps.yaml),文件内容为: platformName: A ...

  6. linux网络常用命令

    1,显示网桥 brctl show2,显示ip ip a3,查看openvswitch的配置信息 ovs-vsctl show4,显示网络命名空间 ip netns5,显示DHCP信息 ps -ef ...

  7. Go语言学习之1 基本概念、环境搭建、第一个Go程序

    一.环境搭建 见我的这篇博客 https://www.cnblogs.com/xuejiale/p/10258244.html 二.golang语言特性1. 垃圾回收    1) 内存自动回收,再也不 ...

  8. centos7: 将nginx,php-fpm加入开机启动

    1. 自己新建一个脚本,如centnet-service.sh 经过后面的几个步骤后,这个脚本在开机的时候会执行,在这个脚本里面可以写你开机的时候想执行的命令,如启动nginx,phpf-pm等服务 ...

  9. 连接PL/SQL

    1.登录PL/SQL Developer 这里省略Oracle数据库和PL/SQL Developer的安装步骤,注意在安装PL/SQL Developer软件时,不要安装在Program Files ...

  10. c++-pimer-plus-6th-chapter03

    Chapter Review Having more than one integer type lets you choose the type that is best suited to a p ...