死鎖: delete+insert不存在PK
###### 表結構及資料
CREATE TABLE `tb` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`a` int(11) ,
`b` int(11) ,
`c` int(11) ,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
/*Data for the table `tb` */
insert into `tb`(`id`,`a`,`b`,`c`) values (1,1,0,1);
insert into `tb`(`id`,`a`,`b`,`c`) values (2,2,1,2);
insert into `tb`(`id`,`a`,`b`,`c`) values (3,3,2,3);
insert into `tb`(`id`,`a`,`b`,`c`) values (4,4,2,4);
insert into `tb`(`id`,`a`,`b`,`c`) values (5,5,3,5);
insert into `tb`(`id`,`a`,`b`,`c`) values (6,6,3,6);
insert into `tb`(`id`,`a`,`b`,`c`) values (7,7,4,7);
insert into `tb`(`id`,`a`,`b`,`c`) values (8,8,5,8);
insert into `tb`(`id`,`a`,`b`,`c`) values (10,10,6,10);
###### 操作流程
事務1
START TRANSACTION;
DELETE FROM tb WHERE id=9 AND b=2;
------- 持有GAP(8,10)鎖
事務2
START TRANSACTION;
DELETE FROM tb WHERE id=9 AND b=2;
------- 持有GAP(8,10)鎖
事務1
INSERT INTO `tb` (`id`,`a`, `b`,`c`) VALUES (9,1,2,0);
------- 阻塞等待鎖,TRX HAS BEEN WAITING 22 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 0 page no 67344 n bits 104 index `PRIMARY` of table `test`.`tb` trx id 1169A0 lock_mode X locks gap before rec insert intention waiting
------- INNODB_LOCKS 表資料
lock_id lock_trx_id lock_mode lock_type lock_table lock_index lock_space lock_page lock_rec lock_data
1169A0:0:67344:27 1169A0 X,GAP RECORD `test`.`tb` `PRIMARY` 0 67344 27 10
11699F:0:67344:27 11699F X,GAP RECORD `test`.`tb` `PRIMARY` 0 67344 27 10
事務2
INSERT INTO `tb` (`id`,`a`, `b`,`c`) VALUES (9,1,2,0);
------- 發生死鎖,都在等待同一個GAP
###### 死鎖資訊
------------------------
LATEST DETECTED DEADLOCK
------------------------
150318 17:19:31
*** (1) TRANSACTION:
TRANSACTION 1169A0, ACTIVE 34 sec inserting
mysql tables in use 1, locked 1
LOCK WAIT 3 lock struct(s), heap size 376, 2 row lock(s)
MySQL thread id 185, OS thread handle 0x164c, query id 1884365 localhost 127.0.0.1 root update
INSERT INTO `tb` (`id`,`a`, `b`,`c`) VALUES (9,1,2,0)
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 0 page no 67344 n bits 104 index `PRIMARY` of table `test`.`tb` trx id 1169A0 lock_mode X locks gap before rec insert intention waiting
Record lock, heap no 27 PHYSICAL RECORD: n_fields 6; compact format; info bits 0
0: len 4; hex 8000000a; asc ;;
1: len 6; hex 000000038a02; asc ;;
2: len 7; hex 3c00010702031e; asc < ;;
3: len 4; hex 8000000a; asc ;;
4: len 4; hex 80000006; asc ;;
5: len 4; hex 8000000a; asc ;;
*** (2) TRANSACTION:
TRANSACTION 11699F, ACTIVE 39 sec inserting, thread declared inside InnoDB 500
mysql tables in use 1, locked 1
3 lock struct(s), heap size 376, 2 row lock(s)
MySQL thread id 187, OS thread handle 0x2a88, query id 1884373 localhost 127.0.0.1 root update
INSERT INTO `tb` (`id`,`a`, `b`,`c`) VALUES (9,1,2,0)
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 0 page no 67344 n bits 104 index `PRIMARY` of table `test`.`tb` trx id 11699F lock_mode X locks gap before rec
Record lock, heap no 27 PHYSICAL RECORD: n_fields 6; compact format; info bits 0
0: len 4; hex 8000000a; asc ;;
1: len 6; hex 000000038a02; asc ;;
2: len 7; hex 3c00010702031e; asc < ;;
3: len 4; hex 8000000a; asc ;;
4: len 4; hex 80000006; asc ;;
5: len 4; hex 8000000a; asc ;;
*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 0 page no 67344 n bits 104 index `PRIMARY` of table `test`.`tb` trx id 11699F lock_mode X locks gap before rec insert intention waiting
Record lock, heap no 27 PHYSICAL RECORD: n_fields 6; compact format; info bits 0
0: len 4; hex 8000000a; asc ;;
1: len 6; hex 000000038a02; asc ;;
2: len 7; hex 3c00010702031e; asc < ;;
3: len 4; hex 8000000a; asc ;;
4: len 4; hex 80000006; asc ;;
5: len 4; hex 8000000a; asc ;;
*** WE ROLL BACK TRANSACTION (2)
------------
TRANSACTIONS
------------
---TRANSACTION 1169A0, ACTIVE 24 sec inserting
mysql tables in use 1, locked 1
LOCK WAIT 3 lock struct(s), heap size 376, 2 row lock(s)
MySQL thread id 185, OS thread handle 0x164c, query id 1884365 localhost 127.0.0.1 root update
INSERT INTO `tb` (`id`,`a`, `b`,`c`) VALUES (9,1,2,0)
------- TRX HAS BEEN WAITING 22 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 0 page no 67344 n bits 104 index `PRIMARY` of table `test`.`tb` trx id 1169A0 lock_mode X locks gap before rec insert intention waiting
Record lock, heap no 27 PHYSICAL RECORD: n_fields 6; compact format; info bits 0
0: len 4; hex 8000000a; asc ;;
1: len 6; hex 000000038a02; asc ;;
2: len 7; hex 3c00010702031e; asc < ;;
3: len 4; hex 8000000a; asc ;;
4: len 4; hex 80000006; asc ;;
5: len 4; hex 8000000a; asc ;;
------------------
---TRANSACTION 11699F, ACTIVE 29 sec
2 lock struct(s), heap size 376, 1 row lock(s)
MySQL thread id 187, OS thread handle 0x2a88, query id 1884362 localhost 127.0.0.1 root
引用
CREATE TABLE `tb` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`a` int(11) ,
`b` int(11) ,
`c` int(11) ,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
/*Data for the table `tb` */
insert into `tb`(`id`,`a`,`b`,`c`) values (1,1,0,1);
insert into `tb`(`id`,`a`,`b`,`c`) values (2,2,1,2);
insert into `tb`(`id`,`a`,`b`,`c`) values (3,3,2,3);
insert into `tb`(`id`,`a`,`b`,`c`) values (4,4,2,4);
insert into `tb`(`id`,`a`,`b`,`c`) values (5,5,3,5);
insert into `tb`(`id`,`a`,`b`,`c`) values (6,6,3,6);
insert into `tb`(`id`,`a`,`b`,`c`) values (7,7,4,7);
insert into `tb`(`id`,`a`,`b`,`c`) values (8,8,5,8);
insert into `tb`(`id`,`a`,`b`,`c`) values (10,10,6,10);
###### 操作流程
事務1
START TRANSACTION;
DELETE FROM tb WHERE id=9 AND b=2;
------- 持有GAP(8,10)鎖
事務2
START TRANSACTION;
DELETE FROM tb WHERE id=9 AND b=2;
------- 持有GAP(8,10)鎖
事務1
INSERT INTO `tb` (`id`,`a`, `b`,`c`) VALUES (9,1,2,0);
------- 阻塞等待鎖,TRX HAS BEEN WAITING 22 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 0 page no 67344 n bits 104 index `PRIMARY` of table `test`.`tb` trx id 1169A0 lock_mode X locks gap before rec insert intention waiting
------- INNODB_LOCKS 表資料
lock_id lock_trx_id lock_mode lock_type lock_table lock_index lock_space lock_page lock_rec lock_data
1169A0:0:67344:27 1169A0 X,GAP RECORD `test`.`tb` `PRIMARY` 0 67344 27 10
11699F:0:67344:27 11699F X,GAP RECORD `test`.`tb` `PRIMARY` 0 67344 27 10
事務2
INSERT INTO `tb` (`id`,`a`, `b`,`c`) VALUES (9,1,2,0);
------- 發生死鎖,都在等待同一個GAP
###### 死鎖資訊
------------------------
LATEST DETECTED DEADLOCK
------------------------
150318 17:19:31
*** (1) TRANSACTION:
TRANSACTION 1169A0, ACTIVE 34 sec inserting
mysql tables in use 1, locked 1
LOCK WAIT 3 lock struct(s), heap size 376, 2 row lock(s)
MySQL thread id 185, OS thread handle 0x164c, query id 1884365 localhost 127.0.0.1 root update
INSERT INTO `tb` (`id`,`a`, `b`,`c`) VALUES (9,1,2,0)
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 0 page no 67344 n bits 104 index `PRIMARY` of table `test`.`tb` trx id 1169A0 lock_mode X locks gap before rec insert intention waiting
Record lock, heap no 27 PHYSICAL RECORD: n_fields 6; compact format; info bits 0
0: len 4; hex 8000000a; asc ;;
1: len 6; hex 000000038a02; asc ;;
2: len 7; hex 3c00010702031e; asc < ;;
3: len 4; hex 8000000a; asc ;;
4: len 4; hex 80000006; asc ;;
5: len 4; hex 8000000a; asc ;;
*** (2) TRANSACTION:
TRANSACTION 11699F, ACTIVE 39 sec inserting, thread declared inside InnoDB 500
mysql tables in use 1, locked 1
3 lock struct(s), heap size 376, 2 row lock(s)
MySQL thread id 187, OS thread handle 0x2a88, query id 1884373 localhost 127.0.0.1 root update
INSERT INTO `tb` (`id`,`a`, `b`,`c`) VALUES (9,1,2,0)
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 0 page no 67344 n bits 104 index `PRIMARY` of table `test`.`tb` trx id 11699F lock_mode X locks gap before rec
Record lock, heap no 27 PHYSICAL RECORD: n_fields 6; compact format; info bits 0
0: len 4; hex 8000000a; asc ;;
1: len 6; hex 000000038a02; asc ;;
2: len 7; hex 3c00010702031e; asc < ;;
3: len 4; hex 8000000a; asc ;;
4: len 4; hex 80000006; asc ;;
5: len 4; hex 8000000a; asc ;;
*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 0 page no 67344 n bits 104 index `PRIMARY` of table `test`.`tb` trx id 11699F lock_mode X locks gap before rec insert intention waiting
Record lock, heap no 27 PHYSICAL RECORD: n_fields 6; compact format; info bits 0
0: len 4; hex 8000000a; asc ;;
1: len 6; hex 000000038a02; asc ;;
2: len 7; hex 3c00010702031e; asc < ;;
3: len 4; hex 8000000a; asc ;;
4: len 4; hex 80000006; asc ;;
5: len 4; hex 8000000a; asc ;;
*** WE ROLL BACK TRANSACTION (2)
------------
TRANSACTIONS
------------
---TRANSACTION 1169A0, ACTIVE 24 sec inserting
mysql tables in use 1, locked 1
LOCK WAIT 3 lock struct(s), heap size 376, 2 row lock(s)
MySQL thread id 185, OS thread handle 0x164c, query id 1884365 localhost 127.0.0.1 root update
INSERT INTO `tb` (`id`,`a`, `b`,`c`) VALUES (9,1,2,0)
------- TRX HAS BEEN WAITING 22 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 0 page no 67344 n bits 104 index `PRIMARY` of table `test`.`tb` trx id 1169A0 lock_mode X locks gap before rec insert intention waiting
Record lock, heap no 27 PHYSICAL RECORD: n_fields 6; compact format; info bits 0
0: len 4; hex 8000000a; asc ;;
1: len 6; hex 000000038a02; asc ;;
2: len 7; hex 3c00010702031e; asc < ;;
3: len 4; hex 8000000a; asc ;;
4: len 4; hex 80000006; asc ;;
5: len 4; hex 8000000a; asc ;;
------------------
---TRANSACTION 11699F, ACTIVE 29 sec
2 lock struct(s), heap size 376, 1 row lock(s)
MySQL thread id 187, OS thread handle 0x2a88, query id 1884362 localhost 127.0.0.1 root
引用
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26250550/viewspace-1464959/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Java鎖——死鎖Java
- 死鎖
- SQ死鎖及死鎖的解決
- 面試官:什麼是死鎖?怎麼排查死鎖?怎麼避免死鎖?面試
- 什麼是死鎖?如何解決死鎖?
- 殺死Oracle死鎖程式Oracle
- 死鎖分析
- oracle 死鎖Oracle
- 檢視oracle死鎖程式並結束死鎖Oracle
- 例項詳解 Java 死鎖與破解死鎖Java
- MySQL死鎖系列-線上死鎖問題排查思路MySql
- 作業系統(5) 死鎖的概念 死鎖產生的必要條件 死鎖的處理策略 預防死鎖 避免死鎖 死鎖的檢測和解除 銀行家演算法作業系統演算法
- MySQL 死鎖和鎖等待MySql
- 遭遇ITL死鎖
- GCD 死鎖原因GC
- 死鎖案例分析
- HashMap死鎖分析HashMap
- SQL Server死鎖SQLServer
- 死鎖-舉例
- MySQL:一個死鎖分析 (未分析出來的死鎖)MySql
- mysql行鎖和死鎖檢測MySql
- SQLServer的死鎖分析(1):頁鎖SQLServer
- Mysql 兩階段鎖和死鎖MySql
- MySQL 死鎖解決MySql
- PostgreSQL死鎖相關SQL
- Java 中的死鎖Java
- MySQL死鎖問題MySql
- PostgreSQL 死鎖異常SQL
- 【MySQL】漫談死鎖MySql
- 淺談死鎖原理
- 批量刪除死鎖
- Oracle 死鎖處理Oracle
- Oracle死鎖處理Oracle
- ORACLE死鎖檢測Oracle
- 搭建死鎖場景
- GreatSQL 死鎖案例分析SQL
- MySQL解決死鎖MySql
- SQL Server 的死鎖SQLServer