【问题标题】:MYSQL8. Why is transaction 2 locked in this situation?MYSQL8。为什么事务 2 在这种情况下被锁定?
【发布时间】:2022-01-19 06:31:59
【问题描述】:

准备好了


# ready
CREATE TABLE table_a
(
    id int PRIMARY KEY AUTO_INCREMENT,
    col_1 INT,
    INDEX idx_a (col_1)
) ENGINE = innodb;
id col_a
1 10
2 15
3 20
4 25

交易 1

START TRANSACTION;

SELECT *
FROM table_a
WHERE col_1 = 15
FOR UPDATE;

交易 2

START TRANSACTION;

# LOCK
INSERT INTO table_a (col_1) VALUES (10);

performance_schema.data_locks;

LOCK_TYPE LOCK_MODE LOCK_STATUS LOCK_DATA
TABLE IX GRANTED
RECORD X GRANTED 15, 2
RECORD X,REC_NOT_GAP GRANTED 2
RECORD X,GAP GRANTED 20, 3

我想知道为什么 Transaction2 被锁定

LOCK_MODE=x,LOCK_DATA=15,2不是在11-14范围内吗?

【问题讨论】:

  • 如果将col_1 = 15 更改为col_1 = 20 会发生什么?

标签: mysql locking innodb


【解决方案1】:

这里需要一个 Insert Intention Lock between (10, 15] on Repeatable reads 隔离级别。还需要在索引idx_a 上锁定col_1 value 15

参考https://dev.mysql.com/doc/refman/8.0/en/innodb-locking.html#innodb-insert-intention-locks

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-16
    • 1970-01-01
    • 1970-01-01
    • 2017-09-24
    • 2014-07-04
    • 1970-01-01
    相关资源
    最近更新 更多