当前位置:Gxlcms > 数据库问题 > mysql悲观锁的实现

mysql悲观锁的实现

时间:2021-07-01 10:21:17 帮助过:3人阅读

(三者选一就可以)

//1.查询出商品信息

select status from table where id=1 for update;

//2.根据商品信息生成订单

insert into table111 (id,goods_id) values (null,1);

//3.修改商品status为2

update table set status=2 where id=1;

//4.提交事务

commit;/commit work; (任选一种释放锁)

总结:

  1. 实现悲观锁利用select ... for update加锁, 操作完成后使用commit来释放锁

  2. innodb引擎时, 默认行级锁, 当有明确字段时会锁一行, 如无查询条件或条件

      字段不明确时, 会锁整个表. 条件为范围时会锁整个表

  3. 查不到数据时, 则不会锁表

mysql悲观锁的实现

标签:修改   art   href   使用   http   https   信息   value   查询   

人气教程排行