In Beginners' Understanding MySQL Deadlock Problems from Source Code, we introduced the process of debugging MySQL source code to view deadlocks. This article will talk about a common case. Let's look at a simplified example # Construct data CREATE TABLE `t1` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(10), `level` int(11), PRIMARY KEY (`id`), UNIQUE KEY `uk_name` (`name`) ); INSERT INTO `t1` (`name`, `level`) VALUES ('A',0); # The problematic SQL statement is as follows, which will cause a deadlock in concurrent situations INSERT ignore INTO `t1` (`name`, `level`) VALUES ('A',0); update t1 set level = 1 where name = "A"; We use the source code analysis method introduced before to first look at what locks are added to these two statements, and then analyze the process of deadlock formation. First statement INSERT ignore INTO t1 (name, level) VALUES ('A',0); The results obtained during debugging are as follows You can see that this statement adds a shared lock (S lock) to the unique key uk_name and succeeds. The second statement update t1 set level = 1 where name = "A"; Updates a database field by a unique key. This situation has been introduced in the previous article. X lock will be added to the unique index, and then X lock will be added to the primary key index. In this way, the deadlock problem can be reproduced very easily. The steps are as follows 1. Open two sessions, begin The detailed lock status changes are as follows
The deadlock log is as follows: LATEST DETECTED DEADLOCK ------------------------ 181208 23:00:52 *** (1) TRANSACTION: TRANSACTION 53A7, ACTIVE 162 sec starting index read mysql tables in use 1, locked 1 LOCK WAIT 3 lock struct(s), heap size 376, 2 row lock(s) MySQL thread id 12, OS thread handle 0x700010522000, query id 1424 localhost root Updating update t1 set level = 1 where name = "A" *** (1) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 89 page no 4 n bits 72 index `uk_name` of table `lock_demo2`.`t1` trx id 53A7 lock_mode X locks rec but not gap waiting Record lock, heap no 2 PHYSICAL RECORD: n_fields 2; compact format; info bits 0 0: len 1; hex 41; asc A;; 1: len 4; hex 80000001; asc ;; *** (2) TRANSACTION: TRANSACTION 53A8, ACTIVE 8 sec starting index read mysql tables in use 1, locked 1 3 lock struct(s), heap size 376, 2 row lock(s) MySQL thread id 96, OS thread handle 0x70001062e000, query id 1425 localhost root Updating update t1 set level = 1 where name = "A" *** (2) HOLDS THE LOCK(S): RECORD LOCKS space id 89 page no 4 n bits 72 index `uk_name` of table `lock_demo2`.`t1` trx id 53A8 lock mode S Record lock, heap no 2 PHYSICAL RECORD: n_fields 2; compact format; info bits 0 0: len 1; hex 41; asc A;; 1: len 4; hex 80000001; asc ;; *** (2) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 89 page no 4 n bits 72 index `uk_name` of table `lock_demo2`.`t1` trx id 53A8 lock_mode X locks rec but not gap waiting Record lock, heap no 2 PHYSICAL RECORD: n_fields 2; compact format; info bits 0 0: len 1; hex 41; asc A;; 1: len 4; hex 80000001; asc ;; *** WE ROLL BACK TRANSACTION (2) Let's take a closer look at this deadlock log *** (1) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 89 page no 4 n bits 72 index uk_name of table lock_demo2.t1 trx id 53A7 lock_mode X locks rec but not gap waiting Transaction 1 wants to obtain an X lock (non-gap lock record lock) on the unique index uk_name *** (2) HOLDS THE LOCK(S): RECORD LOCKS space id 89 page no 4 n bits 72 index uk_name of table lock_demo2.t1 trx id 53A8 lock mode S Transaction 2 holds an S lock (shared lock) on the unique index uk_name *** (2) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 89 page no 4 n bits 72 index uk_name of table lock_demo2.t1 trx id 53A8 lock_mode X locks rec but not gap waiting Transaction 2 wants to obtain an X lock (non-gap lock record lock) on the uk_name unique index. The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: How to use JavaScript to get the most repeated characters in a string
>>: Detailed explanation of the basic usage of the auxiliary function mapGetters in vuex
Table of contents 1. Project Environment 2. Proje...
I'm using a placeholder in a text input and i...
Copy code The code is as follows: <!DOCTYPE ht...
MySQL executes SQL through the process of SQL par...
Recently, when I installed MySQL in Docker, I fou...
1. Idea It only took 6 seconds to insert 1,000,00...
mysql5.6.28 installation and configuration method...
Preface In many MySQL test scenarios, some test d...
MySQL 8.0.13 has a data folder by default. This f...
Table of contents 1. Use SVG 2. Use fontAwesome 3...
If you are not committed to becoming an artist, t...
Regarding display: flex layout, some people have ...
mysql installer community 8.0.16.0 installation g...
1. Dynamic query rules The dynamic query rules ar...
This article is an integrated article on how to c...