# 事务t1 set autocommit =0; # 关闭事务自动提交 select*from users where id =123for update; update users set name ='Peppa'where id =123;
# 事务t2 set autocommit =0; # 关闭事务自动提交 select*from users where id =123 lock in share mode; # 语句会阻塞直到t1事务提交 select*from users where id =123for update; # 语句会阻塞直到t1事务提交 update users set name ='George'where id =123;
行锁实现方式
InnoDB的行锁是把锁加在了索引上,如果查询条件字段上没有索引,那么行锁会升级为表锁,例如
1 2 3 4 5 6 7
# 事务t1 set autocommit =0; # 关闭事务自动提交 update users set age =18where name ='Peppa';
# 事务t2 set autocommit =0; # 关闭事务自动提交 update users set age =20where name ='George'; # 语句会阻塞直到事务t1提交