
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
What happens to the current MySQL transaction if the session is killed by DBA?
Suppose if a session is killed 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 killed.
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)
Now we start a new transaction and delete a row from table ‘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)
Before COMMIT or ROLLBACK, open the other window to run another instance of MySQL and run the SHOW PROCESSLIST command as follows −
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)
Now, kill the current transaction by running KILL command as follows −
mysql> KILL 2; Query OK, 0 rows affected (0.00 sec)
Now, when we return back to the current transaction and COMMIT command is executed as follws −
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)
The above KILL command, which killed the current session, forces MySQL to roll back the changes made in the current transaction. It can be observed from the following query that no row has been deleted from 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)
- Related Articles
- What happens to the current MySQL transaction if the session is ended in the middle of a transaction?
- What happens to MySQL temporary tables if MySQL session is ended?
- What will happen to the current MySQL transaction if a START TRANSACTION command is executed in the middle of that current transaction?
- What will happen to MySQL current transaction, if in the middle of that transaction, the DDL statement is executed?
- What happens when we use COMMIT in MySQL stored procedure and one of the transaction, under START transaction, fails?
- How can we check the current MySQL transaction isolation level?
- How can we find out the current transaction mode in MySQL?
- How can a user explicitly end current MySQL transaction?
- How can a user implicitly end current MySQL transaction?
- What is killed to get silk?
- What is the session layer?
- How to save the current JShell session in Java 9?
- What is transaction processing? Explain the properties of the transaction(DBMS)
- What happens if I pass only one argument to the MySQL CONCAT() function?
- How changes, made in the current transaction, can be permanently recorded\nin MySQL database?
