gpt4 book ai didi

When multiple transactions insert the same value at the same time, why except one get a duplicate exception?(当多个事务同时插入相同的值时,为什么只有一个事务会得到重复的异常?)

转载 作者:bug小助手 更新时间:2023-10-28 22:33:13 25 4
gpt4 key购买 nike



I don't understand INSERT sets an exclusive lock on the inserted row. part of this document.

https://dev.mysql.com/doc/refman/8.0/en/innodb-locks-set.html

我不明白INSERT在插入的行上设置了排他锁。本文档的一部分。Https://dev.mysql.com/doc/refman/8.0/en/innodb-locks-set.html



In below part,

在下面的部分,



Session 1:
START TRANSACTION;
INSERT INTO t1 VALUES(1);

Session 2:
START TRANSACTION;
INSERT INTO t1 VALUES(1);

Session 3:
START TRANSACTION;
INSERT INTO t1 VALUES(1);


I thought they would go in the order below

我以为他们会按下面的顺序排




  1. First transaction got exclusive lock for number 1 row.(didn't committed yet)

  2. Second transaction also try to get exclusive lock.

  3. Second transaction is going to wait, because First transaction already has exclusive lock.



But they didn't like above, the document said

但他们不喜欢上面的内容,文件说




The operations by sessions 2 and 3 both result in a duplicate-key error and they both request a shared lock for the row.




I don't understand why they got duplicate exception.

For a duplicate error to occur, the row must already be committed.

But they didn't. First transaction didn't commit yet.

我不明白为什么他们会有重复的例外。若要发生重复错误,该行必须已提交。但他们没有。第一笔交易还没有提交。


更多回答

Are you sure autocommit is not on? see select @@autocommit

你确定没有自动提交吗?请参见select @@autocommit

You have to look at mysql's internals to get a full understanding. My guess is that mysql indicates in the index records that value 1 is being inserted, hence the duplicate key error. The behaviour is consistent with the documentation.

您必须查看MySQL的内部结构才能完全理解。我的猜测是,MySQL在索引记录中指示插入了值1,因此出现了重复键错误。这种行为与文件中的描述一致。

I have tested again just now. Autocommit is not on. When I execute insert operation in session 2, session 2 was waiting continuously.

我刚刚又测试了一次。自动提交未打开。当我在会话2中执行插入操作时,会话2一直在等待。

优秀答案推荐

I suspect this is because before inserting an unique value (unique index), InnoDB has to do a search (secondary index scan), this will create next-key locks (next-key locks = record locks + gap locks).

我怀疑这是因为在插入唯一值(唯一索引)之前,InnoDB必须执行搜索(二级索引扫描),这将创建下一键锁(下一键锁=记录锁+间隙锁)。


Because there is a gap lock, doing insertion will require transaction to accquire the Insert Intention Locks first. The purpose of this lock is only to allow different transactions to insert rows into a gap at the same time if the rows are not duplicate. If the rows are duplicate then duplicate-key error will be raised.

因为有空隙锁,所以做插入需要交易先获取插入意向锁。此锁的用途只是允许不同的事务在行不重复的情况下同时将行插入到间隙中。如果行重复,则会引发重复键错误。


In this example, transaction 1 accquired next-key locks from the minimum value to 1, then exclusive lock on 1 and inserted the row before committing. Transaction 2 and 3 accquired Insert Intention Locks as there is a next-key locks, and they realize their insertions are duplicate of each other.

在本例中,事务1获取从最小值到1的Next-key锁,然后在1上获得排他锁,并在提交之前插入行。事务2和事务3获得插入意向锁,因为存在下一键锁,并且它们意识到它们的插入是彼此重复的。


更多回答

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com