Two Laptop Bag*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes EntityManager thread safe? 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 thread safe?" Watch "EntityManager thread safe?" New topic
Author

EntityManager thread safe?

Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
Well i understand that EntityManger is not thread safe and should be avoided in Servlet's. As when dependency injection is done, it get's saved in instance variable and the same servlet can be invoked by multiple thread's.

So it is better to avoid DI, and use JNDI lookup for EntityManger.
Even if we are doing JNDI lookup, and entityManager get's saved in instance variable. This same variable will be accessed by mutliple thread's.

1) So why JNDI lookup is safe as compared to DI for EntityManger in Servlet's?

this code will not be safe.
2) Does I conclude here that when below class get's loaded, DI is done only 1 time when class is loaded, as multiple thread access this class instance DI will not be done again-2?
--------------
public class RegistrationServlet extends HttpServlet {

// inject default EntityManager

@javax.persistence.PersistenceContext private EntityManager em;

@Resource private UserTransaction utx;

public void service ( HttpServletRequest req , HttpServletResponse resp)

throws ServletException, IOException {

...

utx.begin();

em.persist(credential);

utx.commit();

...

}

...

}
----------------------------

3) why this code is safe, when the class get's loaded, for the multiple threads, the JNDI lookup is done again-2?

NOTE- I am doing JNDI lookup at class level. This lookup code is out of service(HttpServletRequest req , HttpServletResponse resp) method.
-----------------------------
@PersistenceContext(name="persistence/LogicalName", unitName="ActualPUNameAsItAppearsInPersistence.xml")

public class RegistrationServlet extends HttpServlet {

@Resource private UserTransaction utx;

Context envCtx = InitialContext().lookup("java:comp/env");

EntityManager em = (EntityManager) envCtx.lookup("persistence/LogicalName");


public void service ( HttpServletRequest req , HttpServletResponse resp)

throws ServletException, IOException {


...

utx.begin();

em.persist(credential);

utx.commit();

...

}

...

}
[ November 29, 2008: Message edited by: Amandeep Singh ]

SCJP 1.4, SCWCD 5, SCBCD 5, OCPJWSD 5,SCEA-1, Started Assignment Part 2
My blog- http://rkydesigns.blogspot.com
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
Hi Amandeep,

please use the code presentation when posting code. That makes reading much easier.
Even if we are doing JNDI lookup, and entityManager get's saved in instance variable. This same variable will be accessed by mutliple thread's.
The trick is to use local variables. Actually your code in 3) should better be something like Now each thread works with his own entity manager instance.
[ November 29, 2008: Message edited by: Ralph Jaus ]

SCJP 5 (98%) - SCBCD 5 (98%)
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
thanks Ralph for your response, also i was looking for my question response's
Leon Omk
Ranch Hand

Joined: Aug 17, 2010
Posts: 75

Ralph Jaus wrote:The trick is to use local variables. Actually your code in 3) should better be something like Now each thread works with his own entity manager instance.


Could you provide detailed description from any specification about "Now each thread works with his own entity manager instance. " ?


OK, so that other guy knows Java better than I do, but I bet he can't speak Wuhanese(a Chinese Dialect) like me.
 
 
subject: EntityManager thread safe?