如果会话被DBA终止,当前的MySQL事务会怎样?
假设如果会话在事务中间被杀死,那么当前的MySQL事务将被MySQL回滚并结束。这意味着将删除在当前事务中进行的所有数据库更改。会话被杀死时,它称为n隐式回滚。
示例
假设我们在“标记”表中具有以下值
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; mysql> Delete from marks where id = 4;
在COMMIT或ROLLBACK之前,打开另一个窗口以运行另一个MySQL实例并运行SHOWPROCESSLIST命令,如下所示-
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;
现在,当我们返回到当前事务并以下列方式执行COMMIT命令时:
mysql> COMMIT; ERROR 2006 (HY000): MySQL server has gone away No connection. Trying to reconnect... Connection id: 4 Current database: query
上面的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)