-->
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.  [ 1 post ] 
Author Message
 Post subject: Several hibernate objects don't get garbage collected
PostPosted: Mon Jun 28, 2004 3:45 pm 
Newbie

Joined: Tue Apr 13, 2004 9:54 am
Posts: 2
Environment:

Hibernate: 2.1.4
OS: Windows 2000 5.0
Tomcat: 5.0.19
JVM: SUN 1.4.2_03

Using DBCP connection pooling against Oracle.

While running our applications through JProfiler we noticed that after shutting our application down in Tomcat, we had the following objects that failed to get garbage collected:

net.sf.hibernate.impl.SessionFactoryObjectFactory - there is a static reference to this from net.sf.hibernate.impl.SessionFactoryObjectFactory.INSTANCE

net.sf.hibernate.impl.SessionFactoryImpl$FilterCacheKeyFactory$$KeyFactoryByCGLIB - static reference to this from net.sf.hibernate.impl.SessionFactoryImpl.FILTER_KEY_FACTORY

net.sf.hibernate.impl.SessionFactoryImpl$QueryCacheKeyFactory$$KeyFactoryByCGLIB$$ - static reference to this from net.sf.hibernate.impl.SessionFactoryImpl.QUERY_KEY_FACTORY

We close our SessionFactory (and set it to null) in a ServletContextListener contextDestroyed method which gets executed when the application is stopped. This successfully allows the other hibernate and application objects to get garbage collected.

Is there any way to free the remaining three up that I've listed so they can get garbage collected?

Here is our code for reference...

ServletContextListener:
Code:
public class SessionCleanup implements ServletContextListener {

  public void contextDestroyed(ServletContextEvent arg0) {
    try {
      HibernateSession.destroy();
    } catch (Exception e) {
      e.printStackTrace();
    }
    DaoFactory.destroyDaoFactory();
  }

  public void contextInitialized(ServletContextEvent arg0) {
    try {
      HibernateSession.buildSessionFactory();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

}


HibernateSession:
Code:
public class HibernateSession {
 
  private static SessionFactory sf = null;
  private static final ThreadLocal session = new ThreadLocal();
 
  public static synchronized void buildSessionFactory() throws Exception {
    Configuration config =
      new Configuration().addClass(a.class).
                          addClass(b.class).
                          addClass(c.class);
    sf = config.buildSessionFactory();
  }

  public static void closeSession() throws Exception {
    System.out.println("Closing Hibernate session");
    Session s = (Session) session.get();
    if (s != null) {
      s.clear();
      s.disconnect();
      s.close();
    }
    session.set(null);
  }
 
  public static Session currentSession() throws Exception {
    Session s = (Session) session.get();
    if (s == null) {
      if (sf == null) {
        buildSessionFactory();
      }
      else {
        System.out.println("Reusing existing Hibernate session factory");
      }
      System.out.println("Opening new Hibernate session");
      s = sf.openSession();
      session.set(s);
    }
    else {
      System.out.println("Reusing existing Hibernate session");
    }
    return s;
  }

  public static void destroy() throws Exception {
    if (sf != null) {
      sf.close();
      sf = null;
    }
  }

}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.