Article Directory
for update skip locked
in conclusion:
- Determine whether the record has been locked by other sessions before locking
- If it is already locked, skip the locked record
- If there is no lock, then lock the unlocked record
The verification is as follows, the basic data:
SELECT * FROM stu_info t WHERE t.id IN (1, 2);
session1:
-- 锁住 id = 1 的所有记录
SELECT * FROM stu_info t WHERE t.id = 1 FOR UPDATE;
session2:
-- 锁住 id in (1, 2) 中未被锁定的记录 (跳过已经锁定的记录)
SELECT * FROM stu_info t WHERE t.id IN (1, 2) FOR UPDATE SKIP LOCKED;