Search code examples
phpjqueryajaxoptimistic-lockingpessimistic-locking

PHP/MySQL/jQuery Pessimistic Locking of Record


I've been thinking about developing some simple record locking for an application I'm involved in. There are a few users who will take literally hours to complete an edit of a record. This causes issues when someone else wants to make a change to the record. Currently there is no locking involved.

I'm not certain that Optimistic locking is reliable in my case, as the record is saved thru an AJAX request. I'm looking at applying some kind of Pessimistic Locking; using two fields, such as, locking_user_id and locking_timestamp, I can track who has the record open and the last time it was opened.

But, since the user may have it open for hours at a time, how can I know if the user abandoned it or is just working hard on it? I don't want to force them to update it every 5 minutes ~ yet that might be a possibility (AJAX save every 5 min).

Perhaps a jQuery process could be counting while the user works and would fire off an AJAX request (getJSON) every 5 minutes to update the locking_timestamp. That way I could maintain who is working on the record. After the timestamp gets "old", I can assume the user is no longer working with the reocrd. Has anyone had experience with this kind of locking?


Solution

  • Updating the locking_timestamp with AJAX requests is a fine strategy and will work out fine.

    However, 5 minutes interval between the calls seem a little long to me. Unless you have really high traffic, something like 30 seconds would be better from a user's perspective. Imagine someone having to wait 5 minutes because another user opened the record and then closed his browser...