-->
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.  [ 1 post ] 
Author Message
 Post subject: Session.get with LockMode.UPGRADE throws StaleObjectStateExc
PostPosted: Wed May 10, 2006 5:37 pm 
Newbie

Joined: Wed May 10, 2006 5:15 pm
Posts: 1
We are using managed versioning for optimistic locking.

In one case, we were hoping to apply a pessimistic locking strategy that would block one thread from even reading the object until the other thread finished commiting the changes in its transaction (thereby allowing us to apply both changes to the object without overwriting). I thought we could achieve that with the following code, which I understand would lock the database row for writing AND for reads peformed with a "select for update" query:

InstanceClass ic = session.load(InstanceClass.class, 15327l, LockMode.UPGRADE);
ic.setSomething(true);
...
tx.commit();

However, it looks like Hibernate adds the version number to the generated "select for update" statement that results from the session.load():

'select id from instance_class where id =? and version =? for update' with bind parameters: {1=15327, 2=1}

This causes me to occasionally get StaleObjectStateExceptions when the second thread is executing the session.load(). Is this version number coming from the cache? Even if the pessmistic locking was working as expected at the DB level, it seems this Exception will be thrown simply because I don't know the most recent version number when I execute the query.

I'm not sure I understand how to stop this version checking. I assumed that a database-level pessimistic lock would keep the second thread from retrieving the data until the version changed due to the first thread's commit, and this would solve our problem.

Is there any way to do this with pessimistic locking, or will I simply have to catch the StaleObjectStateException and retry?

Hibernate Version:
3.1.2

Oracle Version:
10.2.0.1.0


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

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.