Prevent users from rewriting each other

I want two users to accidentally overwrite each other when updating a record. That is, two users load a page with an A record on it. The user updates the record to AB, and the second user updates it to AC.

I do not want the latter to hit the database in order to override it. I need a mechanism to say that the record has been updated so that your data cannot be saved.

Now the two ideas that I have are time to seal the notes and check this out. If this does not match, do not allow the update. The second way: GUID records every time an update is performed, check the GUID and if it does not match, do not update.

Are these methods valid, if so, which is better. If not, what do you offer. This is in C # if it matters

thanks

+4
source share
6 answers

The two methods you mentioned are effectively equivalent - one way or another, you have a unique identifier for “record during verification”. I have no concrete view of which is better, although the timestamp obviously gives you the added benefit of data about when the record was valid.

An alternative is to remember the old values ​​and only allow the update if they match - thus, if user A started editing the record, then user B logs in and changes something, then changes it, the user A edits are still valid.

The term for these methods is an optimistic lock (or an optimistic concurrency control ).

+11
source

In fact, there is a third method. To perform the update, issue the update instruction for this form:

UPDATE table SET update_field = new_value WHERE db_pk = my_pk // assume primary key immutable AND update_field = original_field_value 

where original_field_value is the value of the field before trying to update. This update will fail if someone else has changed update_field , unless they have changed it to the same value that you have.

+5
source

You describe an optimistic blocking, a valid and useful technique.

See links here.

+4
source

Any method is valid for verification.

In terms of what is best, you need to look at the size of your application and how long it will take to implement each of them. So if this happens only occasionally, I would try to find a faster solution and implement the timestamp parameter.

If you want something more detailed from google concurrency - this is an article to get you started - concirrency

+2
source

I am using the first option. Update the timestamp with every update. Therefore, during the update, we check the equality of the timestamp.

+1
source

Make terms with optimistic and pessimistic locking of the bell. These are two recognized approaches to the problem you are describing. It looks like you are working in a web environment. In this case, the previous option is more preferable (optimistic blocking). You indicated how this will be implemented. Typically, a time stamp or version number is used to check if a record has been updated since it was restored. Another thing to keep in mind is to tell your users that there has been a change in the underlying data and possibly give them the opportunity to choose between what they tried to save and what was saved by another user. This choice really depends on the business rules.

0
source

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


All Articles