Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

need help on update functionality

 
Kumar Kam
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Philippe Maquet
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kumar,

Welcome to JavaRanch and this forum!

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.

Regards,

Phil.
 
Kumar Kam
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Philippe Maquet
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kumar,

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?


Yes, that's exactly what you should do.

Regards,

Phil.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic