JDBC中Mysql事务机制-行锁和表锁

1、开启事务

  start transaction;

  sql语句

  commit;

  

2、行级锁

  当修改索引字段时候,该条记录就会被锁上,不允许被修改 【暂时索引就是主键】

  A、update user set name='lisi' where id=2;

  B、update user set name='wangwu' where id=2; 【不可以执行,因为id为主键】

     update user set name='zhangsan' where id=3; 【可以执行,因为索引不同】

3、表级锁

  当修改非索引字段时候,该表就会被锁上,不允许被修改 【暂时非索引就是非主键】

  A、update user set name='lisi' where user_name='hanbing'; 【user_name是非索引字段,会将表锁上】

  B、update user set name='王五' where user_name='hanbing'; 【不可以执行】

     update user set name='zhangsan' where id=2; 【不可以执行】

posted @ 2021-02-25 10:06  陌上尘如玉  阅读(276)  评论(0)    收藏  举报