mysql更新和回滚造成锁表_MySQL死锁案例一(回滚导致死锁)

在MySQL 5.7.26环境下,通过创建测试表t1并模拟两个并发会话的插入操作,展示了一个由于回滚导致的死锁现象。当会话1回滚后,会话2和3发生死锁,MySQL选择终止会话3并抛出错误1213。死锁日志显示两个事务分别等待对方释放名为name的唯一索引锁,揭示了死锁的原因。
摘要由CSDN通过智能技术生成

测试环境:MySQL 5.7.26

创建测试表:

create table t1 (id int not null,name int,primary key(id),unique key(name));

死锁模拟过程如下:

b563a00e3e031ee49268aef0c46bf686.png

会话1rollback之后会话2和3出现死锁,innodb选择kill掉会话3并抛出:

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

死锁日志如下:

LATEST DETECTED DEADLOCK

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

2020-08-21 16:56:19 0x7f39023e7700

*** (1) TRANSACTION:

TRANSACTION 26163597, ACTIVE 46 sec inserting, thread declared inside InnoDB 1

mysql tables in use 1, locked 1

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

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

insert into t1 values(2018,255)

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

RECORD LOCKS space id 831 page no 4 n bits 72 index name of table `test`.`t1` trx id 26163597 lock_mode X insert intention waiting

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

0: len 8; hex 73757072656d756d; asc supremum;;

*** (2) TRANSACTION:

TRANSACTION 26163678, ACTIVE 6 sec inserting, thread declared inside InnoDB 1

mysql tables in use 1, locked 1

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

MySQL thread id 210, OS thread handle 139882827511552, query id 3837238 localhost root update

insert into t1 values(2019,255)

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

RECORD LOCKS space id 831 page no 4 n bits 72 index name of table `test`.`t1` trx id 26163678 lock mode S

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

0: len 8; hex 73757072656d756d; asc supremum;;

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

RECORD LOCKS space id 831 page no 4 n bits 72 index name of table `test`.`t1` trx id 26163678 lock_mode X insert intention waiting

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

0: len 8; hex 73757072656d756d; asc supremum;;

*** WE ROLL BACK TRANSACTION (2)

本文由 @崔任远[Vip] 发布于 职涯宝 ,未经作者许可,禁止转载,欢迎您分享文章

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值