Laravel lockforupdate(悲观锁定) [英] Laravel lockforupdate (Pessimistic Locking)

查看:1195
本文介绍了Laravel lockforupdate(悲观锁定)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这只是测试

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

            return $model;
        }));
}

我尝试在2个浏览器中进行测试,浏览器1的用户已登录,浏览器2的未登录,浏览器1刷新后,将出现lockforupdate并在更新前60秒钟进入睡眠状态

在60秒内,我进入浏览器2并单击刷新,但是记录未锁定,我检查了phpmyadmin并更新了记录(在浏览器1触发的60秒内锁定)

但在60秒后,该记录已被浏览器1(点100000)再次修改

所以我误解了lockforupdate的用途?还是我测试不正确?

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

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

我做了一些研究,但仍然无法理解sharedLock(在共享模式下锁定)和lockForUpdate(FOR UPDATE)之间有什么区别

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

解决方案

这项工作终于完成了,但仍然不知道sharedLock(共享模式下的锁定)和lockForUpdate(FOR UPDATE)有什么区别

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

            return $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 = \App\Models\User::find(1)->lockForUpdate();
                sleep(60);
                $model->point = 100000;
                $model->save();
            } else {
                $model = \App\Models\User::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 phpmyadmin 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 = \App\Models\User::lockForUpdate()->find(1);
                sleep(30);
                $model->point = 100000;
                $model->save();
            } else {
                $model = \App\Models\User::lockForUpdate()->find(1);
                $model->point = $model->point + 1;
                $model->save();
            }

            return $model;
        }));
    }

这篇关于Laravel lockforupdate(悲观锁定)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