jQuery in Action, 3rd edition
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Persistence Context Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Persistence Context Question" Watch "Persistence Context Question" New topic

Persistence Context Question

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 10031


There are two flavors of Entity Manager 1) Container Managed 2) Application Managed. What would be the real difference in choosing between them?

SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

With an application managed persistence context, you'd have to manage yourself the entity manager lifecycle, using the entity manager factory. I think that this definition from the spec resumes it well :
In Java EE environments, a JTA transaction typically involves calls across multiple components. Such components may often need to access the same persistence context within a single transaction. To facilitate such use of entity managers in Java EE environments, when an entity manager is injected into a component or looked up directly in JNDI its persistence context will automatically be propagated with the current JTA transaction, and the EntityManager references that are mapped to the same persistence unit will provide access to this same persistence context within the JTA transaction. This propagation of persistence context by the Java EE container avoids the need for the application to pass references to EntityManager instances from one component to another. An entity manager for which the container manages the persistence context in this manner is termed a container-managed entity manager. A container-managed entity manager's lifecycle is managed by the Java EE container.

In less common use cases within Java EE environments, applications may need to access a persistence context that is �stand-alone��i.e. not propagated along with the JTA transaction across the EntityManager references for the given persistence unit. Instead, each instance of creating an entity manager causes a new isolated persistence context to be created that is not accessible through other EntityManager references within the same transaction. These use cases are supported through the createEntityManager methods of the EntityManagerFactory interface. An entity manager that is used by the application to create and destroy a persistence context in this manner is termed an application-managed entity manager. An application-managed entity manager's lifecycle is managed by the application.

[My Blog]
All roads lead to JavaRanch
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 10031

Thanks again Christophe!
I agree. Here's the link: http://aspose.com/file-tools
subject: Persistence Context Question
It's not a secret anymore!