Just in case someone comes across this thread... I never did find an answer. I created my own solution that prevents one person from editing a record that is already being edited by someone else.
How properly use the lockForUpdate() method?
The L5 docs says that it is possible to lock an individual record in a table, in anticipation that this record will be updated.
Here are links related to this feature:
- http://laravel.com/docs/5.0/queries#pessimistic-locking
- https://dev.mysql.com/doc/refman/5.0/en/select.html
- https://github.com/laravel/framework/blob/5.0/src/Illuminate/Database/Query/Builder.php#L1278-L1286
My question: what is the proper way to use this method?
-
Should ~~~DB::table('users')->where('votes', '>', 100)->lockForUpdate()->get();~~~ be wrapped in a transaction?
-
how is the record unlocked?
-
does MySQL have an automatic timeout, so if an hour (or whatever the default timeframe might be) passes, the record is automatically unlocked?
The MySQL doc says:
If you use FOR UPDATE with a storage engine that uses page or row locks, rows examined by the query are write-locked until the end of the current transaction. Using LOCK IN SHARE MODE sets a shared lock that permits other transactions to read the examined rows but not to update or delete them. See Section 14.2.8.3, “SELECT ... FOR UPDATE and SELECT ... LOCK IN SHARE MODE Locking Reads”.
Does this mean we should:
DB::beginTransaction();
DB::table('users')->where('votes', '>', 100)->lockForUpdate()->get();
UPDATE the record;
DB::commit();
Thank you for your help.
Please or to participate in this conversation.