File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Enthuware Persistence Context Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Enthuware Persistence Context Question" Watch "Enthuware Persistence Context Question" New topic
Author

Enthuware Persistence Context Question

Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
When is a Persistent Context created while using container managed EntityManager?
(Assume that no persistence context is already associated with the entity manager.)


1) when entityManager.joinTransaction() is called.

2) as soon as an EntityManager is created.

3) when entityManager.createPersistenceContext() is called.

4) when a new JTA transaction begins.

explanation: Persistence context lives independently of a transaction. Though, usually, both exist at the same time. An EntityManager is usually created right after a transaction has begun. It ends when the method returns (or for an application managed entity manager) when em.close(); is called.

5) when the entity manager is invoked in the scope of an active JTA transaction.

explanation: For a container managed entity manager:
A new persistence context begins when the container-managed entity manager is invoked in the scope of an active JTA transaction, and there is no current persistence context already associated with the JTA transaction. The persistence context is created and then associated with the JTA transaction. The persistence context ends when the associated JTA transaction commits or rolls back, and all entities that were managed by the EntityManager become detached.

Enthuware answer-5.

But my confusion lies in answer 4 explanation. it says persistence context lives independently of a transaction.
But again in answer 5 explanation, it says the persistence context ends when the associated JTA transaction commits or roll back.

So how come then persistence context lives independent of JTA.




SCJP 1.4, SCWCD 5, SCBCD 5, OCPJWSD 5,SCEA-1, Started Assignment Part 2
My blog- http://rkydesigns.blogspot.com
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
First let's point out that the persistence context in the question is transaction scoped and not extended: This follows, since an extended persistence context is created together with the entity manager, while the question assumes that "no persistence context is already associated with the entity manager".
So how come then persistence context lives independent of JTA.
Of course its lifecycle depends on the JTA transaction (the name "transaction scoped" already indicates that). But its creation is independent from the beginning of the transaction, since the
persistence context isn't created when the tx starts, but rather when an entity manager method is called the first time. For instance, if no method of the entity manager is called, no persistence context will be created. In this sense it's independent from the tx.


SCJP 5 (98%) - SCBCD 5 (98%)
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
it is bit doubtful for me here.

Can i say that-
this order is being followed. Please correct me if i am wrong.

1) JTA transaction begins.
2) Persistence Context is created (when em is called)
3) EntityManager is created.

4) EntityManager ends
5) Persistence Context ends
6) JTA transaction ends.
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
I don't think that enthuware's statement
An EntityManager is usually created right after a transaction has begun.
is correct in general. In my thinking one usually injects an entity manager. Then it is created together with the bean (see diagrams in core spec 4.4 or 4.5.1) and exists as long as the bean exists - independently from any tx.

In this case the correct order is as follows:

1. EntityManager is created during dependency injection
2. client calls business method (assume it requires a tx)
3. tx starts
4. Persistence Context is created (when em is called)
5. JTA transaction ends und Persistence Context ends


Concerning 1/2: The order may alternate if it's the first business call for a bean - but I don't think that we have to discuss this topic here.

Concerning 5. jpa spec says (5.6.1):
The persistence context ends when the associated JTA transaction commits or rolls back.

[ December 20, 2008: Message edited by: Ralph Jaus ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Enthuware Persistence Context Question