-->
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.  [ 3 posts ] 
Author Message
 Post subject: Many transactions in a single session - rollback fails
PostPosted: Wed Nov 17, 2004 11:34 am 
Newbie

Joined: Wed Nov 17, 2004 11:07 am
Posts: 2
I want to perform a number of transactions in a row. All of which share common objects. I reuse one session for all of them to avoid loading an object in one session and updating it in another one. This is the code :

Code:
...
Transaction transaction = session.beginTransaction()
try {
     ...
     session.saveOrUpdate(object);
     ...
     transaction.commit();
} catch (Exception e)
{
     transaction.rollback();
}
....


This piece of code is executed a couple times for different objects and transactions but the same session. The problem I am having is that after a rollback the session is neither cleared nor flushed and thus the session's first-level cache still contains updates and insert statements. These will be executed when the session is flushed next. I tried to fix this by flushing the session before the rollback. Someone suggested to clear the session instead. But I am not sure if that is a good idea because it seems like the same as creating a new session for every transaction.

What is the appropriate pattern in this scenario?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 17, 2004 11:37 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
As documented, a Session can not be used after it has thrown an exception. You should ask if your whole unit of work should be wrapped inside a single transaction, instead of one transaction per insert/update call (its not clear if you really do this, it wouldn't be good).

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 17, 2004 11:51 am 
Newbie

Joined: Wed Nov 17, 2004 11:07 am
Posts: 2
It is not the hibernate code that throws the exception but other code encountering some business error. It is also not necessarily just one update or save in every transaction. I just tried to keep the explanation simple.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.