Mycat的使用 - 04.事务支持
admin
2023-05-02 03:41:52
0

03全局序列号篇详细介绍了分片表, 业务在使用分片表时, 很自然的可能会遇到一个事务中操作的数据分布在多个分片节点上, 即分布式事务. 先来直观感受下Mycat处理事务的过程.


登陆tb3表的dnTest2节点主机, 操作如下.

mysql> set global innodb_lock_wait_timeout = 5;

Query OK, 0 rows affected (0.00 sec)


mysql> start transaction;

Query OK, 0 rows affected (0.00 sec)


mysql> select user_name from tb3 where user_id = 59 for update;

+-----------+

| user_name |

+-----------+

| mnop_f    |

+-----------+

1 row in set (0.00 sec)


登陆Mycat, 开启一个事务, 结合日志看下该过程.

mysql> start transaction;

Query OK, 0 rows affected (0.01 sec)


mysql> update tb3 set user_name = 'igkl_2f' where user_id = 4;

Query OK, 1 row affected (0.01 sec)

Rows matched: 1  Changed: 1  Warnings: 0


mysql> update tb3 set user_name = 'mnop_2f' where user_id = 59;

ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction

mysql> commit;

ERROR 1003 (HY000): Transaction error, need to rollback.

mysql> rollback;

Query OK, 0 rows affected (0.00 sec)


关键日志如下.

03/27 12:07:09.189  DEBUG [$_NIOREACTOR-1-RW] (ServerQueryHandler.java:56) -ServerConnection [id=2, schema=testdb, host=192.168.4.184, user=test_user,txIsolation=3, autocommit=true, schema=testdb]begin

03/27 12:16:33.019  DEBUG [$_NIOREACTOR-2-RW] (ServerQueryHandler.java:56) -ServerConnection [id=6, schema=testdb, host=192.168.4.184, user=test_user,txIsolation=3, autocommit=false, schema=testdb]update tb3 set user_name = 'igkl_2f' where user_id = 4

