mysql不能写重复键_MySQL锁定重复键错误

docs:

If a duplicate-key error occurs, a shared lock on the duplicate index

record is set. This use of a shared lock can result in deadlock should

there be multiple sessions trying to insert the same row if another

session already has an exclusive lock. This can occur if another

session deletes the row.

继续使用文档中的示例,

假设InnoDB表t1具有以下结构:

CREATE TABLE t1 (i INT, PRIMARY KEY (i)) ENGINE = InnoDB;

现在假设三个会话按顺序执行以下操作:

第一节:

START TRANSACTION;

INSERT INTO t1 VALUES(1);

第二节:

START TRANSACTION;

INSERT INTO t1 VALUES(1);

第3节:

START TRANSACTION;

INSERT INTO t1 VALUES(1);

第一节:

ROLLBACK;

The first operation by session 1 acquires an exclusive lock for the

row. The operations by sessions 2 and 3 both result in a duplicate-key

error and they both request a shared lock for the row. When session 1

rolls back, it releases its exclusive lock on the row and the queued

shared lock requests for sessions 2 and 3 are granted. At this point,

sessions 2 and 3 deadlock: Neither can acquire an exclusive lock for

the row because of the shared lock held by the other.

我有一些问题 :

1)insert查询对其插入的行进行独占锁定.因此,假设T1插入第1行,它将锁定第1行.现在,当T2开始写入时,INNODB会在执行之前评估查询,并发现它将写入相同的PK(i = 1的行)让T2等待?或者它会开始执行T2并发现它会出现重复键错误或PK违规.

2)为什么T2和T3采用共享锁?插入过程中共享锁如何进入画面?

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值