Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should EntityManagerFactory instance be created/opened?

Ok, I read bunch of articles/examples how to write Entity Manager Factory in singleton.

One of them easiest for me to understand a bit:

http://javanotepad.blogspot.com/2007/05/jpa-entitymanagerfactory-in-web.html

I learned that EntityManagerFactory (EMF) should only be created once preferably in application scope.

And also make sure to close the EMF once it's used (?)

So I wrote EMF helper class for business methods to use:

public class EmProvider {      private static final String DB_PU = "KogaAlphaPU";      public static final boolean DEBUG = true;      private static final EmProvider singleton = new EmProvider();      private EntityManagerFactory emf;      private EmProvider() {}      public static EmProvider getInstance() {         return singleton;     }       public EntityManagerFactory getEntityManagerFactory() {         if(emf == null) {             emf = Persistence.createEntityManagerFactory(DB_PU);         }         if(DEBUG) {             System.out.println("factory created on: " + new Date());         }         return emf;     }      public void closeEmf() {         if(emf.isOpen() || emf != null) {             emf.close();         }         emf = null;         if(DEBUG) {             System.out.println("EMF closed at: " + new Date());         }     }  }//end class 

And my method using EmProvider:

public String foo() {     EntityManager em = null;     List<Object[]> out = null;     try {          em = EmProvider.getInstance().getEntityManagerFactory().createEntityManager();         Query query = em.createNativeQuery(JPQL_JOIN); //just some random query          out = query.getResultList();     }     catch(Exception e) {         //handle error....     }     finally {         if(em != null) {              em.close(); //make sure to close EntityManager         }         //should I not close the EMF itself here?????         EmProvider.getInstance().closeEmf();     } 

I made sure to close EntityManager (em) within method level as suggested. But when should EntityManagerFactory be closed then? And why EMF has to be singleton so bad??? I read about concurrency issues but as I am not experienced multi-thread-grammer, I can't really be clear on this idea.

like image 286
Meow Avatar asked Dec 28 '10 05:12

Meow


People also ask

Why do we need EntityManagerFactory?

EntityManagerFactory provides instances of EntityManager for connecting to same database. All the instances are configured to use the same setting as defined by the default implementation. Several entity manager factories can be prepared for connecting to different data stores.

What is difference between EntityManager and EntityManagerFactory?

EntityManagerFactory vs EntityManager EntityManager: whenever using spring avoid managing/using EntityManagerFactory since Spring manages concurreny for you. The entity manger injected by @PersistenceContext is thread safe. While EntityManagerFactory instances are thread-safe, EntityManager instances are not.

What is EntityManagerFactory?

EntityManagerFactory class is a factory for EntityManager s. EntityManager : The javax. persistence. EntityManager is the primary JPA interface used by applications. Each EntityManager manages a set of persistent objects, and has APIs to insert new objects and delete existing ones.


1 Answers

  • EntityManagerFactory instances are heavyweight objects. Each factory might maintain a metadata cache, object state cache, EntityManager pool, connection pool, and more. If your application no longer needs an EntityManagerFactory, you should close it to free these resources.

  • When an EntityManagerFactory closes, all EntityManagers from that factory, and by extension all entities managed by those EntityManagers, become invalid.

  • It is much better to keep a factory open for a long period of time than to repeatedly create and close new factories. Thus, most applications will never close the factory, or only close it when the application is exiting.

  • Only applications that require multiple factories with different configurations have an obvious reason to create and close multiple EntityManagerFactory instances.

  • Only one EntityManagerFactory is permitted to be created for each deployed persistence unit configuration. Any number of EntityManager instances may be created from a given factory.

  • More than one entity manager factory instance may be available simultaneously in the JVM. Methods of the EntityManagerFactory interface are threadsafe.
like image 129
Nayan Wadekar Avatar answered Sep 20 '22 16:09

Nayan Wadekar