Hi, I am working on the Data implementation class and I am confused about the following situation: 1. Suppose User 1 is viewing some records. 1'.User 2 who is an admin is also viewing the same records. 2. User2 makes use of the update() method and updates a record. 2'.Immediately after that, User 1 books the same record.
Now, user 1 booked the record based on the data that was displayed but at the time of booking it was stale because user 2 had updated it. So how do we take care of this problem? Please help me.
The issue you describe is solved by performing "optimistic" locking:
- you read a record for display - just before updating, you grab a lock on the record - you reread the record and check if it changed in the meantime - if it changed, you don't update (you cancel your transaction) and throw some exception - else you update normally.
The advantage of optimistic locking is that it improves concurrency (records are locked for a minimum of time). Its drawback is that for any transaction you know that it's valid only when it ends. BTW, such a locking scheme is said "optimistic" because it's based on the *hope* that records won't change in the meantime most of the time.
Joined: May 28, 2004
thanks a lot, Phillippe. This was very helpful. I have one more doubt. I am planning to expose the "Book" functionality from the server (not the data class though). So, when the client calls server.book(...), should I send the original record data in the call so that book() method can compare the data and then update the record? Otherwise, the client has to use the low level Data class methods to do the booking.
Joined: Jun 02, 2003
So, when the client calls server.book(...), should I send the original record data in the call so that book() method can compare the data and then update the record?