XA 事务

支持项

  • 支持数据分片后的跨库事务;
  • 两阶段提交保证操作的原子性和数据的强一致性;
  • 服务宕机重启后,提交/回滚中的事务可自动恢复;
  • 支持同时使用 XA 和非 XA 的连接池。

不支持项

  • 服务宕机后,在其它机器上恢复提交/回滚中的数据。

通过 XA 语句控制的分布式事务

  • 通过 XA START 可以手动开启 XA 事务,注意该事务完全由用户管理,ShardingSphere 只负责将语句转发至后端数据库;
  • 服务宕机后,需要通过 XA RECOVER 获取未提交或回滚的事务,也可以在 COMMIT 时使用 ONE PHASE 跳过 PERPARE。
  1. MySQL [(none)]> use test1 MySQL [(none)]> use test2
  2. Reading table information for completion of table and column names Reading table information for completion of table and column names
  3. You can turn off this feature to get a quicker startup with -A You can turn off this feature to get a quicker startup with -A
  4. Database changed Database changed
  5. MySQL [test1]> XA START '61c052438d3eb'; MySQL [test2]> XA START '61c0524390927';
  6. Query OK, 0 rows affected (0.030 sec) Query OK, 0 rows affected (0.009 sec)
  7. MySQL [test1]> update test set val = 'xatest1' where id = 1; MySQL [test2]> update test set val = 'xatest2' where id = 1;
  8. Query OK, 1 row affected (0.077 sec) Query OK, 1 row affected (0.010 sec)
  9. MySQL [test1]> XA END '61c052438d3eb'; MySQL [test2]> XA END '61c0524390927';
  10. Query OK, 0 rows affected (0.006 sec) Query OK, 0 rows affected (0.008 sec)
  11. MySQL [test1]> XA PREPARE '61c052438d3eb'; MySQL [test2]> XA PREPARE '61c0524390927';
  12. Query OK, 0 rows affected (0.018 sec) Query OK, 0 rows affected (0.011 sec)
  13. MySQL [test1]> XA COMMIT '61c052438d3eb'; MySQL [test2]> XA COMMIT '61c0524390927';
  14. Query OK, 0 rows affected (0.011 sec) Query OK, 0 rows affected (0.018 sec)
  15. MySQL [test1]> select * from test where id = 1; MySQL [test2]> select * from test where id = 1;
  16. +----+---------+ │+----+---------+
  17. | id | val | │| id | val |
  18. +----+---------+ │+----+---------+
  19. | 1 | xatest1 | │| 1 | xatest2 |
  20. +----+---------+ │+----+---------+
  21. 1 row in set (0.016 sec) 1 row in set (0.129 sec)
  22. MySQL [test1]> XA START '61c05243994c3'; MySQL [test2]> XA START '61c052439bd7b';
  23. Query OK, 0 rows affected (0.047 sec) Query OK, 0 rows affected (0.006 sec)
  24. MySQL [test1]> update test set val = 'xarollback' where id = 1; MySQL [test2]> update test set val = 'xarollback' where id = 1;
  25. Query OK, 1 row affected (0.175 sec) Query OK, 1 row affected (0.008 sec)
  26. MySQL [test1]> XA END '61c05243994c3'; MySQL [test2]> XA END '61c052439bd7b';
  27. Query OK, 0 rows affected (0.007 sec) Query OK, 0 rows affected (0.014 sec)
  28. MySQL [test1]> XA PREPARE '61c05243994c3'; MySQL [test2]> XA PREPARE '61c052439bd7b';
  29. Query OK, 0 rows affected (0.013 sec) Query OK, 0 rows affected (0.019 sec)
  30. MySQL [test1]> XA ROLLBACK '61c05243994c3'; MySQL [test2]> XA ROLLBACK '61c052439bd7b';
  31. Query OK, 0 rows affected (0.010 sec) Query OK, 0 rows affected (0.010 sec)
  32. MySQL [test1]> select * from test where id = 1; MySQL [test2]> select * from test where id = 1;
  33. +----+---------+ │+----+---------+
  34. | id | val | │| id | val |
  35. +----+---------+ │+----+---------+
  36. | 1 | xatest1 | │| 1 | xatest2 |
  37. +----+---------+ │+----+---------+
  38. 1 row in set (0.009 sec) 1 row in set (0.083 sec)
  39. MySQL [test1]> XA START '61c052438d3eb';
  40. Query OK, 0 rows affected (0.030 sec)
  41. MySQL [test1]> update test set val = 'recover' where id = 1;
  42. Query OK, 1 row affected (0.072 sec)
  43. MySQL [test1]> select * from test where id = 1;
  44. +----+---------+
  45. | id | val |
  46. +----+---------+
  47. | 1 | recover |
  48. +----+---------+
  49. 1 row in set (0.039 sec)
  50. MySQL [test1]> XA END '61c052438d3eb';
  51. Query OK, 0 rows affected (0.005 sec)
  52. MySQL [test1]> XA PREPARE '61c052438d3eb';
  53. Query OK, 0 rows affected (0.020 sec)
  54. MySQL [test1]> XA RECOVER;
  55. +----------+--------------+--------------+---------------+
  56. | formatID | gtrid_length | bqual_length | data |
  57. +----------+--------------+--------------+---------------+
  58. | 1 | 13 | 0 | 61c052438d3eb |
  59. +----------+--------------+--------------+---------------+
  60. 1 row in set (0.010 sec)
  61. MySQL [test1]> XA RECOVER CONVERT XID;
  62. +----------+--------------+--------------+------------------------------+
  63. | formatID | gtrid_length | bqual_length | data |
  64. +----------+--------------+--------------+------------------------------+
  65. | 1 | 13 | 0 | 0x36316330353234333864336562 |
  66. +----------+--------------+--------------+------------------------------+
  67. 1 row in set (0.011 sec)
  68. MySQL [test1]> XA COMMIT 0x36316330353234333864336562;
  69. Query OK, 0 rows affected (0.029 sec)
  70. MySQL [test1]> XA RECOVER;
  71. Empty set (0.011 sec)