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;
}
}
}