What is Persistence Context?
- Entities are managed by javax.persistence.EntityManager instance using persistence context.
- Each EntityManager instance is associated with a persistence context.
- Within the persistence context, the entity instances and their lifecycle are managed.
- Persistence context defines a scope under which particular entity instances are created, persisted, and removed.
- A persistence context is like a cache which contains a set of persistent entities , So once the transaction is finished, all persistent objects are detached from the EntityManager's persistence context and are no longer managed.
A persistence context handles a set of entities which hold data to be persisted in some persistence store (e.g. a database). In particular, the context is aware of the different states an entity can have (e.g. managed, detached) in relation to both the context and the underlying persistence store.
Although Hibernate-related (a JPA provider), I think these links are useful:
http://docs.jboss.org/hibernate/core/4.0/devguide/en-US/html/ch03.html
http://docs.jboss.org/hibernate/entitymanager/3.5/reference/en/html/architecture.html
In Java EE, a persistence context is normally accessed via an EntityManager.
http://docs.oracle.com/javaee/6/api/javax/persistence/EntityManager.html
The various states an entity can have and the transitions between these are described below:
http://docs.jboss.org/hibernate/entitymanager/3.6/reference/en/html/objectstate.html
https://vladmihalcea.com/wp-content/uploads/2014/07/jpaentitystates.png