Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enthuware Persistence Context Question

 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
Ralph Jaus
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic