views:

20

answers:

3

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.

A: 

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.

Centurion
He doesn't have tables.
NullUserException
A sorry, then pass this response
Centurion
+1  A: 

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.

André Hoffmann
+2  A: 

I am currently just working with plain-text files.

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.

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

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

S.Lott