I'm using Hibernate 4.1.9, Apache Tomcat 7.0.34.
Trying to shutdown my webapp, but Hibernate is leaking references and I'm running out of permgen space. I know I can increase the memory with JVM parameter, but my memory is limited because I have a local developement database running along with couple of other (heavy) application.
Its very annoying to keep restarting Tomcat, after 4-5 deploys. Here is my init and shutdown code:
Code:
public static void init() {
if (sessionFactory != null)
return;
try {
Configuration configuration = new Configuration().configure();
serviceRegistry = new ServiceRegistryBuilder()
.applySettings(configuration.getProperties())
.buildServiceRegistry();
sessionFactory = new MetadataSources(serviceRegistry)
.buildMetadata()
.buildSessionFactory();
} catch (Exception ex) {
Logger.getLogger(HibernateHelper.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void shutdown() {
if (sessionFactory != null && !sessionFactory.isClosed()) {
sessionFactory.close();
sessionFactory = null;
}
if (serviceRegistry != null) {
ServiceRegistryBuilder.destroy(serviceRegistry);
serviceRegistry = null;
}
}
These two methods are called from a ContextListener.