• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question about thread safe of EntityManager

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic