最近项目用的mysql数据库,在多个表同时修改时遇到锁表的问题,
测试得知,InnoDB在明确指定表主键id时才会执行ROwLock,否则执行表lock,
select * from table1 where id=1 for update; 此时会执行该行数据的lock
select * from table1 where name=1 for update; select * from table1 where id like '1%' for update; 此时执行的表lock;
在mysql中可以启用事务自测。
代码中,一般事务定义在服务层,在服务层中可以执行
select * from table1 where id=1 for update;
update table1 set name=2 where id=1;
这样可以在保证数据唯一性的前提下 修改数据