用户登录
用户注册

分享至

Laravel lockforupdate(悲观锁)

  • 作者: 无所谓啊79066138
  • 来源: 51数据库
  • 2022-10-21

问题描述

我正在尝试弄清楚如何正确使用/测试 lockforupdate,但我发现它的功能与我预期的不一样

这只是测试

公共函数索引(){返回 dd(DB::transaction(function() {如果 (Auth::guard('user')->check()) {$model = AppModelsUser::find(1)->lockForUpdate();睡眠(60);$model->point = 100000;$model->save();} 别的 {$model = AppModelsUser::find(1);$model->point = 999;$model->save();}返回 $model;}));}

我尝试在 2 个浏览器中进行测试,浏览器 1 用户登录而浏览器 2 未登录,浏览器 1 刷新,然后会在更新前锁定更新并休眠 60 秒

在 60 秒内,我转到浏览器 2 并刷新,但是记录没有锁定,我检查 phpmy***** 并更新记录(在浏览器 1 触发的 60 秒锁定内)

但是60秒后,记录又被浏览器1修改了(Point 100000)

我是不是误解了 lockforupdate 的用途?还是我测试错误?

我期望的是浏览器 2 在前 60 秒内不应修改该行(带有加载图标或错误抛出的空白页面?)

https://laravel.com/docs/5.2/queries#pessimistic-locking

我做了一些研究,但仍然无法理解 sharedLock(LOCK IN SHARE MODE) 和 lockForUpdate(FOR UPDATE) 之间的区别

顺便说一句,我确认数据库是 innodb

解决方案

这个工作,终于,但还是没明白sharedLock(LOCK IN SHARE MODE)和lockForUpdate(FOR UPDATE)有什么不同

 公共函数 index() {返回 dd(DB::transaction(function() {如果 (Auth::guard('user')->check()) {$model = AppModelsUser::lockForUpdate()->find(1);睡眠(30);$model->point = 100000;$model->save();} 别的 {$model = AppModelsUser::lockForUpdate()->find(1);$model->point = $model->point + 1;$model->save();}返回 $model;}));}

i'm trying to figure out how to use/test the lockforupdate correctly, but i found is not function like what i expected

this is just testing

public function index() {
        return dd(DB::transaction(function() {
            if (Auth::guard('user')->check()) {
                $model = AppModelsUser::find(1)->lockForUpdate();
                sleep(60);
                $model->point = 100000;
                $model->save();
            } else {
                $model = AppModelsUser::find(1);
                $model->point = 999;
                $model->save();
            }

            return $model;
        }));
}

i try to test in 2 browser, browser 1 user logged in and browser 2 not logged in, browser 1 hit refresh, then there will lockforupdate and sleep 60 seconds before update

in the 60 seconds, i go browser 2 and hit refresh, however the record is not locked, i check phpmy***** and the record is updated(within the 60 seconds lock trigger by browser 1)

but after 60 seconds, the record has been modified again by browser 1(Point 100000)

so am i misunderstanding the lockforupdate is use for?or i test it incorrectly?

what i expected is the row shouldn't be modified by browser 2 in the first 60 seconds(blank page with loading favicon or error throw?)

https://laravel.com/docs/5.2/queries#pessimistic-locking

and i did some research but still cannot understand what different between sharedLock(LOCK IN SHARE MODE) and lockForUpdate(FOR UPDATE)

btw i confirmed the database is innodb

解决方案

This work, finally, but still don't understand what sharedLock(LOCK IN SHARE MODE) and lockForUpdate(FOR UPDATE) different

    public function index() {
        return dd(DB::transaction(function() {
            if (Auth::guard('user')->check()) {
                $model = AppModelsUser::lockForUpdate()->find(1);
                sleep(30);
                $model->point = 100000;
                $model->save();
            } else {
                $model = AppModelsUser::lockForUpdate()->find(1);
                $model->point = $model->point + 1;
                $model->save();
            }

            return $model;
        }));
    }

软件
前端设计
程序设计
Java相关