-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: Optimistic locking
PostPosted: Tue Dec 02, 2003 6:55 am 
Newbie

Joined: Tue Oct 14, 2003 11:23 am
Posts: 8
I am trying to use an optimistic locking strategy. So I have the following steps:
1. Load an object from DB.
2. Get its ID.
3. Send ID to the client.
4. Client sends back ID and some new data.
5. Server site gets the object from the cache by its ID without hitting DB.
6. Get the old timestamp of the object.
7. Reload the object to get its new timestamp.
8. Compare the timestamps.
9. Update the object.

and the problem is I can't figure out how to implement steps 5-7. I don't even know if it's possible.
I can't send timestamps to the client and I don't hold references to the loaded objects on the server side, but I have the same Session object all the time (so the object must be in the cache, right?).

Could anyone enlight me on this issue?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 02, 2003 9:23 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Hibernate has a mapping tag to map a field as the optimistic locking field. In this case its <timestamp>. When you do the update hibernate will check the value against the database for you and throw an exception if the lock guard has changed. All automatically.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 02, 2003 9:59 am 
Newbie

Joined: Tue Oct 14, 2003 11:23 am
Posts: 8
david wrote:
Hibernate has a mapping tag to map a field as the optimistic locking field. In this case its <timestamp>. When you do the update hibernate will check the value against the database for you and throw an exception if the lock guard has changed. All automatically.


But what happen if I have an ID of the object but not the object itself? To update it I need to get it first from somewhere. The question is, if I have an ID of the object (but don't have a timestamp or version) and I loaded it before (and didn't evict it) is there any way to get it from the cache rather than from DB even if the data is stale?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 02, 2003 10:04 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
What exactly is your problem? If you do a Session.load(primaryKey), you will of course get the data from the cache. If you then try to save or update it and get a timestamp violation, you will get a StaleObjectException.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 02, 2003 10:10 am 
Newbie

Joined: Tue Oct 14, 2003 11:23 am
Posts: 8
gloeglm wrote:
What exactly is your problem? If you do a Session.load(primaryKey), you will of course get the data from the cache. If you then try to save or update it and get a timestamp violation, you will get a StaleObjectException.


Does the loaded object always stay in cache and the only way to remove it from there is to evict it?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 02, 2003 12:13 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
mikhail wrote:
Does the loaded object always stay in cache and the only way to remove it from there is to evict it?

Yes, either evict the object or close the session

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 02, 2003 12:25 pm 
Newbie

Joined: Tue Oct 14, 2003 11:23 am
Posts: 8
epbernard wrote:
Yes, either evict the object or close the session


Thank you!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.