Hibernate / EclipseLink Interview questions
What is the difference between EntityManager persistence context types: transaction-scoped vs extended?
A transaction-scoped persistence context, the default in container-managed environments, lives only for the duration of a single transaction; once the transaction commits or rolls back, every entity managed by that EntityManager becomes detached. An extended persistence context, declared with @PersistenceContext(type = PersistenceContextType.EXTENDED), survives across multiple transactions, typically tied to the lifetime of a stateful component.
| Transaction-scoped | Extended |
| Lives for one transaction only. | Lives as long as the owning component (e.g. a stateful session bean). |
| Entities detach automatically at transaction end. | Entities stay managed across multiple transactions. |
| Default and most common choice. | Used for multi-step, conversational workflows spanning several requests. |
Extended persistence contexts are most associated with stateful conversational workflows, like a multi-step wizard where an entity needs to remain managed and accumulate changes across several user interactions before a final commit, avoiding repeated detach/merge cycles between each step. They're used far less often than transaction-scoped contexts, since holding entities managed across an extended lifetime also means holding onto memory and cache state for longer, which doesn't suit typical short-lived, stateless request handling.
More Related questions...