【MySQL】gap lock 浅析

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,高可用系列 2核4GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介:
【定义】
innodb 行级锁 record-level lock大致有三种:record lock, gap lock and Next-KeyLocks。
record lock  锁住某一行记录  
gap lock     锁住某一段范围中的记录 
next key lock 是前两者效果的叠加。
下面是MYSQL官方文档中相关内容的链接
http://dev.mysql.com/doc/refman/5.1/en/innodb-record-level-locks.html
【实验环境】
session 1 20:39:29> show create table gap \G
*************************** 1. row ***************************
       Table: gap
Create Table: CREATE TABLE `gap` (
  `id` int(11) DEFAULT NULL,
  KEY `ind_gap_id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
session 1 20:39:32> select * from gap;      
+------+
| id   |
+------+
|   17 |
|   20 |
|   33 |
|   39 |
|   42 |
|   43 |
+------+
6 rows in set (0.00 sec)

【实验】
两个会话都在REPEATABLE-READ 事务隔离级别。且都要在事务中进行。
session 1  20:39:37> start transaction;      
Query OK, 0 rows affected (0.00 sec)
session 1  20:39:41> delete from gap where id=33;
Query OK, 1 row affected (0.00 sec)
session 20:40:07> 

在会话2中 插入id <20 和 >=39的值 可以执行成功,而当要插入的id [20,39)的值时 会遇到gap lock 。
session 2 20:40:15> start transaction;
Query OK, 0 rows affected (0.00 sec)
session 2 20:40:30> insert into gap values(14);
Query OK, 1 row affected (0.00 sec)
session 2 20:40:59> insert into gap values(18);
Query OK, 1 row affected (0.00 sec)
session 2 20:41:06> insert into gap values(20);
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
session 2 20:41:12> insert into gap values(24);
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
session 2 20:42:17> 
session 2 20:42:53> insert into gap values(35); 
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
session 2 20:44:09> 
session 2 20:44:56> insert into gap values(39);
Query OK, 1 row affected (0.00 sec)
session 2 20:45:13> insert into gap values(40);              
Query OK, 1 row affected (0.00 sec)

从上面的实验中可以看出会话1 执行删除语句之后,不仅仅锁住 id=33的记录,同时也锁住区间为[20,39)的记录。具体的原因是执行delete from gap where id=33语句,mysql 会执行索引扫描并在该表上施加一个next-key lock ,向左扫描到20,向右扫描到39 ,锁定区间左闭右开,所以lock的范围是 [20,39)。

【gap 锁带来的问题】
生产环境中有这样的一个情况:
程序会对一个表message 进行update 和insert 
session 1
UPDATE message SET gmt_modified = now(),deal_times = deal_times +1   , status = 'sending' , gmt_retry = '2012-11-17 23:54:10' 
WHERE message_id=18;
insert into  message (body ,user_id,status,message_type,version,deal_times,gmt_create,gmt_modified,gmt_retry) 
values ('hello !',-1,'sending','instance_status_sync',2,127,now(),now(),now());

session 2
UPDATE message SET gmt_modified = now(),deal_times = deal_times +1   , status = 'sending' , gmt_retry = '2012-11-17 23:54:10' 
WHERE message_id=19;
insert into  message (body ,user_id,status,message_type,version,deal_times,gmt_create,gmt_modified,gmt_retry) 
values          ('hello world!',-2,'sending','instance_status_sync',1,17,now(),now(),now());

对于上述程序在无并发情况下,运行正常,但是并发量大的情况下,执行顺序可能就会变成下面的:
UPDATE message SET gmt_modified = now(),deal_times = deal_times +1   , status = 'sending' , gmt_retry = '2012-11-17 23:54:10' 
WHERE message_id= 61;
UPDATE message SET gmt_modified = now(),deal_times = deal_times +1   , status = 'sending' , gmt_retry = '2012-11-17 23:54:10' 
WHERE message_id= 73;
insert into  message (body ,user_id,status,message_type,version,deal_times,gmt_create,gmt_modified,gmt_retry) 
values          ('hello world!',-2,'sending','instance_status_sync',1,17,now(),now(),now());
insert into  message (body ,user_id,status,message_type,version,deal_times,gmt_create,gmt_modified,gmt_retry) 
values ('hello !',-1,'sending','instance_status_sync',2,127,now(),now(),now()); 
此时 往往会报错
[ERROR]  Could not execute Write_rows event on table db.message; Deadlock found when trying toget lock; ; try restarting transaction, Error_code: 1213;  

前两条update 类型的语句都已经获得了[59,75 )区间内记录的S锁,然后两个事务又分别对该区间段内的message_id=10这个位置请求X锁,这时就发生死锁,谁都请求不到X锁,因为互相都持有S锁。

【解决方案有两种】
1、改变程序中数据库操作的逻辑
2、取消gap lock机制
Gap locking can be disabled explicitly.This occurs if you change the transaction isolation level to READ COMMITTED orenable the innodb_locks_unsafe_for_binlog system variable.
相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
相关文章
|
关系型数据库 MySQL Java
MySQL数据锁:Record Lock,Gap Lock 和 Next-Key Lock
本文基于 MySQL 8.0.30 版本及 InnoDB 引擎,深入解析三种行锁机制:记录锁(Record Lock)、间隙锁(Gap Lock)和临键锁(Next-key Lock)。记录锁锁定索引记录,确保事务唯一修改;间隙锁锁定索引间的间隙,防止新记录插入;临键锁结合两者,锁定范围并记录自身,有效避免幻读现象。通过具体示例展示了不同锁的作用机制及其在并发控制中的应用。
1224 2
|
SQL 关系型数据库 MySQL
Mysql Lock Wait
Mysql Lock Wait
282 0
|
存储 SQL 关系型数据库
【MySQL技术内幕】6.1-锁、lock和latch
【MySQL技术内幕】6.1-锁、lock和latch
305 0
|
关系型数据库 MySQL 数据库
MySQL报错:Lock wait timeout exceeded; try restarting transaction
MySQL报错:Lock wait timeout exceeded; try restarting transaction
1542 0
|
SQL 关系型数据库 MySQL
MySQL报错:1205 Lock wait timeout exceeded; try restarting transaction处理
MySQL报错:1205 Lock wait timeout exceeded; try restarting transaction处理
941 0
|
SQL 关系型数据库 MySQL
【MySQL异常】MySQL事务锁问题----lock wait timeout exceeded; try restarting transaction
【MySQL异常】MySQL事务锁问题----lock wait timeout exceeded; try restarting transaction
1171 0
|
SQL 关系型数据库 MySQL
Mysql 异常:Lock wait timeout exceeded; try restarting transaction的解决办法
Mysql 异常:Lock wait timeout exceeded; try restarting transaction的解决办法
876 0
|
SQL 关系型数据库 MySQL
mysql Lock wait timeout exceeded; try restarting transaction解决方案
在测试程序时,打的断点怎么都跳不进去,console一直报 “Lock wait timeout exceeded; try restarting transaction”
379 0
|
SQL 关系型数据库 MySQL
mysql中lock tables与unlock tables(锁表/解锁)使用总结
mysql中lock tables与unlock tables(锁表/解锁)使用总结
551 0

推荐镜像

更多