MySQL更新锁(for update)摘要

来源:互联网 发布:航天金税官网开票软件 编辑:程序博客网 时间:2024/05/19 03:20

* 更新锁:for update *

  • 在事务中加更新锁后,该事务以外的加更新锁查询无法执行,但不影响普通查询。
// 进程1begin;select * from t where id = 1 for update;

现在开启另一个进程

// 进程2select * from t where id = 1 for update; // 此查询会等待进程1提交,或进程关闭后才执行。select * from t where id = 1; // 此查询不会等待,直接执行。

再开启一个进程

// 进程3begin;select * from t where id = 1 for update; // 此查询会等待进程1提交,或进程关闭后才执行。select * from t where id = 1; // 此查询不会等待,直接执行。

* 事务不能嵌套 *

  • 在事务中开启另一个事务,会提交前一个事务。
  • Laravel中可以使用 DB::transaction() 嵌套。框架中实际只会执行一次事务。
原创粉丝点击