wood burning stoves 2.0*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Application managed EntityManagers with transactions. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Application managed EntityManagers with transactions." Watch "Application managed EntityManagers with transactions." New topic
Author

Application managed EntityManagers with transactions.

Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
From an explanation from WhizLabs:

WhizLabs wrote:Application managed extended persistence contexts needs to explicitly associated with JTA transaction.


Is this correct? What if the entity manager is created inside an active JTA transaction scope? Don't it automatically be associated with that transaction?
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
It's correct. For extended persistence contexts, the persistence context is created when the Stateful Session Bean get created.

Don't get confused between entity manager and persistence context, thing that get associated to JTA transaction is "persistence context", not entity manager.


SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
Kengkaj Sathianpantarit wrote:It's correct. For extended persistence contexts, the persistence context is created when the Stateful Session Bean get created.


But the question is about application managed entity managers.


Now when will the persistence context be created? The question says that it need to be explicitly join to the transaction. Is it correct here?
Jonathan Elkharrat
Ranch Hand

Joined: Dec 31, 2008
Posts: 170


besides, does Extended persistence context have special meaning in application managed persistence context?
it's just letting it open throughout the bean existence....


SCJP 5, SCWCD 5, SCBCD 5
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Treimin Clark wrote:
Kengkaj Sathianpantarit wrote:It's correct. For extended persistence contexts, the persistence context is created when the Stateful Session Bean get created.


But the question is about application managed entity managers.


Now when will the persistence context be created? The question says that it need to be explicitly join to the transaction. Is it correct here?

Sorry to make a confusion, but please forget my first reply, I don't sure what does application-managed extended persistence contexts mean.

From the code, the persistent context will be created when emf.createEntityManager() is called, so you need to call joinTransaction in other method to synchronize the persistence context with JPA transaction.
Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
Application managed persistence context are always extended.


Kengkaj Sathianpantarit wrote:so you need to call joinTransaction in other method to synchronize the persistence context with JPA transaction.

Confusion again, as I know, this persistence context (above code) is already associated with the methods JTA transaction. So no need to call joinTransaction() here. Right?

Now what about this WhizLabs statement:
WhizLabs wrote:Application managed extended persistence contexts needs to explicitly associated with JTA transaction.

Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Treimin Clark wrote:Application managed persistence context are always extended.


Kengkaj Sathianpantarit wrote:so you need to call joinTransaction in other method to synchronize the persistence context with JPA transaction.

Confusion again, as I know, this persistence context (above code) is already associated with the methods JTA transaction. So no need to call joinTransaction() here. Right?

Right.

Treimin Clark wrote:
Now what about this WhizLabs statement:
WhizLabs wrote:Application managed extended persistence contexts needs to explicitly associated with JTA transaction.


Quoted from EJB 3 in Action:
An application-managed entity manager participates in a JTA transaction in one of two
ways. If the persistence context is created inside the transaction, then the persistence provider
will automatically synchronize the persistence context with the transaction. If the persistence
context was created earlier (outside of a transaction or in a transaction that has since ended),
the persistence context may be manually synchronized with the transaction by calling
joinTransaction() on the EntityManager interface.


We need to see entire question to see the explanation is right or wrong, we cannot just cut some part of explanation and say whether it's correct.
Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
That is the exact explanation given. Not a part of that. As I remembered, the question is something like, choose the correct statement.
Karnati Sudhakar
Ranch Hand

Joined: Aug 26, 2008
Posts: 270

The extended persistence context exists from the point at which the entity manager has been created
using EntityManagerFactory.createEntityManager until the entity manager is closed by
means of EntityManager.close. The extended persistence context obtained from the application-
managed entity manager is a stand-alone persistence context—it is not propagated with the transaction.


This might solve your doubt. From specification 5.7.

Regards
Sudhakar
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Application managed EntityManagers with transactions.