• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Application managed EntityManagers with transactions.

 
Treimin Clark
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Treimin Clark
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 170
Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

besides, does Extended persistence context have special meaning in application managed persistence context?
it's just letting it open throughout the bean existence....
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 757
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 757
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 270
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic