这个文章是我在自己的个人微信公众号“数据库开发者” 里面首发的,现在在我的博客里面再发一遍。欢迎您扫码
关注“数据库开发者” 公众号。
MariaDB与MySQL对比 --- 对分布式事务的支持
MariaDB与MySQL对比 --- 对分布式事务的支持
MariaDB和MySQL两者对分布式事务的支持有所不同,总的来说MySQL的支持更好,是完备的和可靠的(也发现了一个bug),而MariaDB的支持还有诸多问题,先举例说明。
本文测试用例使用MySQL-5.7.16和MariaDB-10.1.9 进行测试。
MySQL/MariaDB对分布式事务的支持是根据 X/Open CAE document Distributed Transaction Processing: The XA Specification (http://www.opengroup.org/public/pubs/catalog/c193.htm) ,主要包括下面这几个语句:
xa start 'gtid';
xa end 'gtid';
xa prepare 'gtid';
xa commit 'gtid';
xa rollback 'gtid';
xa recover;
外部的分布式事务管理器(transaction manager, TM) 可以使用这套XA命令来操作mysql 数据库,按照XA标准的两阶段提交算法(2PC) 完成分布式事务提交。各个语句的意义见官方文档。
其中mariadb的问题在于 xa prepare 'gtid' 之后,这个事务分支(transaction branch) 可能会丢失。详见下文。
事先创建1个简单的表并且插入4行数据:
create table t1(a int primary key);
insert into t1 values(1),(2),(3),(4);
一。两者的公共行为(相同点)
本节的查询是要说明,xa prepare之前(无论是xa end 之前还是之后),xa事务的改动对外部不可见,也不持久化,退出连接就会丢失修改。xa事务信息本身也会在退出连接后消失,重新连接后xa recover不会显示它。
这些行为mysql与mariadb相同,都是正确的。
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> xa start '124';
Query OK, 0 rows affected (0.00 sec)
mysql> insert into t1 values(5);
Query OK, 1 row affected (0.00 sec)
mysql> insert into t1 values(6);
Query OK, 1 row affected (0.00 sec)
mysql> quit;
Bye
david@david-VirtualBox:~/mysql_installs/mysql-5.7.16$ ./bin/mysql -S ../../mysql-instances/a/mysql.sock
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.16-debug-log Source distribution
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the curr