When is a Persistent Context created while using container managed EntityManager?
(Assume that no persistence context is already associated with the entity manager.)
1) when entityManager.joinTransaction() is called.
2) as soon as an EntityManager is created.
3) when entityManager.createPersistenceContext() is called.
4) when a new JTA transaction begins.
explanation: Persistence context lives independently of a transaction. Though, usually, both exist at the same time. An EntityManager is usually created right after a transaction has begun. It ends when the method returns (or for an application managed entity manager) when em.close(); is called.
5) when the entity manager is invoked in the scope of an active JTA transaction.
explanation: For a container managed entity manager:
A new persistence context begins when the 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.
Enthuware answer-5.
But my confusion lies in answer 4 explanation. it says persistence context lives independently of a transaction.
But again in answer 5 explanation, it says the persistence context ends when the associated JTA transaction commits or roll back.
So how come then persistence context lives independent of JTA.
