EntityManagerFactory emf = Persistence.createEntityManagerFactory(\"foo\"); EntityManager em = emf.crea
Definitely, always close your EntityManagers.
I had a similar connection leak. The root cause was that the select statement implicitly starts a transaction. That transaction is still active when EntityManager.close() is called, and for some reason that keeps the connection open.
Worse, our connection pool never reclaimed connections that were stuck in this state.
My fix was to explicitly check for an active transaction before closing any entityManager. I wrote a little helper function for this:
public static void rollbackAndClose(EntityManager mgr) {
if (mgr != null) {
EntityTransaction transaction = mgr.getTransaction();
if (transaction.isActive()) {
transaction.rollback();
}
mgr.close();
}
}
(I am used to transactions rolling back automatically when disconnecting a SQL client from Oracle or SQLServer, so the EntityManager behavior is counter-intuitive to me. Not sure if JPA dictates this behavior, or if it is specific to Hibernate or MySQL.)
From the snippet you displayed it looks like your application is a standalone and not a managed application running within some Java EE container like JBoss AS or with Spring (which for the sake of argument can be considered a Java EE container). According to the following Wiki you are running in an Application-Managed Entity Manager. So you need to explicitly close the entity manager and the factory.
I would like to clarify that you need to close the EntityManager at the transaction boundary. If you will look at the code of hibernate Session (EntityManagerImpl.close() actuality delegate to session close). You will notice that it close the transaction and clear the persistence context. Closing the EntityManagerFactory is more at the application level so you can reuse it and close it when you destroy your application.
Saying that, and following your concern for connection leaks note that connection are not managed directly by hibernate. Hibernate has a plugin architecture that allows integration with connection pool. Hibernate (I think since version 3.3, not sure) ships with a default connection pooling mechanism which is not recommended for production. If you are using the default one it may be the cause for your connection leaks (see the following post).
The most common connection pool is used with hibernate is C3P0 (I am not sure it’s the best one..). In a non-managed environment you need to verify the configuration for the connection pool (e.g. hibernate.c3p0.* related hibernates properties)
If you are creating your own EntityManager instances using the EMF then you need to close them.
If you are running within a manged container (e.g JBoss AS or any other EJB container) then you can have an EntityManger injected to your bean and then you should not worry about closing it.