数据更新冲突
我目前正在开发一个网络应用程序,但遇到了一些问题。在此系统中,多个用户可以登录同一页面并更新数据(一系列复选框、下拉列表和文本字段)。
问题是,如果一个用户已经在加载旧数据的页面上,并且此后已更新,并提交其更改(这会更新所有内容),则数据可能会被覆盖。
关于如何解决这个问题有什么建议吗?我目前只处理纯文本文件。
I am currently working on a web application where I have encountered a little problem. In this system, multiple users can log onto the same page and update the data (a series of checkboxes, dropdowns, and text fields).
The issue is that data might get overwritten if one user was already on a page where old data was loaded, and has since been updated, and submits their changes, which update everything.
Any suggestions on how to solve this problem? I am currently just working with plain-text files.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
建议1.使用数据库。
建议2.使用锁定文件。使用操作系统级 API 调用来打开具有独占属性的文件
锁。第一个获取该文件的用户拥有该数据的独占访问权。当那个
用户完成事务,关闭文件,释放操作系统级锁。
建议 3. 不要“更新”文件。记录更改的历史记录。然后,您可以从日志中读取用户名和时间戳以查找最新版本。
如果你这样做,你需要让每个请求都做这样的事情。
获取当前状态时,读取文件的最后一行。另外,获取文件大小和上次修改时间。保留会话中的大小和上次修改时间。在表单中显示当前状态。
处理用户的更改时,检查文件大小和上次修改时间。如果该文件与会话中记录的文件不同,则该用户正在尝试更新已被其他人更改的数据。读取文件的最后一行。另外,获取文件大小和上次修改时间。保留会话中的大小和上次修改时间。在表单中显示当前状态。
此外,您可能需要两个文件。一个包含“当前”数据,另一个包含更改的历史记录。这可以更快地查找当前数据,因为它是当前状态文件中的唯一记录。
另一种选择是在文件中包含一个“标题”,它是固定大小的文本块。每次追加时,您还会
seek(0,0)
并刷新标头,其中包含最后一条记录的偏移量以及上次更改的时间戳。Suggestion 1. Use a database.
Suggestion 2. Use a lock file. Use OS-level API calls to open a file with an exclusive
lock. The first user to acquire this file has exclusive access to the data. When that
user finishes their transaction, close the file, release the OS-level lock.
Suggestion 3. Don't "update" the file. Log the history of changes. You can then read usernames and timestamps from the log to find the latest version.
If you do this, you need to make each request do something like this.
When getting the current state, read the last line from the file. Also, get the file size and last modification time. Keep the size and last modified time in the session. Display the current state in the form.
When the user's change is being processed, check the file size and last modification time. If the file is different from what was recorded in the session, this user is attempting an update to data which was changed by someone else. Read the last line from the file. Also, get the file size and last modification time. Keep the size and last modified time in the session. Display the current state in the form.
In addition, you might want to have two files. One with "current" data, the other with the history of changes. This can make it faster to find the current data, since it's the only record in the current state file.
Another choice is to have a "header" in your file that is a fixed-size block of text. Each time you append, you also
seek(0,0)
and refresh the header with the offset to the last record as well as the timestamp of the last change.保存新数据时,您可以将数据修改日期与用户开始编辑的时间进行比较。
如果用户进行更改时发生了修改,您可以向用户显示一条消息,询问他们采用哪个版本或允许他合并两个版本。
这个问题已经通过版本系统(如 svn、git 等)以同样的方式解决了。
When saving new data you could compare the date that data has been modified with the time the user started editing.
If there have been modifications while the user was making changes you could then show a message to the user and ask them which version to take or allow him to merge the two versions.
This problem has been addressed by revision systems, like svn, git, etc. in the very same fashion.
您可以创建一个附加表,并在其中存储所有信息以及用户 ID,这样您就可以使用联接来访问用户插入的所有数据。
You can make an additional table, and store there all information as well as userID, so you will be able to access using joins all data that users inserted.