开发者

Does executing EntityManagerFactory.createEntityManager() reutrn new instance each time?

开发者 https://www.devze.com 2023-01-03 10:56 出处:网络
Does executing EntityManagerFactory.createEntityManager() reutrn new instance each time? or it returns the cached copy of the same Enti开发者_如何学GotyManager each time? The Javadoc is unambiguous:

Does executing EntityManagerFactory.createEntityManager() reutrn new instance each time? or it returns the cached copy of the same Enti开发者_如何学GotyManager each time?


The Javadoc is unambiguous:

Create a new application-managed EntityManager. This method returns a new EntityManager instance each time it is invoked.


EntityManagerFactory returns new instance of EntityManager on each call to EntityManagerFactory.createEntityManager().

If you execute...

emf = Persistence.createEntityManagerFactory("basicPU");
 for (int i = 0 ; i<10; i++){
     System.out.println(em = emf.createEntityManager());
 }

It Prints:

org.apache.openjpa.persistence.EntityManagerImpl@18105e8
org.apache.openjpa.persistence.EntityManagerImpl@9bad5a
org.apache.openjpa.persistence.EntityManagerImpl@91f005
org.apache.openjpa.persistence.EntityManagerImpl@1250ff2
org.apache.openjpa.persistence.EntityManagerImpl@3a0ab1
org.apache.openjpa.persistence.EntityManagerImpl@940f82
org.apache.openjpa.persistence.EntityManagerImpl@864e43
org.apache.openjpa.persistence.EntityManagerImpl@17c2891
org.apache.openjpa.persistence.EntityManagerImpl@4b82d2
org.apache.openjpa.persistence.EntityManagerImpl@179d854


To second skaffman's answer, here is an extract of the JPA 1.0 specification:

5.9.2 Provider Responsibilities

The Provider has no knowledge of the distinction between transaction-scoped and extended persistence contexts. It provides entity managers to the container when requested and registers for synchronization notifications for the transaction.

  • When EntityManagerFactory.createEntityManager is invoked, the provider must create and return a new entity manager. If a JTA transaction is active, the provider must register for synchronization notifications against the JTA transaction.
0

精彩评论

暂无评论...
验证码 换一张
取 消