MySQL5.7.11,tx_isolation为可重复读取;
像这样的table:
CREATE TABLE a (
id int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
insert into a values(1);
在会话1中,执行如下命令:
begin;
select * from a where id=2 for update;
则在会话2中执行:
begin;
insert into a values(3);
session 2被阻塞,我认为session 2被阻塞是因为差距锁,但是在information_schema. inodb_lock中,它显示了上确界伪记录和RECORD锁;
*************************** 1. row ***************************
lock_id: 234076:115:3:1
lock_trx_id: 234076
lock_mode: X
lock_type: RECORD
lock_table: `test`.`a`
lock_index: PRIMARY
lock_space: 115
lock_page: 3
lock_rec: 1
lock_data: supremum pseudo-record
*************************** 2. row ***************************
lock_id: 234075:115:3:1
lock_trx_id: 234075
lock_mode: X
lock_type: RECORD
lock_table: `test`.`a`
lock_index: PRIMARY
lock_space: 115
lock_page: 3
lock_rec: 1
lock_data: supremum pseudo-record
2 rows in set (0.00 sec)
MySQL_Doc描述如下:
For the last interval, the next-key lock locks the gap above the largest value in the index and the
“supremum” pseudo-record having a value higher than any value actually in the index. The supremum
is not a real index record, so, in effect, this next-key lock locks only the gap following the largest index
value.
为什么记录锁和lock_data是上确界伪记录?
1条答案
按热度按时间zpqajqem1#
这个StackOverflow answer解释得非常清楚。
基本上,如果没有可用的下一个键,比如当您锁定表末尾的一个范围时,MySQL将使用
pseudo-record
,因为它不知道该范围的结束位置。在这种情况下,锁将阻止您在SQL查询WHERE子句中使用的 predicate 锁的边界之外添加条目。
This article对于理解如何获取这些锁也非常有用。