人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動(dòng)態(tài)

通過(guò)唯一索引S鎖與X鎖來(lái)了解MySQL死鎖套路

發(fā)布日期:2022-03-11 14:59 | 文章來(lái)源:源碼中國(guó)

在初學(xué)者從源碼理解MySQL死鎖問(wèn)題中介紹了使用調(diào)試 MySQL 源碼的方式來(lái)查看死鎖的過(guò)程,這篇文章來(lái)講講一個(gè)常見(jiàn)的案例。
這次我們講一段唯一索引 S 鎖與 X 鎖的愛(ài)恨情仇

我們來(lái)看一個(gè)簡(jiǎn)化過(guò)的例子

# 構(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)問(wèn)題的sql語(yǔ)句如下,并發(fā)情況下就會(huì)出現(xiàn)死鎖
INSERT ignore INTO `t1` (`name`, `level`) VALUES ('A',0);
update t1 set level = 1 where name = "A";

我們用之前介紹過(guò)的源碼分析方式,先來(lái)看下這兩條語(yǔ)句分別加什么鎖,然后分析死鎖形成的過(guò)程。

第一條語(yǔ)句

INSERT ignore INTO t1 (name, level) VALUES ('A',0);

在調(diào)試中得到的結(jié)果如下

可以看到這條語(yǔ)句對(duì)唯一鍵 uk_name 加共享鎖(S鎖),而且成功。

第二條語(yǔ)句

update t1 set level = 1 where name = "A"; 

通過(guò)唯一鍵更新數(shù)據(jù)庫(kù)字段。

這種情況在之前的文章已經(jīng)介紹過(guò),會(huì)對(duì)唯一索引加 X 鎖,然后對(duì)主鍵索引加 X 鎖

這樣就可以非常輕松的復(fù)現(xiàn)死鎖的問(wèn)題了,步驟如下

1.開(kāi)啟兩個(gè) 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)生,被回滾,同時(shí)事務(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鎖,但沒(méi)有成功,處于等待狀態(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)

來(lái)詳細(xì)看一下這個(gè)死鎖日志

*** (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é)論是一致的

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持本站。

海外穩(wěn)定服務(wù)器

版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實(shí)時(shí)開(kāi)通

自選配置、實(shí)時(shí)開(kāi)通

免備案

全球線路精選!

全天候客戶(hù)服務(wù)

7x24全年不間斷在線

專(zhuān)屬顧問(wèn)服務(wù)

1對(duì)1客戶(hù)咨詢(xún)顧問(wèn)

在線
客服

在線客服:7*24小時(shí)在線

客服
熱線

400-630-3752
7*24小時(shí)客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部