多用户应用程序记录锁定 - 最好的方法?
我正在开发一个处理多个并发用户的 php/mysql 应用程序。 我正在考虑针对当前正在查看/编辑的记录进行锁定/警告时采取的最佳方法。
要避免的情况是两个用户查看记录,其中一个进行更改,然后另一个进行同样的操作 - 一个更改可能会覆盖前一个更改。
在最新版本的 WordPress 中,他们使用某种方法来检测这一点,但它似乎并不完全可靠 - 经常返回误报,至少根据我的经验。
我假设必须采用某种形式的 ajax 来“ping”应用程序并让它知道记录仍在查看/编辑(否则,用户可能只是关闭其浏览器窗口,然后应用程序如何知道这一点)。
我可以看到的另一个解决方案是在提交更新记录时检查上次更新时间,看看在此期间它是否已在其他地方更新 - 然后为用户提供继续或放弃自己的更改的选择。
也许我在解决方案方面找错了方向——人们实现这个(这一定是相当普遍的)要求的经验是什么?
I'm developing a php / mysql application that handles multiple simultaneous users. I'm thinking of the best approach to take when it comes to locking / warning against records that are currently being viewed / edited.
The scenario to avoid is two users viewing the record, one making a change, then the other doing likewise - with the potential that one change might overwrite the previous.
In the latest versions of WordPress they use some method to detect this, but it does not seem wholly reliable - often returning false positives, at least in my experience.
I assume some form of ajax must be in place to 'ping' the application and let it know the record is still being viewed / edited (otherwise, a user might simply close their browser window, and then how would the application know that).
Another solution I could see is to check the last updated time when a record is submitted for update, to see if in the interim it has been updated elsewhere - and then offer the user a choice to proceed or discard their own changes.
Perhaps I'm barking up the wrong tree in terms of a solution - what are peoples experiences of implementing this (what must be a fairly common) requirement?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会这样做:在编辑表单中存储上次修改的时间。 将提交时的时间与数据库中存储的时间进行比较。 如果它们相同,则锁定表,更新数据(以及修改时间)并解锁表。 如果时间不同,请通知用户并询问下一步。
I would do this: Store the time of the last modification in the edit form. Compare this time on submission with the time stored in the database. If they are the same, lock the table, update the data (along with the modification time) and unlock the table. If the times are different, notify the user about it and ask for the next step.
时间戳比较的好主意。 它的实施成本低廉,并且在生产中运行的成本也很低。 您只需要编写逻辑来向用户发送回状态消息,表明他们的写入/更新没有发生,因为有人抢先了他们。
也许可以考虑将每次更新的用户名存储在名为“LastUpdateBy”的字段中,并将其返回给更新被抢占的用户。 只是为用户带来一点好处。 从企业的角度来看,这很好,但也许在不合适的环境中。
Good idea with the timestamp comparison. It's inexpensive to implement, and it's an inexpensive operation to run in production. You just have to write the logic to send back to the user the status message that their write/update didn't occur because someone beat them to it.
Perhaps consider storing the username on each update in a field called something like 'LastUpdateBy', and return that back to the user who had their update pre-empted. Just a little nicety for the user. Nice in the corporate sense, perhaps not in an environment where it might not be appropriate.