A developer has created an application managed entity manager.
Which statement is correct?
A.
A new persistence context begins when the entity manager is created.
B.
A new persistence context begins when a new JTA transaction begins.
C.
A new persistence context begins when the entity manager is invoked in the context of transaction.
D.
A new persistence context begins when the entity manager is invoked in the context of a resource-local transaction.
Explanation:
http://docs.oracle.com/javaee/6/tutorial/doc/bnbqw.html#bnbra
A
A
A
A
A new persistence context begins when the container-managed entity manager is invoked[36] in the scope of an active JTA transaction, and there is no current persistence context already associated with the JTA transaction. The persistence context is created and then associated with the JTA transaction.
http://stackoverflow.com/questions/2547817/what-is-the-difference-between-transaction-scoped-persistence-context-and-extend
Answer: A
Answer: A
1. The scope of the persistence context of an application-managed entity manager is always extended. It is the responsibility of the application to manage the lifecycle of the persistence context.
The extended persistence context exists from the point at which the entity manager has been created using EntityManagerFactory.createEntityManager until the entity manager is closed by means of EntityManager.close.
An extended persistence context obtained from the application-managed entity manager is a stand-alone persistence context—it is not propagated with the transaction. (Container managed persistence contexts can be either transaction scoped or extended.)
2. A transaction scoped persistence context exists from the point at which container-managed entity manager is invoked in the scope of an active JTA transaction, and there is no current persistence context already associated with the JTA transaction.
The persistence context is created and then associated with the JTA transaction. The persistence context ends when the associated JTA transaction commits or rolls back, and all entities that were managed by the EntityManager become detached.
3. A container-managed extended persistence context can only be initiated within the scope of a stateful session bean.
It exists from the point at which the stateful session bean that declares a dependency on an entity manager of type PersistenceContextType.EXTENDED is created, and is said to be bound to the stateful session bean.
The dependency on the extended persistence context is declared by means of the PersistenceContext annotation or persistence-context-ref deployment descriptor element.
The persistence context is closed by the container when the @Remove method of the stateful session bean completes (or the stateful session bean instance is otherwise destroyed).
Correct answer is A.
Explanation from the Pro JPA 2.0 book:
In terms of the persistence context, the application-managed entity manager is similar to an extended container-managed entity manager. When an application-managed entity manager is created, it creates its own private persistence context that lasts until the entity manager is closed. This means that any entities managed by the entity manager will remain that way, independent of any transactions.