问题描述;
研发同事反应MySQL数据库有锁,检查innodb_trx时,发现有很多长时间未结束的空事务。
这些事务的trx_mysql_thread_id都为0,因此不能通过kill id 的方式强制关闭这些长时间未结束的僵尸事务。
SELECT
trx_mysql_thread_id, trx_id, trx_started, sysdate(), trx_state, trx_query
FROM information_schema.innodb_trx t
WHERE (UNIX_TIMESTAMP(sysdate()) - UNIX_TIMESTAMP(t.trx_started)) > 10;
问题分析
从trx_state 列的信息来看,这些事务都在Running 状态,但trx_query 为null。直观上无法分析出这些事务对应的SQL.
在information_schema中的PROCESSLIST, INNODB_LOCKS,INNODB_LOCK_WAITS 这几张表中也没分析出有价值的信息。
通过show engine innodb statusG 确实发现有很多Active的事务
---TRANSACTION 328393988035152, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 16946872173, ACTIVE (PREPARED) 1648933 sec recovered trx
1 lock struct(s), heap size 1136, 0 row lock(s), undo log entries 1
---TRANSACTION 16944705639, ACTIVE (PREPARED) 1708259 sec recovered trx
4 lock struct(s), heap size 1136, 2 row lock(s), undo log entries 1
---TRANSACTION 16944693889, ACTIVE (PREPARED) 1708524 sec recovered trx
2 lock struct(s), heap size 1136, 1 row lock(s), undo log entries 1
---TRANSACTION 16944693748, ACTIVE (PREPARED) 1708549 sec recovered trx
1 lock struct(s), heap size 1136, 0 row lock(s), undo log entries 1
正常的事务不应该执行这么长时间,而不被发现。有没有可能是XA事务呢?
通过xa recover; 检查 , 果然发现很多XA事务。
XA RECOVER;
+----------+--------------+--------------+------------------------------------------------------+
| formatID | gtrid_length | bqual_length | data |
+----------+--------------+--------------+------------------------------------------------------+
| 1 | 32 | 8 | 5685c69b0df34ccc94e9da437c399344xdb1_002 |
| 1 | 32 | 10 | 84e2f7d6fd674f5592f135415e9c0ad4idcent_002 |
| 1 | 32 | 9 | f0eedc6c38894e88837771f019ddc090imail_002 |
| 1 | 32 | 10 | 6677f0102a6c4a0aab2c4696bd20d690idcent_002 |
+----------+--------------+--------------+------------------------------------------------------+
如何释放这些长时间未提交的XA事务呢?
通过MySQL官方文档,最终决定采用rollback的方式,回滚掉这些XA事务。
xa rollback '5685c69b0df34ccc94e9da437c399344','xdb1_002',1;
Query OK, 0 rows affected (0.00 sec)
依次回滚这些长时间未结束的XA事务, 再次检查innodb_trx 未发现相关的事务信息。
---------------------
作者:lwei_998
来源:CSDN
原文:https://blog.csdn.net/lwei_998/article/details/83149231
版权声明:本文为博主原创文章,转载请附上博文链接!