我正在使用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的具体行为 –