XA transaction support is limited to the InnoDB storage engine. XA事务只在InnoDB存储引擎中支持
Prior to MySQL 5.7.7, XA transactions were not compatible with replication at all. This was because an XA transaction that was in PREPARED state would be rolled back on clean server shutdown or client disconnect. Similarly, an XA transaction that was in PREPARED state would still exist in PREPARED state in case the server was shutdown abnormally and then started again, but the contents of the transaction could not be written to the binary log. In both of these situations the XA transaction could not be replicated correctly. 官方文档说明,在mysql 5.5.7版本之前,XA也不能完全保证一致性。
mysql单纯的事务是不支持跨库的。真要做的话可以使用XA Transactions
1.确认是否开启XA事务支持
如上,innodb_support_xa为ON 表示已开启,否则需先开启
2.XA事务实现
代码实现参考如下文章,供参考,未测试
https://blog.csdn.net/soonfly...
实际上使用事务会大大的影响性能,如非业务足够特别,一定要使用分布式事务强烈不建议使用,甚至单库的事务也谨慎使用。
通过异步的手段保证事务的最终一致是比较合理的思路。