@
wenxueywx 根据本人目前掌握理解的水平, 如果主表不加锁, a 事务和 b 事务 都分别各自按照下面 sql 执行执行
====sessionA
begin;
UPDATE a_detail set approval_status = 2 WHERE id = 1 and approval_status = 1;
select count(0) from a_detail WHERE auid = 'a1' and approval_status!=2 lock in share mode ;
commit;
=====sessionB
begin;
UPDATE a_detail set approval_status = 2 WHERE id = 2 and approval_status = 1;
select count(0) from a_detail WHERE auid = 'a1' and approval_status!=2 lock in share mode ;
commit;
各自执行 update where id=都会加上各自 id 的行锁,
但各自执行到 select count()都要去根据 auid 这个非唯一索引进行等值查询,都会触发各自的间隙锁,但是又与对方事务的行锁冲突,造成死锁