Prevent editing of the same data by two users

I saw a function in various web applications, including Wordpress (not sure?), Which alerts the user if he opens an article / message / page / regardless of the database, and someone else is editing the same data at the same time.

I would like to implement the same function in my application, and I thought a little. Is the following example a good practice on how to do this?

This is a bit like this:

1) User A goes to the edit page for the cryptic article X. The Events database table has been programmed to ensure that no one else is editing the same page at the moment, which no one belongs to. The token is then randomly generated and inserted into a database table called Events .

1) User B also wants to make updates for article X. Now that our User A already editing the article, the Events table is requested and looks like this:

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

2) The timestamp is checked. If it is valid and less than 100 seconds, 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 continue and save his changes, the token is sent along with all the other data to update the database and switches the request to delete the row using the uniqueid## token. If he decides to do something else instead of making his changes, article X will still be editable in 100 seconds for User B

Let me know what you think of this approach!

Wish everyone a great weekend!

+4
source share
4 answers

Does editing an article always take less than 100 seconds?

0
source

Yes, it’s great and should work well.

In addition, I would add the ability for user B to break the lock - if at all!

That is, the ability to replace the lock with B. Thus, you can avoid temporary restraint, and they would see "Hey, this is edited by A, and this lock is XXX seconds / minutes. Do you want to break this lock?".

With pleasant users (i.e. without malicious administrators) this approach may be better than having just 100 seconds to edit something - sometimes you just need more time.

+3
source

Everything seems to be fine. If you want to denormalize this and delete the extra Events table, just add the UserId and Timestamp field to the Articles table, as that’s all you really need.

You can easily check if UserId does not match, and if Timestamp less than 100 seconds, then show the message.

This way you do not need to do any deletions in a separate table.

+2
source

I would add that you could fire AJAX every minute or so if something was done on the page to update the timestamp.

+2
source

Source: https://habr.com/ru/post/1309155/


All Articles