permaculture playing cards
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes EntityManager question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "EntityManager question" Watch "EntityManager question" New topic

EntityManager question

Khaled Mahmoud
Ranch Hand

Joined: Jul 15, 2006
Posts: 361
I have read in "EJB 3 In Action" is that entity manager is not thread safe. In EJB enterprise applications where many session beans use an entity manger reference i suppose that every thread gets its own reference of the entity manager object. My question is there a level of coordination between the entity manager instances.
[ August 19, 2007: Message edited by: Khaled Mahmoud ]

Life is the biggest school
Shivani Chandna
Ranch Hand

Joined: Sep 18, 2004
Posts: 380
What do you mean when you say a level of coordination?


/** Code speaks louder than words */
Camilo Morales
Ranch Hand

Joined: Jul 14, 2007
Posts: 92
I'm not sure if I understand your question, anyway, refering to EntityManager operations take this into account:

There are 3 ways to get a reference to a EntityManager: Injection - JNDI lookup - EJBContext lookup. All of them create a NEW instance of the EntityManager (Injected or looked up), so every instance of the Session or Message Driven Bean get a NEW instance of the EntityManager. (there is no sharing of EntityManagers between components)

There is a special case where the SAME PersistenceContext attached to an EntityManager is propagated and used by two different STATEFUL SESSION BEAN instances. Is the following:

If one Stateful Session Beans with an EXTENDED Persistence Context is the Client of another Session Bean with an EXTENDED Persistence Context, in that case, and ONLY IF BOTH PERSISTENCE CONTEXTS WERE INJECTED, both Statefull Session Bean instances share the same persistence context in the scope of the transaction.

Take a look at the following link to the chapter 7 of Mikalai Zaikin notes and/or numeral (page 122) of the EJB 3 Persistence Specification, where all the Container-Managed Persistence Contexts propagation cases are explained:

Hope this helps you,

Camilo Morales<br />SCJP 5, SCBCD 5
Khaled Mahmoud
Ranch Hand

Joined: Jul 15, 2006
Posts: 361
Actually i had a confusion about
of the meaning of persistence context but it is now clear.

Thank you.
I agree. Here's the link:
subject: EntityManager question
It's not a secret anymore!