-->
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: Hibernat Session.close
PostPosted: Tue Feb 28, 2012 6:51 am 
Newbie

Joined: Tue Feb 28, 2012 6:40 am
Posts: 3
Hi All,

I have situation where connection to hibernate session will be provided by some external code. Hibernate session will be flushed once the required work is complete. Commit/Rollback on the hibernate session will not be called. The external code which provided the connection will call commit/rollback on connection(Not hibernate session). The external code will close the connection.

Now all of this is working fine. Only concern I have is whether, Hibernate session.close is not called in this case. As per documentation session.close will close the connection. But this is not required, since the external code is going to close the connection. My only concern if I do not close the hibernate session, does hibernate maintain reference to the session and which may cause out of memory issue due to open sessions later on.
In other words , Is hibernate session.close is necessary to be called in case where connection closure is handled by some external code?


Top
 Profile  
 
 Post subject: Re: Hibernat Session.close
PostPosted: Tue Feb 28, 2012 7:32 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Hi rameshbende,

let us take a short look to how Session(Impl).close is implemented (Hibernate 3.6.2):

Code:
org.hibernate.impl.SessionImpl.java
...

public Connection close() throws HibernateException {
      log.trace( "closing session" );
      if ( isClosed() ) {
         throw new SessionException( "Session was already closed" );
      }
      

      if ( factory.getStatistics().isStatisticsEnabled() ) {
         factory.getStatisticsImplementor().closeSession();
      }

      try {
         try {
            if ( childSessionsByEntityMode != null ) {
               Iterator childSessions = childSessionsByEntityMode.values().iterator();
               while ( childSessions.hasNext() ) {
                  final SessionImpl child = ( SessionImpl ) childSessions.next();
                  child.close();
               }
            }
         }
         catch( Throwable t ) {
            // just ignore
         }

         if ( rootSession == null ) {
            return jdbcContext.getConnectionManager().close();
         }
         else {
            return null;
         }
      }
      finally {
         setClosed();
         cleanup();
      }
   }


1. The first 10 rows are regarding logging and statistics and are not releasing any resources anyway.
2. The next block is regarding childSessions which you probably are not using
3. return jdbcContext.getConnectionManager().close(); // not needed, as you state that connection closure is handled by external code
4. method setClosed() in the finally block just sets a boolean value, no resources are freed.
5. method cleanup() in the finally block just clears the persistence context, this you can do also manually



Conclusions:
Quote:
does hibernate maintain reference to the session

It seems not, at least Session.close, as we have seen from the code, does not release any reference in this direction.

Quote:
and which may cause out of memory issue due to open sessions later on.


You should not have problems with memory retention, anyway I suggest you to call

Code:
session.flush();
session.clear(); // clears the persistent context


before your external code does commit and close the connection.

P.S.: You can check if there is a retention problem to sessions by using
Quote:
jmap -histo:live <yourjava-pid>

If there is a retention problem with sessions, then the number of org.hibernate.impl.SessionImpl instances grows with the time.


Top
 Profile  
 
 Post subject: Re: Hibernat Session.close
PostPosted: Tue Feb 28, 2012 8:58 am 
Newbie

Joined: Tue Feb 28, 2012 6:40 am
Posts: 3
Thanks pb00067.
I had checked code of session.close. Just wanted to get confirmation from hibernate experts that it wont have any implications.
Thanks once again.


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.