如果DBA杀死了会话,当前的MySQL事务会发生什么?
假设在事务中间杀死了会话,那么MySQL将回滚当前的MySQL事务并结束。这意味着当前事务中所做的所有数据库更改都将被删除。当会话被杀死时,这被称为隐式回滚。
示例
假设表“marks”中有以下值
mysql> Select * from marks; +------+---------+-----------+-------+ | Id | Name | Subject | Marks | +------+---------+-----------+-------+ | 1 | Aarav | Maths | 50 | | 1 | Harshit | Maths | 55 | | 3 | Gaurav | Comp | 69 | | 4 | Rahul | History | 40 | | 5 | Yashraj | English | 48 | | 6 | Manak | History | 70 | +------+---------+---------+---------+ 6 rows in set (0.00 sec)
现在我们开始一个新的事务,并从表“marks”中删除一行
mysql> START TRANSACTION; Query OK, 0 rows affected (0.00 sec) mysql> Delete from marks where id = 4; Query OK, 1 row affected (0.00 sec)
在COMMIT或ROLLBACK之前,打开另一个窗口运行另一个MySQL实例,并运行以下SHOW PROCESSLIST命令:
mysql> SHOW PROCESSLIST\G *************************** 1. row *************************** Id: 2 User: root Host: localhost:49303 db: query Command: Sleep Time: 22 State: Info: NULL *************************** 2. row *************************** Id: 3 User: root Host: localhost:49350 db: NULL Command: Query Time: 0 State: NULL Info: show processlist 2 rows in set (0.00 sec)
现在,通过运行以下KILL命令杀死当前事务:
mysql> KILL 2; Query OK, 0 rows affected (0.00 sec)
现在,当我们返回到当前事务并执行以下COMMIT命令时:
mysql> COMMIT; ERROR 2006 (HY000): MySQL server has gone away No connection. Trying to reconnect... Connection id: 4 Current database: query Query OK, 0 rows affected (1.01 sec)
上述KILL命令杀死了当前会话,迫使MySQL回滚当前事务中所做的更改。从以下查询可以观察到,表“marks”中没有删除任何行。
mysql> Select * from marks; +------+---------+-----------+-------+ | Id | Name | Subject | Marks | +------+---------+-----------+-------+ | 1 | Aarav | Maths | 50 | | 1 | Harshit | Maths | 55 | | 3 | Gaurav | Comp | 69 | | 4 | Rahul | History | 40 | | 5 | Yashraj | English | 48 | | 6 | Manak | History | 70 | +------+---------+---------+---------+ 6 rows in set (0.00 sec)
广告