PHP/MySQL/jQuery Pessimistic Locking of Record

左心房为你撑大大i 提交于 2019-12-22 09:08:12

问题


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?


回答1:


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...




回答2:


Perhaps I'm being silly but how about having jQuery monitoring mouse and keyboard events on the page.

PHP places lock somewhere in filesystem or some flag on database entry so the next user will not be able to edit but just view the information. The file would contain the user information in the entry so when changes are sent only his changes are accepted

While the first user has activity on the page nothing happens. 3 minutes of the user not doing anything and he will be prompted to either confirm that he still is working on it or he will within 3 minutes loose his current changes. If he confirms nothing happens but if he does not Ajax will remove the lock .




回答3:


If you say users could take several hours before submitting a record, then a simple 5 min lock or whatever wouldn't be sufficient.

Instead, why don't you implement a sort of check in/ check out feature which would be activated (checked out) on the edit of a record and "kept alive" at regular intervals, and deactivated (checked in) when the record was saved/ session expired/ browser closed/ etc.



来源:https://stackoverflow.com/questions/15012314/php-mysql-jquery-pessimistic-locking-of-record

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!