views:

58

answers:

4

Hi everyone,

I have seen a feature in different web applications including Wordpress (not sure?) that warns a user if he/she opens an article/post/page/whatever from the database, while someone else is editing the same data simultaneously.

I would like to implement the same feature in my own application and I have given this a bit of thought. Is the following example a good practice on how to do this?

It goes a little something like this:

1) User A enters a the editing page for the mysterious article X. The database tableEvents is queried to make sure that no one else is editing the same page for the moment, which no one is by then. A token is then randomly being generated and is inserted into a database table called Events.

1) User B also want's to make updates to the article X. Now since our User A already is editing the article, the Events table is queried and looks like this:

|   timestamp   |   owner   |   Origin      |   token      |
------------------------------------------------------------
|   1273226321  |   User A  |   article-x   | uniqueid##   |

2) The timestamp is being checked. If it's valid and less than say 100 seconds old, a message appears and the user cannot make any changes to the requested article X:

Warning: User A is currently working with this article. In the meantime, editing cannot be done. Please do something else with your life.

3) If User A decides to go on and save his changes, the token is posted along with all other data to update the database, and toggles a query to delete the row with token uniqueid##. If he decides to do something else instead of committing his changes, the article X will still be available for editing in 100 seconds for User B

Let me know what you think about this approach!

Wish everyone a great weekend!

+2  A: 

Yeah, that's great and should work fine.

In addition, I'd add the possibility for user B to break the lock - if that's at all wanted!

That is, the possibility to replace A's lock by B's. This way, you could avoid the time restraint, and they would see 'Hey, this is being edited by A, and this lock is XXX seconds/minutes old. Do you want to break this lock?'.

With nice users (i.e. no malicious admins), this approach may be better than having just 100 seconds to edit something - sometimes you just need more time.

Seb
Hi Seb, that's a great idea that I really should implement. Thanks man!
Industrial
+1  A: 

Sounds like it will work fine. If you want to denormalize this and remove the extra Events table, just add a UserId and Timestamp field to the Articles table, as that is all you really need.

You can easily check if the UserId doesn't match and if the Timestamp is less than 100 seconds old, then show the message.

This way, you won't have to do any deletions on a separate table.

John Rasch
+1  A: 

I'd just add that you could have an AJAX query fire every minute or so if something has been done on the page to update the timestamp.

Sid
+1  A: 

Does editing an article always take less than 100 seconds ?

Erwin Smout
Well no, the delay of 100 seconds were just an example, so that's might be a bit unrealistic on a complex update of an article. How would you do it?
Industrial
It doesn't really matter whether you took 100 seconds as "just an example". The point is : there does not exist any single such time lapse of which you can safely say "well after this lapse of time i can be positively sure that that editor is no longer really editing this article" (AND that will free the article within reasonable time for editing to subsequent editors who DO want to do some real work on it).What you want to do bears much resemblance to "leaving locks pending while a transaction is awaiting user input". And that is considered a very poor design technique.
Erwin Smout
Seb suggested a possibility of "lock breaking", that is, the possibility for B to overrule A's lock without explicitly requiting A's consent.You can do that, but then in some cases, things might turn into kind of a boxing competition between competing users. Allowing third parties to just "break up" something that I am doing without even notifying me, isn't exactly the politest solution either.
Erwin Smout
Hi Erwin, this would be possible to implement in a more advanced way with Jquery with continuously sending an AJAX call that updates the timestamp if the user's still editing and still active. There's some great plugins out there to detect inactivity from a user which might come handy for this! Thanks a lot for your thoughts!
Industrial