mysql自增序号更新死锁_MySQL死锁案例二(自增列导致死锁)

测试环境:MySQL 5.7.26

创建测试表:

create table t2(id int auto_increment,status int default null,primary key(id),unique key(status));

Query OK, 0 rows affected (0.01 sec)

死锁模拟过程如下:

8395534aff69d74b97225c3ff33df6fc.png

会话2申请s锁,等待会话1,会话1申请意向插入锁,等待会话2。innodb选择kill掉会话2,并抛出

ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction

过程说明:

3734fa3d5783dd10f7132cf4f6acfbcd.png

查看死锁日志:

LATEST DETECTED DEADLOCK

------------------------

2020-08-21 17:28:12 0x7f38f84d3700

*** (1) TRANSACTION:

TRANSACTION 26167492, ACTIVE 17 sec inserting

mysql tables in use 1, locked 1

LOCK WAIT 2 lock struct(s), heap size 1136, 1 row lock(s), undo log entries 1

MySQL thread id 208, OS thread handle 139883056465664, query id 3844857 localhost root update

insert into t2 values(null,10)

*** (1) WAITING FOR THIS LOCK TO BE GRANTED:

RECORD LOCKS space id 832 page no 4 n bits 72 index status of table `test`.`t2` trx id 26167492 lock mode S waiting

Record lock, heap no 2 PHYSICAL RECORD: n_fields 2; compact format; info bits 0

0: len 4; hex 8000000a; asc     ;;

1: len 4; hex 80000001; asc     ;;

*** (2) TRANSACTION:

TRANSACTION 26167469, ACTIVE 28 sec inserting, thread declared inside InnoDB 5000

mysql tables in use 1, locked 1

3 lock struct(s), heap size 1136, 2 row lock(s), undo log entries 2

MySQL thread id 209, OS thread handle 139882660706048, query id 3844926 localhost root update

insert into t2 values(null,9)

*** (2) HOLDS THE LOCK(S):

RECORD LOCKS space id 832 page no 4 n bits 72 index status of table `test`.`t2` trx id 26167469 lock_mode X locks rec but not gap

Record lock, heap no 2 PHYSICAL RECORD: n_fields 2; compact format; info bits 0

0: len 4; hex 8000000a; asc     ;;

1: len 4; hex 80000001; asc     ;;

*** (2) WAITING FOR THIS LOCK TO BE GRANTED:

RECORD LOCKS space id 832 page no 4 n bits 72 index status of table `test`.`t2` trx id 26167469 lock_mode X locks gap before rec insert intention waiting

Record lock, heap no 2 PHYSICAL RECORD: n_fields 2; compact format; info bits 0

0: len 4; hex 8000000a; asc     ;;

1: len 4; hex 80000001; asc     ;;

*** WE ROLL BACK TRANSACTION (1)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值