03/27 12:16:33.021  DEBUG [$_NIOREACTOR-2-RW] (NonBlockingSession.java:113) -ServerConnection [id=6, schema=testdb, host=192.168.4.184, user=test_user,txIsolation=3, autocommit=false, schema=testdb]update tb3 set user_name = 'igkl_2f' where user_id = 4, route={

   1 -> dnTest1{update tb3 set user_name = 'igkl_2f' where user_id = 4}

03/27 12:16:33.021  DEBUG [$_NIOREACTOR-2-RW] (MySQLConnection.java:459) -con need syn ,total syn cmd 3 commands SET names utf8;SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;SET autocommit=0;schema change:false con:MySQLConnection [id=8, lastTime=1522124193021, user=appacc, schema=test1, old shema=test1, borrowed=true, fromSlaveDB=false, threadId=28, charset=utf8, txIsolation=0, autocommit=true, attachment=dnTest1{update tb3 set user_name = 'igkl_2f' where user_id = 4}, respHandler=SingleNodeHandler [node=dnTest1{update tb3 set user_name = 'igkl_2f' where user_id = 4}, packetId=0], host=192.168.4.235, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=true]

03/27 12:16:52.795  DEBUG [$_NIOREACTOR-2-RW] (ServerQueryHandler.java:56) -ServerConnection [id=6, schema=testdb, host=192.168.4.184, user=test_user,txIsolation=3, autocommit=false, schema=testdb]update tb3 set user_name = 'mnop_2f' where user_id = 59

03/27 12:16:52.796  DEBUG [$_NIOREACTOR-2-RW] (NonBlockingSession.java:113) -ServerConnection [id=6, schema=testdb, host=192.168.4.184, user=test_user,txIsolation=3, autocommit=false, schema=testdb]update tb3 set user_name = 'mnop_2f' where user_id = 59, route={

   1 -> dnTest2{update tb3 set user_name = 'mnop_2f' where user_id = 59}

03/27 12:16:52.797  DEBUG [$_NIOREACTOR-2-RW] (MySQLConnection.java:459) -con need syn ,total syn cmd 3 commands SET names utf8;SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;SET autocommit=0;schema change:false con:MySQLConnection [id=20, lastTime=1522124212797, user=appacc, schema=test2, old shema=test2, borrowed=true, fromSlaveDB=false, threadId=8, charset=utf8, txIsolation=0, autocommit=true, attachment=dnTest2{update tb3 set user_name = 'mnop_2f' where user_id = 59}, respHandler=SingleNodeHandler [node=dnTest2{update tb3 set user_name = 'mnop_2f' where user_id = 59}, packetId=0], host=192.168.4.151, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=true]

03/27 12:16:58.800   WARN [$_NIOREACTOR-1-RW] (SingleNodeHandler.java:232) -execute  sql err : errno:1205 Lock wait timeout exceeded; try restarting transaction con:MySQLConnection [id=20, lastTime=1522124212784, user=appacc, schema=test2, old shema=test2, borrowed=true, fromSlaveDB=false, threadId=8, charset=utf8, txIsolation=3, autocommit=false, attachment=dnTest2{update tb3 set user_name = 'mnop_2f' where user_id = 59}, respHandler=SingleNodeHandler [node=dnTest2{update tb3 set user_name = 'mnop_2f' where user_id = 59}, packetId=1], host=192.168.4.151, port=3306, statusSync=org.opencloudb.mysql.nio.MySQLConnection$StatusSync@52530466, writeQueue=0, modifiedSQLExecuted=true] frontend host:192.168.4.184/59858/test_user

03/27 12:17:05.660  DEBUG [$_NIOREACTOR-2-RW] (ServerQueryHandler.java:56) -ServerConnection [id=6, schema=testdb, host=192.168.4.184, user=test_user,txIsolation=3, autocommit=false, schema=testdb]commit

03/27 12:17:08.868  DEBUG [$_NIOREACTOR-2-RW] (ServerQueryHandler.java:56) -ServerConnection [id=6, schema=testdb, host=192.168.4.184, user=test_user,txIsolation=3, autocommit=false, schema=testdb]rollback

03/27 12:17:08.870  DEBUG [$_NIOREACTOR-2-RW] (RollbackNodeHandler.java:79) -rollback job run for MySQLConnection [id=20, lastTime=1522124212784, user=appacc, schema=test2, old shema=test2, borrowed=true, fromSlaveDB=false, threadId=8, charset=utf8, txIsolation=3, autocommit=false, attachment=dnTest2{update tb3 set user_name = 'mnop_2f' where user_id = 59}, respHandler=SingleNodeHandler [node=dnTest2{update tb3 set user_name = 'mnop_2f' where user_id = 59}, packetId=1], host=192.168.4.151, port=3306, statusSync=org.opencloudb.mysql.nio.MySQLConnection$StatusSync@52530466, writeQueue=0, modifiedSQLExecuted=true]

03/27 12:17:08.870  DEBUG [$_NIOREACTOR-2-RW] (RollbackNodeHandler.java:79) -rollback job run for MySQLConnection [id=8, lastTime=1522124193010, user=appacc, schema=test1, old shema=test1, borrowed=true, fromSlaveDB=false, threadId=28, charset=utf8, txIsolation=3, autocommit=false, attachment=dnTest1{update tb3 set user_name = 'igkl_2f' where user_id = 4}, respHandler=SingleNodeHandler [node=dnTest1{update tb3 set user_name = 'igkl_2f' where user_id = 4}, packetId=1], host=192.168.4.235, port=3306, statusSync=null, writeQueue=0, modifiedSQLExecuted=true]


Mycat目前对于未分片的节点, 是可以保证事务的完整性的; 若是多个分片节点, 在执行事务时, 遇到任何分片出错, 也是能保证所有分片回滚的, 即上边展示的情况. 可是应用一旦进入commit过程, 若此时出现问题, 其就无能为力了, 这也是Mycat称之为弱XA的原因.


上述commit过程是指: InnoDB prepare, write/sync Binlog, InnoDB commit(其历经5.5至5.7版本的多次迭代优化, 这块内容也是精彩纷呈). 虽该阶段一般不会出现问题, 这也正暗示了Mycat在特殊情况下还不能保证分布式事务安全. 那应用架构中又如何实现可靠的分布式事务呢, 这又是另一个宏大的话题了...


若感兴趣可关注订阅号”数据库最佳实践”(DBBestPractice).

Mycat的使用 - 04.事务支持

相关内容

热门资讯

商界巨头、政府高官、儿子儿媳:... 澎湃新闻记者 朱郑勇 实习生 蒋嘉和美国总统特朗普即将于今天晚些时候抵达中国。据中国新闻网13日报道...
日本零食厂商因石脑油匮乏部分停... 【环球网报道】据日本共同社5月13日报道,日本食品生产公司“野村煎豆加工店”当天接受采访时表示,由于...
郑丽文:特朗普若反对“台独”,... 美国总统特朗普将于5月13日至15日访华,台湾问题是主要议题之一。中国国民党主席郑丽文称,特朗普若表...
特朗普要求中国对美经贸团队访问... 澎湃新闻记者 杨文钦 朱郑勇5月13日,外交部发言人郭嘉昆主持例行记者会。法新社记者提问,美国总统特...
美媒又想起这茬:2年前在地中海... 【文/观察者网 阮佳琪】2024年12月23日,载有16名船员的俄罗斯“大熊星座”号货船在西班牙近海...
App过度索取授权或被境外间谍... 微信公众号“国家安全部”5月13日发文: 手机里各种各样的应用程序(APP)五花八门,在方便我们生...
广合科技获得发明专利授权:“一... 证券之星消息,根据天眼查APP数据显示广合科技(001389)新获得一项发明专利授权,专利名为“一种...
华尔街科技老将:大科技公司分化... 5月11日,互联网泡沫时期的知名芯片分析师、Niles Investment Management创...
香港80后“地产女王”烧炭身亡... 据《香港01》报道,5月12日,香港九龙传统豪宅地段加多利山畔的豪宅项目Kadoorie Hill发...
谷歌发布安卓 AI 系统,这就... 和去年一样,在正式的 Google I/O 开发者大会之前,谷歌为 Android 单独开了一次小型...