Somedeveloper
Somedeveloper

Reputation: 867

Allow for Database changes

can anyone tell me approaches to tackling the following which is a very typical use case.

User 1 is editing page 1 which is modifying a users roles etc. User 2 is also modifying the same user as user 1. How do i prevent the changes overwriting one another???

this is for asp.net site with framework 3.5???

thanks Niall

Upvotes: 1

Views: 48

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062745

Optimistic concurrency is very common here, often using the ROWVERSION (aka TIMESTAMP in SQL server) data type. Most ORMs will handle this automatically (detecting the presence of a ROWVERSION/TIMESTAMP) - otherwise you will need to handle this in your code.

At the simplest level; you keep hold of the ROWVERSION you fetched when you fetched the record for editing, and check it when you attempt to save. If it matches, the save is committed; otherwise you throw an exception (the second edit loses).

Note you can (as an alternative) check each column during the update; this is more granular, but more costly (and it doesn't fully cope with some scenarios with inter-dependent properties).

Upvotes: 4

Related Questions