当另一个用户正在编辑记录时阻止编辑该记录 - 如何操作?

发布于 2024-09-11 20:57:51 字数 1143 浏览 6 评论 0原文

我正在尝试向站点添加记录锁定。这不是数据库锁定,即我有 2 个进程同时编辑同一条记录,而是 2 个用户同时编辑同一条记录。情况如下:

我需要能够在一个人正在编辑记录时锁定该记录,以便其他人可以同时对记录进行更改(因此会造成混乱)。可能发生的情况是用户 A 开始编辑记录。同时(在用户 A 保存记录之前),用户 B 开始编辑记录。现在用户A保存field_a值为“0001”的记录。现在,用户 B 正在查看并编辑该记录的过时版本。然后用户B保存值为“0002”的记录。数据库最终的值为 0002。

我可以理解发生了什么,但用户很难弄清楚数据库中的值将是 0002 而不是 0001。在我的例子中,他们使用这个数字来关联将在线数据库中的记录记录到另一条记录。发生这种情况时,他们最终会在另一个系统中得到 2 条记录,而在线系统中则得到 1 条记录。

我的想法是通过将用户的 ID 放入字段来“锁定”记录。当为 0 时,未锁定。 >0 被锁定。

以下是我对事情如何运作的想法:

  • 当用户单击记录上的编辑时,他们将收到该记录的锁定。
  • 他们的锁定时间最长为 X 分钟(可能是 30 分钟)。出现这种情况的原因是因为单击了后退按钮而不是保存或取消。
  • 一旦他们保存,记录就会被解锁。
  • 要取消锁定而不保存,他们需要单击编辑页面上的“解锁”(或类似按钮)。我会取消,但我希望他们能够取消并仍然保留锁。
  • 在记录列表中,我将包含锁定记录的人的姓名,以便每个人都知道。
  • 管理员将能够解除记录的锁定,以防万一它全部崩溃。
  • 在每个页面上添加一个区域,以警告用户完成操作后需要解锁当前记录,或者只是在每个页面上显示他们已锁定的记录数。
  • 添加一个脚本来检查已锁定超过 30 分钟的记录并解锁它们(可能会向锁定记录的用户发送电子邮件)。

我想知道是否有任何我遗漏的案例会导致悲伤,或者您对如何做到这一点有任何建议吗?

注意:我正在使用 PHP 和 MySQL,它是一个 Web 应用程序。我也不认为数据库中的记录锁定是我所需要的。

-- 添加:Dan Hulton 的建议是添加一个日期字段,用于存储上次检查的时间出去。这将消除检查过期结帐的脚本。相反,系统只会在列出或限制记录编辑时检查此日期。

I'm trying to add record locking to a site. This isn't database locking where I have 2 processes editing the same record at the same time, but instead 2 users editing the same record at the same time. Here's the situation:

I need to be able to lock a record while 1 person is editing it so someone else can make changes to record at the same time (and therefore cause confusion). What can happen is user A starts editing a record. In the mean time (before user A saves the record), user B starts editing the record. Now user A saves the record with field_a having a value of "0001". Now user B is seeing and editing an out of date version of the record. Then user B saves the record with a value of "0002". The database ends up with a value of 0002.

I can understand what happened, but it's hard for a user to figure out that the value in the DB will be 0002 instead of 0001. In my case, they are using this number to associate the record in the online database to another record. When this happens, they'll end up with 2 records in the other system for 1 record in the online system.

My thought is to "lock" a record by putting the user's id in field. When 0 it's not locked. >0 is locked.

Here are my thoughts on how things would function this:

  • When a user clicks the edit on a record, they will receive the lock for the record.
  • They will have this lock for a maximum of X minutes (probably 30). The reason for this is because of clicking the back button instead of save or cancel.
  • Once they save, the record will be unlocked.
  • To remove the lock without saving, they will need to click the "unlock" (or similar) on the edit page. I would do cancel, but I want them to be able to cancel and still hold onto the lock.
  • On the list of records, I'll include the name of the person who's locked the record so everyone will know.
  • Admins will be able to remove the lock for a record, just incase it all falls apart.
  • Add an area on every page to warn the user that either the current record needs to be unlocked when they are done or just displaying on every page the number of records they have locked.
  • Add a script that checks for records that have been locked for more than 30 minutes and unlock them (possibly sending an email to the user that locked the record).

What I'm wondering is if there are any cases I'm missing that are going to cause grief or if you have any suggestions on how to do this?

Note: I am working in PHP and MySQL and it's a web application. I also don't think record locking in the database is what I need.

-- Addition: a suggestion from Dan Hulton was to add a date field which would store the last time it was checked out. This would eliminate the script to check for expired checkouts. Instead the system would just check this date when listing or restricting editing of the records.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半山落雨半山空 2024-09-18 20:57:51

好吧,您需要一些锁定方案。对于您所说的这种事情,人们经常使用“乐观锁定”,其工作原理如下:

  • 当有人打开文件进行编辑时,将版本号与适当的记录相关联
  • ,跟踪版本号
  • < p>当有人尝试保存文件时,请检查版本号。如果版本号与您检索到的版本号相同,则保存,然后增加版本号但是如果版本号与打开文件时保存的版本号不同,则存在冲突;当您没有查看时,其他人已更改记录。

  • 如果有冲突,您会要求用户以某种方式解决它。

(这称为“乐观”,因为您假设大多数情况下不会有两个用户尝试同时更新。)

悲观版本是有一个锁的字段。当有人打开记录进行编辑时,检查该字段;如果为 0,则可以编辑,但随后将 1 添加到该字段。保存时再减1。如果不为0,则拒绝用户编辑。

Well, you need some locking scheme. For the kind of thing you're talking about, people often use "optimistic locking", which works like this:

  • associate a version number with the appropriate record(s)
  • when someone opens the file for edit, keep track of the version number
  • when anyone attempts to save the file, check the version number. If the version number is the same as the one you retrieved, you save, and then increment the version number. BUT if the version number is not equal to the one you saved when you opened the file, you have a conflict; someone else has changed the record when you weren't looking.

  • If you have a conflict, you ask the user to resolve it somehow.

(This is called "optimistic" because you're assuming that most of the time you won't have two users trying to update at the same time.)

The pessimmistic version of this would be to have a field for the lock. When someone opens the record for edit, check that field; if it's 0, you can edit, but then add 1 to the field. When you save, subtract 1 again. If it's not 0, you refuse to let the user edit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文