上一篇文章介紹了使用調試 MySQL 源碼的方式來查看死鎖的過程,這篇文章來說講一個常見的案例。mysql
絕不誇張的說,有一半以上的死鎖問題由惟一索引貢獻,後面介紹的不少死鎖的問題都跟惟一索引有關。此次咱們講一段惟一索引 S 鎖與 X 鎖的愛恨情仇sql
咱們來看一個簡化過的例子數據庫
# 構造數據
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);
# 出現問題的sql語句以下,併發狀況下就會出現死鎖
INSERT ignore INTO `t1` (`name`, `level`) VALUES ('A',0);
update t1 set level = 1 where name = "A";
複製代碼
咱們用以前介紹過的源碼分析方式,先來看下這兩條語句分別加什麼鎖,而後分析死鎖造成的過程。bash
INSERT ignore INTO t1 (name, level) VALUES ('A',0);
session
在調試中獲得的結果以下併發
能夠看到這條語句對惟一鍵 uk_name 加共享鎖(S鎖),並且成功。update t1 set level = 1 where name = "A";
經過惟一鍵更新數據庫字段。源碼分析
這種狀況在以前的文章已經介紹過,會對惟一索引加 X 鎖,而後對主鍵索引加 X 鎖ui
這樣就能夠很是輕鬆的復現死鎖的問題了,步驟以下INSERT ignore INTO t1 (name, level) VALUES ('A',0);
INSERT ignore INTO t1 (name, level) VALUES ('A',0);
update t1 set level = 1 where name = "A";
進入等待狀態update t1 set level = 1 where name = "A";
,死鎖產生,被回滾,同時事務 1 執行成功詳細的鎖狀態變化以下spa
t1 | t2 | 備註 |
---|---|---|
INSERT IGNORE INTO | - | t1成功得到uk的S鎖 DB_SUCCESS |
- | INSERT IGNORE INTO | t2成功得到uk的S鎖 DB_SUCCESS |
UPDATE | - | t1嘗試得到uk的X鎖,但沒有成功,處於等待狀態 DB_LOCK_WAIT |
- | UPDATE | t2嘗試得到uk的X鎖,發現死鎖產生 DB_DEADLOCK |
- | Deadlock | t2釋放S鎖 |
成功 | - | - |
死鎖日誌以下:3d
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)
複製代碼
來詳細看一下這個死鎖日誌
*** (1) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 89 page no 4 n bits 72 index
uk_name
of tablelock_demo2
.t1
trx id 53A7 lock_mode X locks rec but not gap waiting
事務 1 想獲取 uk_name 惟一索引上的 X 鎖 (非 gap 鎖的記錄鎖)
*** (2) HOLDS THE LOCK(S): RECORD LOCKS space id 89 page no 4 n bits 72 index
uk_name
of tablelock_demo2
.t1
trx id 53A8 lock mode S
事務 2 持有uk_name 惟一索引上的 S 鎖(共享鎖)
*** (2) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 89 page no 4 n bits 72 index
uk_name
of tablelock_demo2
.t1
trx id 53A8 lock_mode X locks rec but not gap waiting
事務 2 想得到 uk_name 惟一索引上的 X 鎖(非 gap 鎖的記錄鎖)
跟以前理論上推斷的結論是一致的