-->
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.  [ 7 posts ] 
Author Message
 Post subject: warning: unclosed connection
PostPosted: Wed Nov 23, 2005 8:01 am 
Newbie

Joined: Tue Oct 04, 2005 1:35 am
Posts: 12
Hibernate version: 3.0

Mapping documents: N/A

Code between sessionFactory.openSession() and session.close():
private static final SessionFactory sessionFactory;

static {
try {
// Create the SessionFactory
sessionFactory = new Configuration().configure().buildSessionFactory();

} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
log.error("Initial SessionFactory creation failed.", ex);
throw new ExceptionInInitializerError(ex);
}
}


public static Session currentSession() throws HibernateException {
Session s = sessionFactory.openSession();
return s;
}

public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s != null)
s.close();
}

Full stack trace of any exception that occurs:

WARN Finalizer org.hibernate.jdbc.ConnectionManager - unclosed connection, forgot to call close() on your session?

Name and version of the database you are using: Mysql 4.0.24

The generated SQL (show_sql=true): N/A

Debug level Hibernate log excerpt:
WARN Finalizer org.hibernate.jdbc.ConnectionManager - unclosed connection, forgot to call close() on your session?

Hi all,
can u tell me why i m getting this warning? it doesn't harm my application but it gets logged everytime.. even if u can tell how to turn of this warning in log4j , it would be gr8.. for ur convenience i m giving log4j.properties


log4j.rootLogger=info, R

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%c %d{ISO8601} -- %p -- %m%n

log4j.logger.org.apache.commons=INFO
log4j.logger.org.apache.struts=INFO

log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=../webapps/MForms/mformsLogs/mforms_log

log4j.appender.R.datePattern='_'MM-dd-yyyy'.txt'

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 23, 2005 8:07 am 
Beginner
Beginner

Joined: Tue Nov 22, 2005 4:53 pm
Posts: 41
Location: Netherlands
What is session ?
Is it a collection of some sort ? or the TreadLocal?

What you nou have:
Code:
public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session.set(null);
// Now s = null !!! so close is never called!!
if (s != null)
   s.close();


try closing the session _before_ setting it to null in the ThreadLocal

Code:
public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
if (s != null)
s.close();
   session.set(null);



Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 23, 2005 8:17 am 
Newbie

Joined: Tue Oct 04, 2005 1:35 am
Posts: 12
oh.. i forgot to write this line..
public static final ThreadLocal session = new ThreadLocal();

but i m not calling closeSession(). because doing this it give me exception when doing another database operation session closed or connection not found..
hence i dont call closeSession().

can u please tell me better way to find session from session factory and then calling openSession and closeSession without any problems..
i think there is problem with the class i hv written for this..


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 23, 2005 8:24 am 
Beginner
Beginner

Joined: Tue Nov 22, 2005 4:53 pm
Posts: 41
Location: Netherlands
How is this implemented?

Is this sample code of the Filter an do you use it in Jboss/Tomcat?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 23, 2005 8:45 am 
Newbie

Joined: Tue Oct 04, 2005 1:35 am
Posts: 12
thanx for ur resopnse.. i m sending u complete code of this class

/*
* HibernateUtils.java
*/

package com.mforms.util;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.*;
import org.hibernate.cfg.*;

public class HibernateUtils {

private static Log log = LogFactory.getLog(HibernateUtils.class);

private static final SessionFactory sessionFactory;

static {
try {
// Create the SessionFactory
sessionFactory = new Configuration().configure().buildSessionFactory();

} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
log.error("Initial SessionFactory creation failed.", ex);
throw new ExceptionInInitializerError(ex);
}
}

public static final ThreadLocal session = new ThreadLocal();

public static Session currentSession() throws HibernateException {
Session s = sessionFactory.openSession();
session.set(s);
return s;
}

public static void closeSession() throws HibernateException {
Session s = (Session) session.get();

if (s != null)
s.close();
session.set(null);
}

}



i open the connection like this:
Session session = HibernateUtils.currentSession();and close it like this:
HibernateUtils.closeSession();
if i dont call HibernateUtils.closeSession(), it gives me the warning i mentioned. else it would give following error:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.mforms.servicebean.Users.role, no session or session was closed


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 23, 2005 8:53 am 
Beginner
Beginner

Joined: Tue Nov 22, 2005 4:53 pm
Posts: 41
Location: Netherlands
So why dont you just call closeSession() when your 'unit of work' is done?

Do you use Hibernate stand-alone or in a application server?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 23, 2005 9:00 am 
Beginner
Beginner

Joined: Tue Nov 22, 2005 4:53 pm
Posts: 41
Location: Netherlands
If you use a application server you can rewrite your class to a SessionFilter.

Just let it implement Filter and override the doFilter() method:
Code:

public class HibernateUtils implements javax.servlet.Filter {

// ... other code

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

      if (threadLocal.get() != null) throw new IllegalStateException("A session is already associated with this thread!  " + "Someone must have called getSession() outside of the context "
            + "of a servlet request.");

      try {
         chain.doFilter(request, response);
      }
      finally {
         final Session sess = (Session) hibernateHolder.get();
         if (sess != null) {
            threadLocal.set(null);
            try {
               if (log.isDebugEnabled()) log.debug("Closing Hibernate Session...");
               sess.close();
            }
            catch (HibernateException ex) {
               log.error("Exception while closing Hibernate Session", ex);
               throw new ServletException(ex);
            }
         }
      }

   }
// .. other code..
}



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