-->
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.  [ 6 posts ] 
Author Message
 Post subject: Does rollback(); automatically close the session?
PostPosted: Wed Nov 12, 2008 6:56 pm 
Newbie

Joined: Wed Nov 12, 2008 6:41 pm
Posts: 6
Hi,

I was wondering whether session.getTransaction().rollback(); automatically closes the session in hibernate 3.3.1 GA?

I'm trying to close the session after the rollback in a finally block, as is recommended by the documentation, but I get an exception saying that the session is already closed. If I comment out the session.close() statement, the code runs fine, as shown below:


session.beginTransaction();
try
{
session.save(account);
session.getTransaction().commit();
session.close();

return ApplicationResponseEnum.ACCOUNT_CREATED;
}
catch (RuntimeException e)
{
session.getTransaction().rollback();
finally
{
//session.close();
}

TIA,
- Ole


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 13, 2008 3:49 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
There is a configuration option for this: hibernate.transaction.auto_close_session

See table 3.7 on this page: http://www.hibernate.org/hib_docs/v3/re ... ional.html


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 13, 2008 12:33 pm 
Newbie

Joined: Wed Nov 12, 2008 6:41 pm
Posts: 6
Hi,

Thanks for pointing that out. I'm running with Teneo, so I thought maybe Teneo set it. I tried adding hibernate.transaction.auto_close_session=true to my hibernate.properties, and now I'm getting more session is already closed exceptions from other areas of my code, so that rules out the configuration option.

Any other ideas?

Thanks again,
- Ole


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 13, 2008 12:43 pm 
Newbie

Joined: Wed Nov 12, 2008 6:41 pm
Posts: 6
Just in case a little more context is helpful. The test I'm running tries to insert a duplicate account, which triggeres a org.hibernate.exception.ConstraintViolationException. It also causes this to be logged:

2008-11-13 10:22:44,463 [main] ERROR org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session

Maybe hibernate automatically closes the session, if it can't synchronize it?

Thanks again,
- Ole


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 13, 2008 1:27 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The hibernate.transaction.auto_close_session should be set to false. Which seems to be the default since your problems got worse when setting it to true. But... take a look at your code again...

Code:
session.beginTransaction();
try
{
   session.save(account);
   session.getTransaction().commit();
   session.close();
   return ApplicationResponseEnum.ACCOUNT_CREATED;
}
catch (RuntimeException e)
{
   session.getTransaction().rollback();
}
finally
{
   //session.close();
}


If the transaction is committed successfully the next line calls session.close(), which is also called a second time in the 'finally' part.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 13, 2008 11:07 pm 
Newbie

Joined: Wed Nov 12, 2008 6:41 pm
Posts: 6
:-) Whoops - Thanks! I need to stop burning the midnight oil.

Thanks again,
- Ole


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.