InnoDB中的行锁+间隙锁Next-Key Lock。A事务范围更新语句将给范围内的数据行添加行锁,其他事务只能读不能写;范围间的间隙添加间隙锁,该示例中将在(105,111),(111,123),(123,124)之间添加间隙锁,间隙中不能插入新的记录,
该机制使得mysql在可重复读级别(repeatable read)解决了幻读的问题
测试6(mysql的表锁):
事务A
事务B
update order_test set buy_num=25 where item_name ='西瓜'; (无主键、索引,table lock)
update order_test set buy_num=10 where order_num=105; 表被锁住,无法更新
update order_test set buy_num=25 where order_num =90; (指定主键,若查无数据,加间隙锁(-∞,90)(90,105))
Insert into order_test values(95,100,'哈密瓜',9,now()); 间隙内,无法更新
insert into order_test values(89,309,'茄子',8,now()); 插入成功,间隙外不受影响
update order_test set buy_num =23 where order_num like '11%';(主键不明确,table lock)
update order_test set buy_num=10 where order_num=105; 表被锁住,无法更新
InnoDB 预设是Row-Level Lock,所以只有明确的指定主键或索引,MySQL 才会执行Row lock (只锁住被选取的数据) ,否则MySQL 将会执行Table Lock (将整个数据表单给锁住)