您好,登錄后才能下訂單哦!
在初學(xué)者從源碼理解MySQL死鎖問題中介紹了使用調(diào)試 MySQL 源碼的方式來查看死鎖的過程,這篇文章來講講一個常見的案例。
這次我們講一段唯一索引 S 鎖與 X 鎖的愛恨情仇
我們來看一個簡化過的例子
# 構(gòu)造數(shù)據(jù) 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); # 出現(xiàn)問題的sql語句如下,并發(fā)情況下就會出現(xiàn)死鎖 INSERT ignore INTO `t1` (`name`, `level`) VALUES ('A',0); update t1 set level = 1 where name = "A";
我們用之前介紹過的源碼分析方式,先來看下這兩條語句分別加什么鎖,然后分析死鎖形成的過程。
第一條語句
INSERT ignore INTO t1 (name, level) VALUES ('A',0);
在調(diào)試中得到的結(jié)果如下
可以看到這條語句對唯一鍵 uk_name 加共享鎖(S鎖),而且成功。
第二條語句
update t1 set level = 1 where name = "A";
通過唯一鍵更新數(shù)據(jù)庫字段。
這種情況在之前的文章已經(jīng)介紹過,會對唯一索引加 X 鎖,然后對主鍵索引加 X 鎖
這樣就可以非常輕松的復(fù)現(xiàn)死鎖的問題了,步驟如下
1.開啟兩個 session,分別 begin
2.session1 執(zhí)行INSERT ignore INTO t1 (name, level) VALUES ('A',0);
3.session2 執(zhí)行INSERT ignore INTO t1 (name, level) VALUES ('A',0);
4.session1 執(zhí)行update t1 set level = 1 where name = "A"; 進(jìn)入等待狀態(tài)
5.session2 執(zhí)行update t1 set level = 1 where name = "A";,死鎖產(chǎn)生,被回滾,同時事務(wù) 1 執(zhí)行成功
詳細(xì)的鎖狀態(tài)變化如下
t1 | t2 | 備注 |
---|---|---|
INSERT IGNORE INTO | - | t1成功獲得uk的S鎖 DB_SUCCESS |
- | INSERT IGNORE INTO | t2成功獲得uk的S鎖 DB_SUCCESS |
UPDATE | - | t1嘗試獲得uk的X鎖,但沒有成功,處于等待狀態(tài) DB_LOCK_WAIT |
- | UPDATE | t2嘗試獲得uk的X鎖,發(fā)現(xiàn)死鎖產(chǎn)生 DB_DEADLOCK |
- | Deadlock | t2釋放S鎖 |
成功 | - | - |
死鎖日志如下:
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)
來詳細(xì)看一下這個死鎖日志
*** (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
事務(wù) 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 table lock_demo2.t1 trx id 53A8 lock mode S
事務(wù) 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 table lock_demo2.t1 trx id 53A8 lock_mode X locks rec but not gap waiting
事務(wù) 2 想獲得 uk_name 唯一索引上的 X 鎖(非 gap 鎖的記錄鎖)
跟之前理論上推斷的結(jié)論是一致的
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。