-->
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.  [ 5 posts ] 
Author Message
 Post subject: session.close() in CMT enviroment
PostPosted: Mon Jun 12, 2006 8:33 am 
Newbie

Joined: Tue Apr 11, 2006 4:57 am
Posts: 17
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.1.3

Do I have to close the session, or the JTA does it for me?

If I close the session, the sessionFactory.getCurrentSession() will open a new session, or reopen the closed one? How much expensive it is?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 12, 2006 9:04 am 
Senior
Senior

Joined: Tue Mar 09, 2004 2:38 pm
Posts: 141
Location: Lowell, MA USA
When using JTA, the Session is closed automatically on commit. There is a danger in closing a session that is returned by getCurrentSession(). If you have 2 or more components involved in a transaction which load data via Hibernate. Suppose that component 1 loads data 1st and calls session.close(). When component 2 call getCurrentSession(), an exception will be thrown stating that the session was closed.

In short: when using a JTA managed SessionFactory let Hibernate do the closing.

Ryan-

_________________
Ryan J. McDonough
http://damnhandy.com

Please remember to rate!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 12, 2006 9:32 am 
Newbie

Joined: Tue Apr 11, 2006 4:57 am
Posts: 17
Thanks,
I was a bit confused, because some said that session object must always be closed at the end of the work, and "in a CMT enviroment you must close if for safety".
I think in case of HibernateException I have to call the session.close() haven't I?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 12, 2006 9:44 am 
Senior
Senior

Joined: Tue Mar 09, 2004 2:38 pm
Posts: 141
Location: Lowell, MA USA
Nope. When a RuntimeException is thrown in a CMT environment, the transaction is automaticaly rolled back. HibernateException is a RuntimeException and when the transaction is rolled back, the Session is closed. I can confirm this is true with Hibernate 3.1 under JBoss 4 and WLS 8.1. under Hibernate 2.x, you did have to close the session manually. This is no longer true for 3.1.

Ryan-

_________________
Ryan J. McDonough
http://damnhandy.com

Please remember to rate!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 13, 2006 6:40 am 
Newbie

Joined: Tue Jun 13, 2006 5:59 am
Posts: 4
Remember that hibernate 3.x in a CMT environment implements (and binds) transaction lifecycle interceptors, which are in charge of mantaining resource and session consistency across the different stages of a transaction.

According to the help doc Sessions and Transactions you don't need to close the session.

Guido Scalise


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