Heap filling with Hibernate SessionFactoryImpls

Solution Verified - Updated

Environment

  • JBoss Enterprise Application Platform (EAP)
  • Hibernate
  • Spring

Issue

  • JBoss is going OOME and the heap is filled with hibernate SessionFactoryImpls. These trace back to the SessionFactoryObjectFactory's INSTANCES FastHashMap or the SessionFactoryRegistry's sessionFactoryMap.

Resolution

  • Reuse session factories instead of recreating them
  • If using Spring, don't recreate new ClassPathXmlApplicationContexts
  • If you do recreate session factories or entity manager factories, be sure to call close on them when done. This will result in that session factory being cleaned up from the SessionFactoryObjectFactory's INSTANCES map.

Root Cause

  • Hibernate session factories are often recreated instead of being reused.
  • If using Spring, creating new ClassPathXmlApplicationContexts also creates Hibernate session factories

Diagnostic Steps

  • If seeing growing counts of EntityManagerFactory objects, check their names easily with the following OQL to see if some specific EMFs are leaking:
SELECT entityManagerFactoryName.toString() FROM org.hibernate.ejb.EntityManagerFactoryImpl 
Components
Category

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.