PHP / MySQL / jQuery悲观锁定记录

时间:2021-06-25 06:51:31

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.

在我的情况下,我不确定乐观锁定是否可靠,因为记录是通过AJAX请求保存的。我正在考虑应用某种悲观锁定;使用两个字段,例如locking_user_id和locking_timestamp,我可以跟踪打开记录的人和上次打开记录的人。

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

但是,由于用户可能一次打开几个小时,我怎么知道用户是放弃它还是只是努力工作?我不想强迫他们每5分钟更新一次〜但这可能是一种可能性(AJAX每5分钟保存一次)。

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?

也许jQuery进程可以在用户工作时进行计数,并且每5分钟发出一次AJAX请求(getJSON)来更新locking_timestamp。这样我就可以保持谁在制作唱片。在时间戳变为“旧”之后,我可以假设用户不再使用reocrd。有没有人有这种锁定的经验?

3 个解决方案

#1


3  

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

使用AJAX请求更新locking_timestamp是一个很好的策略,并且可以正常工作。

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

但是,电话间隔5分钟对我来说似乎有点长。除非你有非常高的流量,否则从用户的角度来看,30秒就会更好。想象一下,有人必须等待5分钟,因为另一个用户打开了记录,然后关闭了他的浏览器......

#2


1  

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

也许我很傻但是如何让jQuery监控页面上的鼠标和键盘事件。

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

PHP将锁定放在文件系统中的某个位置或数据库条目上的某个标志位,这样下一个用户将无法编辑,只能查看信息。该文件将包含条目中的用户信息,因此当发送更改时,只接受其更改

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分钟的用户没有做任何事情,他将被提示确认他仍在使用它,否则他将在3分钟内失去当前的变化。如果他确认没有任何反应,但如果他没有Ajax将取消锁定。

#3


1  

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

如果你说用户可能需要几个小时才能提交记录,那么一个简单的5分钟锁定或任何不足够的。

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.

相反,为什么不实施一种签入/签出功能,该功能将在编辑记录时激活(签出)并定期“保持活动”,并在记录时停用(签入)已保存/会话已过期/浏览器已关闭/等

#1


3  

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

使用AJAX请求更新locking_timestamp是一个很好的策略,并且可以正常工作。

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

但是,电话间隔5分钟对我来说似乎有点长。除非你有非常高的流量,否则从用户的角度来看,30秒就会更好。想象一下,有人必须等待5分钟,因为另一个用户打开了记录,然后关闭了他的浏览器......

#2


1  

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

也许我很傻但是如何让jQuery监控页面上的鼠标和键盘事件。

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

PHP将锁定放在文件系统中的某个位置或数据库条目上的某个标志位,这样下一个用户将无法编辑,只能查看信息。该文件将包含条目中的用户信息,因此当发送更改时,只接受其更改

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分钟的用户没有做任何事情,他将被提示确认他仍在使用它,否则他将在3分钟内失去当前的变化。如果他确认没有任何反应,但如果他没有Ajax将取消锁定。

#3


1  

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

如果你说用户可能需要几个小时才能提交记录,那么一个简单的5分钟锁定或任何不足够的。

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.

相反,为什么不实施一种签入/签出功能,该功能将在编辑记录时激活(签出)并定期“保持活动”,并在记录时停用(签入)已保存/会话已过期/浏览器已关闭/等