Mysql DeadLock 死锁

表结构

CREATE TABLE `t_custom_sku_price` (
  `id` BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `customSkuId` BIGINT(20) DEFAULT NULL COMMENT '自定义skuid',
  `priceType` INT(11) DEFAULT NULL COMMENT '价格类型',
  `price` DECIMAL(14,4) DEFAULT '0.0000' COMMENT '价格',
  PRIMARY KEY (`id`),
  KEY `customSkuId` (`customSkuId`)
)

执行步骤

 

sessionAsessionB
beginbegin
delete from t_custom_sku_price where customSkuId in (6243929); 
 delete from t_custom_sku_price where customSkuId in (6243923);
 insert into t_custom_sku_price (customSkuId,priceType,price)values(6243929,9,0.0); 
 insert into t_custom_sku_price (customSkuId,priceType,price)values(6243932,9,0.0);

执行结果

数据不存在的情况

sessionA

sessionB

sessionA rollback前,各事务锁占用情况:

SELECT * FROM information_schema.innodb_locks; 

 

代码中delete操作的是非唯一索引列在innoDB引擎下会触发 next-key lock(间隙锁)。

 

 

 

https://my.oschina.net/u/2342969/blog/1813772?p=1

 

beginbegin
delete from t_custom_sku_price where customSkuId in (6243929); 
 insert into t_custom_sku_price (customSkuId,priceType,price)values(6243932,9,0.0)

 

 

http://blog.itpub.net/22664653/viewspace-2145736/

 

调 delete 不存在的记录是要加上GAP锁,事务日志中显示Lock_mode X wait .

 

数据存在的情况,没有死锁

 

如何解决呢?
   a 先select 检查一下看看是否存在,然后在删除。这里也存在两个或者多个会话并发执行同一个select where条件的,这里需要开发同学做处理。
   b insert into on deuplicate key .

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值