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 Question about thread safe of EntityManager 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 "Question about thread safe of EntityManager" Watch "Question about thread safe of EntityManager" New topic
Author

Question about thread safe of EntityManager

Mellon Sun
Ranch Hand

Joined: Feb 20, 2003
Posts: 126
"If you're using the EntityManager API directly from a component in the web tier, we recommend that you avoid injecting entity managers because the EntityManager interface is not thread-safe. Instead, you should use a JNDI lookup to grab an instance of a container-managed EntityManager."

The above paragraph is come from EJB 3 in Action, I cannot understand it very much. Is there any difference between the injected EntityManager and the JNDI lookuped EntityManager? Or it just means if injecting entity manager to a servlet, the entity manager should be an instance variable of that servlet so it is not thread safe?


SCJP,SCWCD1.3,SCWCD1.4,SCJD,SCBCD5,SCEA5
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

The injected EntityManager is an instance of a servlet, so as you said, this is not thread safe. On the contrary, declaring an EntityManager locally and doing a JNDI lookup manually is thread safe.


[My Blog]
All roads lead to JavaRanch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about thread safe of EntityManager
 
Similar Threads
Injecting PersistenceContext
No unique bean of type [javax.persistence.EntityManagerFactory] is defined: but found 0
EntityManagerFactory vs EntityManager
Threads in EJB ??
EntityManager in Servlets Doubt