重庆小潘seo博客

当前位置:首页 > 重庆网络营销 > 小潘杂谈 >

小潘杂谈

mysql悲观锁怎么实现?

时间:2020-09-22 04:00:06 作者:重庆seo小潘 来源:
mysql悲观锁的方法: 1、开始事务begin;/begin work;/start transaction; (三者选一就可以)2、查询出商品信息select status from table where id=1 for update;3、根据商品信息生成订单insert into table111 (id,goods_id) values (null,1);4、修改商品statu

mysql悲观锁怎么实现?

mysql悲观锁的方法:

1、开始事务begin;/begin work;/start transaction; (三者选一就可以)2、查询出商品信息select status from table where id=1 for update;3、根据商品信息生成订单insert into table111 (id,goods_id) values (null,1);4、修改商品status为2update table set status=2 where id=1;5、提交事务commit;/commit work; (任选一种释放锁)总结:

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

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

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

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

以上就是mysql悲观锁怎么实现?的详细内容,更多请关注小潘博客其它相关文章!