File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Doubt on PersistenceContext 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 "Doubt on PersistenceContext" Watch "Doubt on PersistenceContext" New topic

Doubt on PersistenceContext

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 10031


Whys is that only those EntityManager instances injected with the @PersistenceContext annotation are transaction-scoped?

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

You mean, why application-managed persistence contexts are not transaction-scoped ? You don't want the container to mess up with your application-managed stuff (EntityManager, persistence context), do you ? You are the one who controls the scope of the entity manager and its persistence context. You are the one who must close the entity manager. Why would you need a transaction scope ?

[My Blog]
All roads lead to JavaRanch
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
Yes I agree, as Christophe says, that an application-managed persistence contexts must be managed by the application(as its name says )

Additionally, when a JTA application-managed entity manager is used, if the entity manager is created outside the scope of the current JTA transaction, it is the responsibility of the application to associate the entity manager with the transaction (if desired) by calling EntityManager.joinTransaction.

For resource local transactions, the entity manager should start and commit the transaction by itself. Please see "5.7 Application-managed Persistence Contexts" of ejb-3_0-fr-spec-persistence.

Thanks and Regards
Chaminda Amarasinghe
Ranch Hand

Joined: May 17, 2006
Posts: 404
Hi Jothi,

See the reply by Ralph to your thread "EntityManagerFactory vs EntityManager".

He has given a good explanation on this.

I agree. Here's the link:
subject: Doubt on PersistenceContext
It's not a secret anymore!