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.