A deadlock happens when transaction holds lock that the another one needs, This usually happens when multiple transactions are waiting for a resource \u00a0to become available, neither ever release the locks it holds. A deadlock can occur when transactions lock rows in multiple tables (through statements such like UPDATE or SELECT … FOR UPDATE) . We have explained dead lock scenario in the example below :<\/p>\n
Transaction T1\u00a0<\/strong><\/p>\n Transaction T1 creates a table “tab1” with one row and begins a transaction. Transaction T1 obtains an S<\/strong> lock on the row by selecting in a shared mode:<\/p>\n Transaction T2<\/strong><\/p>\n Begins a transaction and attempts to delete the row from the table tab1<\/p>\n The delete operation above requires an X (exclusive) lock, This lock cannot be granted because it is not compatible with S (shared) lock that Transaction T1 holds, So the request from Transaction T2 goes on the queue of lock requests for the row and Transaction T2 blocks.<\/p>\n Now Transaction T1 also attempts to delete the same row in the table tab1, Let’s see what happens :<\/p>\n Transaction T1<\/strong><\/p>\n Deadlock happened because Transaction T1 needs an X (exclusive) lock to delete the row, the lock request from Transaction T1 cannot be granted because Transaction T2 already has a request for an X (exclusive) lock and is waiting for Transaction T1 to release its S lock. Nor can the S (shared) lock held by Transaction T1 can be upgraded to an X (exclusive) lock due the prior request by Transaction T2 for an X (exclusive) lock, So InnoDB generated error for one of the transaction and release its lock, The transaction returns the error below<\/p>\n A deadlock happens when transaction holds lock that the another one needs, This usually happens when multiple transactions are waiting for a resource \u00a0to become available, neither ever release the locks it holds. A deadlock […]<\/a><\/p>\n<\/div>","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20,114,2,113],"tags":[23,116,5,115],"yoast_head":"\nmysql> CREATE TABLE tab1 (col1 INT) ENGINE = InnoDB;\r\nQuery OK, 0 rows affected (1.07 sec)\r\n\r\nmysql> INSERT INTO tab1 (col1) VALUES(108);\r\nQuery OK, 1 row affected (0.09 sec)\r\n\r\nmysql> START TRANSACTION;\r\nQuery OK, 0 rows affected (0.00 sec)\r\n\r\nmysql> SELECT * FROM tab1 WHERE col1 = 108 LOCK IN SHARE MODE;\r\n+------+\r\n| col1 |\r\n+------+\r\n| 1 |\r\n+------+<\/pre>\n
mysql> START TRANSACTION;\r\nQuery OK, 0 rows affected (0.00 sec)\r\n\r\nmysql> DELETE FROM tab1 WHERE col1 = 108;<\/pre>\n
mysql> DELETE FROM tab1 WHERE col1 = 108;\r\nERROR 1213 (40001): Deadlock found when trying to get lock;\r\ntry restarting transaction\r\n<\/pre>\n
ERROR 1213 (40001): Deadlock found when trying to get lock;\r\ntry restarting transaction<\/pre>\n
<\/h3>\n
How can we reduce the possibility of deadlocks ?<\/h3>\n
\n