Deprecated: Function Yoast\WP\SEO\Conditionals\Schema_Blocks_Conditional::get_feature_flag is deprecated since version Yoast SEO 20.5 with no alternative available. in /home1/minerho3/public_html/wp-includes/functions.php on line 6078

Deprecated: Function Yoast\WP\SEO\Conditionals\Schema_Blocks_Conditional::get_feature_flag is deprecated since version Yoast SEO 20.5 with no alternative available. in /home1/minerho3/public_html/wp-includes/functions.php on line 6078

Deprecated: Function Yoast\WP\SEO\Conditionals\Schema_Blocks_Conditional::get_feature_flag is deprecated since version Yoast SEO 20.5 with no alternative available. in /home1/minerho3/public_html/wp-includes/functions.php on line 6078

Warning: Cannot modify header information - headers already sent by (output started at /home1/minerho3/public_html/wp-includes/functions.php:6078) in /home1/minerho3/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/minerho3/public_html/wp-includes/functions.php:6078) in /home1/minerho3/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/minerho3/public_html/wp-includes/functions.php:6078) in /home1/minerho3/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/minerho3/public_html/wp-includes/functions.php:6078) in /home1/minerho3/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/minerho3/public_html/wp-includes/functions.php:6078) in /home1/minerho3/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/minerho3/public_html/wp-includes/functions.php:6078) in /home1/minerho3/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/minerho3/public_html/wp-includes/functions.php:6078) in /home1/minerho3/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/minerho3/public_html/wp-includes/functions.php:6078) in /home1/minerho3/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831
{"id":1142,"date":"2018-03-18T18:27:35","date_gmt":"2018-03-18T18:27:35","guid":{"rendered":"http:\/\/minervadb.com\/?p=1142"},"modified":"2018-03-19T03:39:27","modified_gmt":"2018-03-19T03:39:27","slug":"innodb-deadlocks-explained-with-example","status":"publish","type":"post","link":"https:\/\/minervadb.com\/index.php\/2018\/03\/18\/innodb-deadlocks-explained-with-example\/","title":{"rendered":"InnoDB deadlocks explained with example"},"content":{"rendered":"

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

mysql> 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

Transaction T2<\/strong><\/p>\n

Begins a transaction and attempts to delete the row from the table tab1<\/p>\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

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

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

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

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