mysql三例死锁场景分析

一、商户批量代发业务

1.表结构(简化):

CREATE TABLE `batch` (

 `batch_id` varchar(32) NOT NULL COMMENT '批次号',

 `cash_id` varchar(32) NOT NULL COMMENT '明细单号',

 `status` int(11) NOT NULL COMMENT '状态:0初始化,1已处理,2成功,3处理中,4失败,5 入库',

 PRIMARY KEY (`cash_id`),

  KEY`idx_batch_id` (`batch_id`),

KEY `idx_status` (`status`)

) ENGINE=InnoDB

2.产生死锁的两个事务:

tx1

update batch set status=0 wherebatch_id=’142’ and status=5

tx2

begin

insert …..

insert into batch (batch_id,cash_id, status)values(‘143’, ‘385’,5)

insert into batch (batch_id,cash_id, status)values(‘143’, ‘386’,5)

insert …..

 

3.死锁日志如下:

*** (1) TRANSACTION:

TRANSACTION 1576868964, ACTIVE 0 sec fetching rows

mysql tables in use 3, locked 3

LOCK WAIT 10 lock struct(s), heap size 2936, 103 rowlock(s), undo log entries 1

MySQL thread id 604347, OS thread handle0x7faf68903700, query id 2125933789 192.168.40.214 pay Searching rows forupdate

update pay_batch_paid

       set status = 0 where batch_id = '142' and status = 5

*** (1) WAITING FOR THIS LOCKTO BE GRANTED:

RECORD LOCKS space id 337 page no 963 n bits 504 index `idx_status` of table `pay`.`pay_batch_paid` trx id1576868964 lock_mode X waiting

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

 0: len 4; hex80000005; asc     ;;

 1: len 27; hex323031363037303631343637373932373637313432333536333835; asc 385;;

 

*** (2) TRANSACTION:

TRANSACTION 1576868930, ACTIVE 0 sec inserting

mysql tables in use 1, locked 1

4 lock struct(s), heap size 1184, 2 row lock(s), undolog entries 43

MySQL thread id 604176, OS thread handle0x7faf66b0d700, query id 2125933793 192.168.40.44 pay update

insert into batch (batch_id, cash_id, status)values(‘143’,‘386’,5)   

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

RECORD LOCKS space id 337 page no 963 n bits 504 index `idx_status` of table `pay`.`pay_batch_paid` trx id1576868930 lock_mode X locks rec but not gap

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

 0: len 4; hex80000005; asc     ;;

 1: len 27; hex323031363037303631343637373932373637313432333536333835; asc 385;;

 

*** (2) WAITING FOR THIS LOCKTO BE GRANTED:

RECORD LOCKS space id 337 page no 963 n bits 504 index`idx_status` of table `pay`.`pay_batch_paid` trx id 1576868930 lock_mode X locks gap before rec insert intention waiting

Record lock, heap no 330

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值