我正在使用MySQL 5.5.我注意到在并发场景中发生了一个特殊的死锁,我不认为应该发生这种死锁.
使用两个同时运行的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)
Run Code Online (Sandbox Code Playgroud)
mysql session 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 …Run Code Online (Sandbox Code Playgroud)