Analysis of gap lock:
The table structure is as follows:
CREATE TABLE `test_gap` ( `ID` int(11) NOT NULL, -- primary key `NAME` varchar(255) NOT NULL, -- non-unique index PRIMARY KEY (`ID`), KEY `NAME` (`NAME`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The data in the table is as follows:
Assuming UPDATE NAME = 'c' or DELETE NAME = 'c' would result in a gap lock:
At this time, the upper and lower intervals of the gap lock of the non-unique index will be searched, and the corresponding data interval in the table is (a, e);
1: If the inserted data belongs between (a, e), both b and d, it cannot be inserted at this time, because b and d are locked by the gap lock
2: If the inserted data does not belong between (a, e), then it is not affected by the gap lock and can be inserted freely
3: If the inserted data is equal to a or e, then the lock range needs to be judged according to the primary key:
1): If it is a, then take the maximum ID of a as 50, as long as the inserted data ID<50 can be inserted freely, if ID>50 is all locked and cannot be inserted
2): If it is e, take the minimum ID of e as 30, as long as the inserted data ID>30 can be inserted freely, if ID<30 is all locked and cannot be inserted
Then start simulating deadlock scenarios
Execution order: 1,2,3,4