Laravel lockforupdate (Pessimistic Locking)(Laravel lockforupdate(悲观锁))
问题描述
我正在尝试弄清楚如何正确使用/测试 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 并刷新,但是记录没有锁定,我检查 phpmyadmin 并更新记录(在浏览器 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 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 = 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;
}));
}
这篇关于Laravel lockforupdate(悲观锁)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Laravel lockforupdate(悲观锁)
基础教程推荐
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 使用 PDO 转义列名 2021-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01