重复键错误导致的死锁,“X 插入意图等待...X 锁定记录但不锁定间隙”与“S 锁定记录但不锁定间隙等待”

问题描述 投票:0回答:0

我正在调试由重复键错误引起的死锁。根据MySQL文档

如果发生重复键错误,重复索引上的共享锁 记录被设定。使用共享锁可能会导致死锁 如果另一个会话有多个会话尝试插入同一行 session 已经有独占锁。如果另一个 会话删除该行。

它给出的例子很容易理解,因为它涉及 3 个会话。但我的案例只涉及2笔交易。根据日志,发生了以下死锁,事务A持有

lock_mode X locks rec but not gap
等待
lock_mode X insert intention waiting
,而事务B等待
lock mode S locks rec but not gap waiting

在死锁发生之前,我们发现其中一个事务已经插入了一条记录,然后另一个事务试图插入相同的数据。但我还是不明白为什么重复键错误会导致死锁。

有人可以解释一下吗?

*** (1) TRANSACTION:
TRANSACTION 3802025924, ACTIVE 0 sec inserting
...
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 1469239 page no 46337 n bits 128 index PRIMARY of table `emicall_cc_man`.`seat_c_state_record` /* Partition `p4` */ trx id 3802025924 lock mode S locks rec but not gap waiting

*** (2) TRANSACTION:
TRANSACTION 3802025922, ACTIVE 0 sec inserting
...
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 1469239 page no 46337 n bits 128 index PRIMARY of table `emicall_cc_man`.`seat_c_state_record` /* Partition `p4` */ trx id 3802025922 lock_mode X locks rec but not gap

*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 1469239 page no 46338 n bits 128 index PRIMARY of table `emicall_cc_man`.`seat_c_state_record` /* Partition `p4` */ trx id 3802025922 lock_mode X insert intention waiting

mysql innodb database-deadlocks
© www.soinside.com 2019 - 2024. All rights reserved.