What happens to the current MySQL transaction if the session is ended in the middle of a transaction?


Suppose if a session is ended in the middle of a transaction then that current MySQL transaction will be rolled back by MySQL and ended. It means that all the database changes made in the current transaction will be removed. It is called n implicit rollback when the session is ended.

Example

Suppose we have the following values in the table ‘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)

mysql> START TRANSACTION;
Query OK, 0 rows affected (0.00 sec)

mysql> UPDATE marks SET Name = ‘Yash’ Where id = 5;
Query OK, 1 row affected (0.06 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> quit;
Bye

In the example above, after updating the value in the table, the session is ended by running quit statement. When we check the table after starting the session again, the updated value has been rolled back by MySQL because the session is ended in the middle of a transaction.

Updated on: 22-Jun-2020

88 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements