我正在使用MySQL 5.5。我注意到在併發場景中發生了一個特殊的死鎖,我認爲這種僵局不會發生。當升級共享到獨佔鎖時避免MySQL死鎖
重現這樣,利用同時運行兩個MySQL客戶端會話:
MySQL的會話1:
create table parent (id int(11) primary key);
insert into parent values (1);
create table child (id int(11) primary key, parent_id int(11), foreign key (parent_id) references parent(id));
begin;
insert into child (id, parent_id) values (10, 1);
-- this will create shared lock on parent(1)
MySQL的會話2:
begin;
-- try and get exclusive lock on parent row
select id from parent where id = 1 for update;
-- this will block because of shared lock in session 1
MySQL的會話1:
-- try and get exclusive lock on parent row
select id from parent where id = 1 for update;
-- observe that mysql session 2 transaction has been rolled back
MySQL的會議2:
ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction
從show engine innodb status
報告的信息是這樣的:
------------------------
LATEST DETECTED DEADLOCK
------------------------
161207 10:48:56
*** (1) TRANSACTION:
TRANSACTION 107E67, ACTIVE 43 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 2 lock struct(s), heap size 376, 1 row lock(s)
MySQL thread id 13074, OS thread handle 0x7f68eccfe700, query id 5530424 localhost root statistics
select id from parent where id = 1 for update
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 0 page no 3714 n bits 72 index `PRIMARY` of table `foo`.`parent` trx id 107E67 lock_mode X locks rec but not gap waiting
Record lock, heap no 2 PHYSICAL RECORD: n_fields 3; compact format; info bits 0
0: len 4; hex 80000001; asc ;;
1: len 6; hex 000000107e65; asc ~e;;
2: len 7; hex 86000001320110; asc 2 ;;
*** (2) TRANSACTION:
TRANSACTION 107E66, ACTIVE 52 sec starting index read
mysql tables in use 1, locked 1
5 lock struct(s), heap size 1248, 2 row lock(s), undo log entries 1
MySQL thread id 12411, OS thread handle 0x7f68ecfac700, query id 5530425 localhost root statistics
select id from parent where id = 1 for update
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 0 page no 3714 n bits 72 index `PRIMARY` of table `foo`.`parent` trx id 107E66 lock mode S locks rec but not gap
Record lock, heap no 2 PHYSICAL RECORD: n_fields 3; compact format; info bits 0
0: len 4; hex 80000001; asc ;;
1: len 6; hex 000000107e65; asc ~e;;
2: len 7; hex 86000001320110; asc 2 ;;
*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 0 page no 3714 n bits 72 index `PRIMARY` of table `foo`.`parent` trx id 107E66 lock_mode X locks rec but not gap waiting
Record lock, heap no 2 PHYSICAL RECORD: n_fields 3; compact format; info bits 0
0: len 4; hex 80000001; asc ;;
1: len 6; hex 000000107e65; asc ~e;;
2: len 7; hex 86000001320110; asc 2 ;;
*** WE ROLL BACK TRANSACTION (1)
你可以看到交易(1)不顯示任何S或X鎖已經獲得;它只是試圖獲得排他鎖。據我瞭解,由於沒有周期,所以在這種情況下不應該出現僵局。
這是一個已知的MySQL錯誤?有其他人遇到過嗎?使用了哪些解決方法?
這些都是可能的前進腳步,我們可以採取:
- 減少我們的外鍵的使用(以我們的生產場景中,我們只軟刪除引用的錶行,但噁心)
- 採集獨家鎖定前面而不是隱式共享鎖(會降低我們的併發吞吐量)
- 更改我們的邏輯,以便我們不再需要在同一個事務中添加子行的排他鎖(風險和難度)
- 更改我們的版本的MySQL到o ne沒有表現出這種行爲
我們沒有考慮其他選擇嗎?
https://bugs.mysql.com/bug.php?id=48652特別是在2012年10月22日12:32由馬爾科Mäkelä評論。 – bishop
剛剛轉載了上述步驟。 Mysql 5.1.73 UPD沒有錯誤。 但是,錯誤存在於5.7.17,所以我認爲它是版本> 5.5的具體行爲 –