Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop users overwriting each other

I'm wanting to stop two users accidently overwriting each other when updating a record. That is to say two users load a page with record A on it. User one updates record to AB and user two updates it to AC.

I don't just want the last to hit the database to override. I need a mechanism to say the record has been updated so yours can't be saved.

Now the two ideas I have is to time stamp the records and check that. If it doesn't match up don't allow the update. The second method is to GUID the record each time an update is performed, check the GUID and if it doesn't match don't update.

Are either of these methods valid, if so, which is best. If not, what do you suggest. This is in C# if it makes a difference

Thanks

like image 419
mat-mcloughlin Avatar asked Nov 29 '22 19:11

mat-mcloughlin


1 Answers

The two methods you've mentioned are effectively equivalent - either way you've got a unique identifier for "the record at the checkout time" effectively. I don't have any particular view on which is better, although a timestamp obviously gives you the added benefit of data about when the record was valid.

An alternative is to remember what the previous values were, and only allow an update if they match - that way if user A started editing a record, then user B goes in and changes something, then changes it back, user A's edits are still valid.

The term for these techniques is optimistic locking (or optimistic concurrency control).

like image 122
Jon Skeet Avatar answered Dec 18 '22 10:12

Jon Skeet