23

What is the best approach to get EntityManagerFactory in web app(jsp/servlets)? Is this a good way When should EntityManagerFactory instance be created/opened?, or is it better to get it from JNDI, or something else?

Jordan Noel
  • 175
  • 2
  • 3
  • 13
Evgeni Dimitrov
  • 19,437
  • 29
  • 105
  • 137

1 Answers1

64

They're heavyweight and they're supposed to be in the application scope. So, you need to open them on application startup and close them on application shutdown.

How to do that depends on your target container. Does it support EJB 3.x (Glassfish, JBoss AS, etc)? If so, then you don't need to worry about opening/closing them (neither about transactions) at all if you just do the JPA job in EJBs with @PersistenceContext the usual way:

@Stateless
public class FooService {

    @PersistenceContext
    private EntityManager em;

    public Foo find(Long id) {
        return em.find(Foo.class, id);
    }

    // ...
}

If your target container doesn't support EJBs (e.g. Tomcat, Jetty, etc) and an EJB add-on like OpenEJB is also not an option for some reason, and you're thus manually fiddling with creating EntityManagers (and transactions) yourself, then your best bet is a ServletContextListener. Here's a basic kickoff example:

@WebListener
public class EMF implements ServletContextListener {

    private static EntityManagerFactory emf;

    @Override
    public void contextInitialized(ServletContextEvent event) {
        emf = Persistence.createEntityManagerFactory("unitname");
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        emf.close();
    }

    public static EntityManager createEntityManager() {
        if (emf == null) {
            throw new IllegalStateException("Context is not initialized yet.");
        }

        return emf.createEntityManager();
    }

}

(note: before Servlet 3.0, this class needs to be registered by <listener> in web.xml instead of @WebListener)

Which can be used as:

EntityManager em = EMF.createEntityManager();
// ...
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • Excellent example as always but just wanted to add that when using the @PersistenceContext with no "unitName" attribute, it implies (in a non-declarative way) use of the only/default persistence-unit defined in "persistence.xml". If there are multiple datasources/persistence-units, it could lead to some confusion as to which one this is. – Darrell Teague Feb 26 '13 at 13:10
  • Not sure what else is missing, I follow your answer but there is a runtime error? > Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 0: – huahsin68 Oct 23 '13 at 04:13
  • 3
    @huahsin68: The original question doesn't involve Spring at all and I don't do Spring either. Just press "Ask Question". – BalusC Oct 23 '13 at 09:21
  • Could you give the full name for the `Persistence` class? – Jarekczek Apr 01 '17 at 17:28