aspose file tools*
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


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "EntityManager question" Watch "EntityManager question" New topic
Author

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 ]

SCJP, SCJD,SCWCD,SCDJWS,SCEA 5 MCP-C#, MCP-ASP.NET - http://www.khaledinho.com/
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?

Regards.


/** 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 5.6.3.1 (page 122) of the EJB 3 Persistence Specification, where all the Container-Managed Persistence Contexts propagation cases are explained:

http://java.boot.by/scbcd5-guide/ch07s04.html

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: http://aspose.com/file-tools
 
subject: EntityManager question