EntityManagerFactory emf = Persistence.createEntityManagerFactory(\"foo\"); EntityManager em = emf.crea
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)