-->
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.  [ 4 posts ] 
Author Message
 Post subject: What happens with Hibernate session after a rollback?
PostPosted: Tue Mar 20, 2007 11:53 am 
Newbie

Joined: Mon Aug 07, 2006 7:14 pm
Posts: 16
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

[b]Hibernate version:3.1.1[/b]


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

Hi all,
I have a problem regarding the using of the same Hibernate session across transactional methods.
I thinks this issue is related to another one posted in Jira: "HHH-511"

When occurs an exception in a transactional method, the Hibernate session associated seems to be cleared, then when is executed another method with the same session and it tries to save an object that was in the session after the exception ocurrencie, the following exception is triggered: "org.springframework.orm.hibernate3.HibernateSystemException: Found two representations of same collection: org.appfuse.model.User.roles; nested exception is org.hibernate.HibernateException: Found two representations of same collection: <<collectionName>> "

For example:
BeanA
method {
User u = UserDao.getUser(1l);
for (int i=0;i<=1;i++) {
try {
beanB.transactionalMethod(u);
}catch (RuntimeException e) { System.err.println("RuntimeException ="+e);}
}
}

BeanB
transactionalMethod(User u) {
...
userDao.save(u);
}

Assuming that BeanA.method and BeanB.transactionalMethod are transactional and they are using the same Hibernate session, if an exception is triggered in the first call to transactionalMethod, then in the second call that method an exception will be triggered in userDao.save(u).
This exception is the commented above, and it happens because in the first method the hibernate session is cleared at the moment of the rollback, so it seems to have no entities and collections associated, but the exceptions is telling us another thing. This seems contradictory and it looks like Hibernate is not handling correctly this situation.
I was able to avoid this exceptions using session.refresh(u) before saving the user object, but I think that Hibernate should be handling this transparently.

Anybody Know something regarding this issue?
Any ideas will be very helpful.

Thanks to all.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 21, 2007 12:48 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
The hibernate docs are very clear on this subject. Sessions are not to be used after a HibernateException has been thrown. Session.clear() will not restore them to a usable state. The only correct action is to release the session and get a new one.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 21, 2007 2:34 pm 
Newbie

Joined: Mon Aug 07, 2006 7:14 pm
Posts: 16
Ok, but I'm not talking about a HibernateException, any RuntimeException triggered in a transactional method triggers a rollback and the issue explained happens.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 21, 2007 3:41 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Any exception thrown by Hibernate, be it a HibernateException or any other RuntimeException. You need to catch RuntimeException and discard the Session.

Do not use the Spring stuff.

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


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