Home  >  Article  >  Database  >  How to use mysql gap lock

How to use mysql gap lock

WBOY
WBOYforward
2023-05-29 10:31:051359browse

Interval lock, only lock one index interval (open interval, excluding double-end endpoints)

1. Lock in the gap between index records, or lock before or after the index record, The index record itself is not included.

2. Gap locks can be used to prevent phantom reads to ensure that data will not be inserted between indexes.

Example

session 1:
start  transaction ;
select  * from news where number=4 for update ;
 
session 2:
start  transaction ;
insert into news value(2,4);#(阻塞)
insert into news value(2,2);#(阻塞)
insert into news value(4,4);#(阻塞)
insert into news value(4,5);#(阻塞)
insert into news value(7,5);#(执行成功)
insert into news value(9,5);#(执行成功)
insert into news value(11,5);#(执行成功)

The above is the detailed content of How to use mysql gap lock. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete