問題描述
我正在嘗試弄清楚如何正確使用/測試 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(悲觀鎖)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!