-->
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: optimistic locking application workaround
PostPosted: Thu Jan 24, 2008 10:32 am 
Newbie

Joined: Wed Mar 08, 2006 8:11 am
Posts: 17
Location: Israel
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.1.3

I am using optimistic locking with a Version column (Integer) in my hibernate-enabled application.

I am trying to apply some logic for the case the user changed a list of objects (in a GUI table, for example), and tries to save them all. In case one of the objects has already changed in the database, I want to catch the exception, find the object that changed in the db, and try saving all the other objects again. I tried doing that by catching the StaleObjectState exception, and then doing a session.evict(obj) on the object (I got it from the exception's members). It didn't work - after evicting I tried querying again for another object on the same table, which should cause a session flush, and the flush caused the update command to be sent again, and the StaleObjectState was raised again for me.
My test looks something like that:

1. read object1
2. read object2
3. change data in object1
4. change data in object2
5. stop in debug (I know it's crude, but I was just playing around to see how to get it to work before I started anything automatic), and change object1 version in the database directly.
6. try to read object3 <-- this causes the first exception, which is understandable.
7. find object1 form the session, using e.getPersistentClassName() and e.getIdentifier.
8. evict object1 from the session
9. read object3 again <-- this should have caused the update for object2 to be performed, but it causes the object1 update to be performed again, and the exception is raised again

what am I doing wrong? how can I make sure the changes are saved to the database, for all the objects that were not changed by some other user before, without opening a different transaction for each object?

Thanks for your input.
Noam Gal.


Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html


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.