我发现了一个非常混乱的死锁情况,我需要帮助才能理解。
正在进行两项交易:
(2)持有查询delete from myTable where id = NAME_CONST('p_id',10000)
的锁。这是PRIMARY KEY的锁定,虽然不是完整的密钥而是范围。当它显示lock_mode X locks rec but not gap
时,看起来这是一个完整的写锁定
(1)正在等待同一个锁,也用于查询delete from myTable where id = NAME_CONST('p_id',10000)
(2)也试着去获取此锁并且MySQL检测到死锁。
我无法理解的是为什么(2)必须再次获取锁,因为它已经拥有它并且在所有情况下它都是写锁(lock_mode X)。
它看起来也像是完全相同的查询。
这是表格定义
create myTable (
id int unsigned not null,
value1 char(8) not null,
value2 int unsigned,
primary key (id, value1)
);
以下是来自SHOW ENGINE INNODB STATUS\G
------------------------
LATEST DETECTED DEADLOCK
------------------------
130313 14:46:28
*** (1) TRANSACTION:
TRANSACTION 75ACB8A3, ACTIVE 0 sec, process no 6110, OS thread id 139973945382656 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 5154970, query id 5201313618 192.168.0.2 user updating
delete from myTable where id = NAME_CONST('p_id',10000)
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 22371 page no 1598 n bits 104 index `PRIMARY` of table `db`.`myTable` trx id 75ACB8A3 lock_mode X waiting
Record lock, heap no 32 PHYSICAL RECORD: n_fields 5; compact format; info bits 0
0: len 4; hex 0005af3a; asc :;;
1: len 8; hex 2020202020202020; asc ;;
2: len 6; hex 000075acb890; asc u ;;
3: len 7; hex ea0000020d011e; asc ;;
4: len 4; hex 00000065; asc e;;
*** (2) TRANSACTION:
TRANSACTION 75ACB890, ACTIVE 0 sec, process no 6110, OS thread id 139973957895936 starting index read
mysql tables in use 1, locked 1
7 lock struct(s), hea
p size 1248, 6 row lock(s), undo log entries 4
MySQL thread id 5155967, query id 5201313625 192.168.0.1 user updating
delete from myTable where id = NAME_CONST('p_id',10000)
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 22371 page no 1598 n bits 104 index `PRIMARY` of table `db`.`myTable` trx id 75ACB890 lock_mode X locks rec but not gap
Record lock, heap no 32 PHYSICAL RECORD: n_fields 5; compact format; info bits 0
0: len 4; hex 0005af3a; asc :;;
1: len 8; hex 2020202020202020; asc ;;
2: len 6; hex 000075acb890; asc u ;;
3: len 7; hex ea0000020d011e; asc ;;
4: len 4; hex 00000065; asc e;;
*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 22371 page no 1598 n bits 104 index `PRIMARY` of table `db`.`myTable` trx id 75ACB890 lock_mode X waiting
Record lock, heap no 32 PHYSICAL RECORD: n_fields 5; compact format; info bits 0
0: len 4; hex 0005af3a; asc :;;
1: len 8; hex 2020202020202020; asc ;;
2: len 6; hex 000075acb890; asc u ;;
3: len 7; hex ea0000020d011e; asc ;;
4: len 4; hex 00000065; asc e;;
*** WE ROLL BACK TRANSACTION (1)
答案 0 :(得分:23)
它不是同一个锁 - 锁事务1只在(索引)记录上,而不是间隙锁。
以下是发生的事情:
我不完全确定为什么事务2不立即获取下一键锁定 - 也许获取记录锁定然后间隙锁定的过程不是原子的(在一般意义上)。
我认为问题在于你有一个复合主键(id,value1),但你要从一个范围中删除(仅指定id) - 这需要间隙锁。请参阅http://dev.mysql.com/doc/refman/5.0/en/innodb-record-level-locks.html,特别是:
使用唯一锁定行的语句不需要间隙锁定 用于搜索唯一行的索引。 (这不包括这种情况 搜索条件仅包括多列的一些列 独特指数;在这种情况下,确实会出现间隙锁定。)
您是否可以更改代码,以便在删除时指定完整的主键,即id 和 value1?
其他选择:
如果交易失败,请务必重新发布交易 僵局。死锁并不危险。再试一次。