I am creating a web application which has a few management screens, where a user can go to edit records (for example, changing the contact details of a user). Access to these managements screens are controlled by roles, with multiple users possibly having access. The problem now arises of what to do if two users simultaneously try to edit the same record.
My problem is with the front-end, not with the back-end. What are some of the patterns I can use in order to design my pages so as to be both user-friendly and prevent concurrent modifications? The only two options I can think of are these:
Any suggestions?
How about a timed reservation? This is a model used in many on-line booking systems.
An example based on trivial single table case (I hope that extending to more tables should be obvious) Add a column to the table called RESERVATION_TIME. All records are initially populated with a reservation time of "many years ago".
Use this in conjunction with optimistic locking. At the point you go into Edit mode you
Make a reservation for the record you want
UPDATE RESERVATION_TIME to NOW where KEY = id and RESERVATION_TIME more than 30 mins ago
This will only work if no one has already set the up update to recently. The idea is that we don't even allow the edit screen to be populated if some other user is already working. But we set that reservation to expire after 30 mins (or whatever) so that nothing is "locked" permenently. But note that we are not (in database terms) holding a pessimistic lock.
Now retrieve the data (maybe inn the same tran as took the reservation)
on write back check the optimistic predicate and clear the reservation back to a long time ago.
Now this mediates between two users of any applications that honour the reservation system. The optimistic lock mediates between all optimistic users of the system, so it's still possible for the user to be irritated, but assumig that out-of-band updates are rare then you should get acceptable usability.
Some other ideas:
We implement a first-write-wins mechanism in similar situations (or Optimistic Locking)
BACKEND:
Create a timestamp field in the database (this is automatically updated in MSSQL on an INSERT and UPDATE)
When you load the objects for editing, including a timestamp property, only allow saving in your stored procedures if the timestamp is the same else throw an error, handle this in your application indicating the record has changed and give them the opportunity to re-load the page. This works well on the web/disconnected environments.
FRONT END
The page needs to poll the database (using ajax) to detect a change. If there is a change prompt the user indicating the record has changed and option to reload/merge. On the page postback display labels or secondary form fields (readonly) with the new entered values on the right that can be copied, eg arrow button pointing left that copies the values back to the original form fields.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With