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);#(执行成功)
session 1: start transaction ; select * from news where number=13 for update ; session 2: start transaction ; insert into news value(11,5);#(执行成功) insert into news value(12,11);#(执行成功) insert into news value(14,11);#(阻塞) insert into news value(15,12);#(阻塞) update news set id=14 where number=11;#(阻塞) update news set id=11 where number=11;#(执行成功)
session 1: start transaction ; select * from news where number=5 for update; session 2: start transaction ; insert into news value(4,4);#(阻塞) insert into news value(4,5);#(阻塞) insert into news value(5,5);#(阻塞) insert into news value(7,11);#(阻塞) insert into news value(9,12);#(执行成功) insert into news value(12,11);#(阻塞) update news set number=5 where id=1;#(阻塞) update news set id=11 where number=11;#(阻塞) update news set id=2 where number=4 ;#(执行成功) update news set id=4 where number=4 ;#(阻塞)
session 1: start transaction; select * from news where number>4 for update; session 2: start transaction; update news set id=2 where number=4 ;#(执行成功) update news set id=4 where number=4 ;#(阻塞) update news set id=5 where number=5 ;#(阻塞) insert into news value(2,3);#(执行成功) insert into news value(null,13);#(阻塞)