This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes When is a Persistent Context created ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "When is a Persistent Context created ?" Watch "When is a Persistent Context created ?" New topic
Author

When is a Persistent Context created ?

Benoît de Chateauvieux
Ranch Hand

Joined: Aug 10, 2007
Posts: 183
Hi All,

I read in the specs 5.6.1:
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.

and, in the same section:
If the entity manager is invoked outside the scope of a transaction, any entities loaded from the database will immediately become detached at the end of the method call.


In the second case, the persistence context is created too, isn't it ?
So the good answer to "When is a Persistent Context created while using container managed EntityManager?" is

"as soon as an EntityManager is invoked." ?

Thanks for your opinion...

Beno�t


SCJP5 | SCBCD5 | SCEA5 Part 1
Uchana Jackson
Ranch Hand

Joined: Dec 07, 2007
Posts: 37
look at footnote 36 at the bottom of page 121 of the JSR 220 persistence spec:

"Specifically, when one of the methods of the EntityManager interface is invoked."

As far as container-managed transaction-scoped entity managers are concerned, "every time an operation is invoked on the entity manager, it checks to see if a persistence context is associated with the transaction. If it finds one, the entity manager will use this persistence context. If it doesn't find one, then it creates a new persistence context and associates it with the transaction. When the transaction ends, the persistence context goes away."

source: pg. 113 of Pro EJB3 JPA by Mike Keith

As far as container-managed extended-scoped entity managers are concerned, "extended entity managers create a persistence context when a stateful session bean instance is created and lasts until the bean is removed."

pg. 116

I highly recommend you read this entire book, Keith was a co-spec lead on JSR220.


SCJP 1.4<br />SCBCD 5
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

"Seam Rocks",
You have already be warned once about the ranch's Naming Policy. Please read it and change your name accordingly.


[My Blog]
All roads lead to JavaRanch
Uchana Jackson
Ranch Hand

Joined: Dec 07, 2007
Posts: 37
done
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Sorry, but I think that you missed the part on obviously fake names. Please take some time to carefully read through the policy and set your name properly.
Uchana Jackson
Ranch Hand

Joined: Dec 07, 2007
Posts: 37
ok you got me, as if it matters.
Benoît de Chateauvieux
Ranch Hand

Joined: Aug 10, 2007
Posts: 183
Hi Uchana Jackson,

Thanks for your answer.
I understand what you said: a persistence context is created when the entity manager is invoked in the scope of a JTA transaction.

But what if the method invoked has a NOT_SUPPORTED transaction demarcation ?

Thanks,

Beno�t
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When is a Persistent Context created ?
 
Similar Threads
Using Application Managed Entity Manager for Bean with CMT
JTA Transaction on Stateful Session Bean.
Transaction Scoped Persistence Contexts
Enthuware Persistence Context Question
getTransaction() on transaction-scoped persistence context.