如果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)
广告