Java Reference
In-Depth Information
In a very simple sense, a persistence context is a self-contained collection of entities man-
aged by an EntityManager during a given persistence scope. The persistence scope is
the duration of time a given set of entities remains managed.
The best way to understand this is to start by examining what the various persistence scopes
are and what they do and then backtrack to the meaning of the term. We'll explain how the
persistence context and persistence scope relate to the EntityManager by first explor-
ing what the persistence scope is.
There are two different types of persistence scopes: transaction and extended .
Transaction-scoped EntityManager
An EntityManager associated with a transaction-scoped persistence context is known
as a transaction-scoped EntityManager . If a persistence context is under transaction
scope, entities attached during a transaction are automatically detached when the transac-
tion ends. (All persistence operations that may result in data changes must be performed
inside a transaction, no matter what the persistence scope is.) In other words, the persist-
ence context keeps managing entities while the transaction it's enclosed by is active. Once
the persistence context detects that a transaction has either been rolled back or committed,
it'll detach all managed entities after making sure that all data changes until that point are
synchronized with the database. Figure 10.3 depicts this relationship between entities, the
transaction persistence scope, and persistence contexts.
Search WWH ::




Custom Search