Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

EntityManager thread safe?

 
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Amandeep Singh
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Ralph for your response, also i was looking for my question response's
 
Ranch Hand
Posts: 75
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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. " ?
 
reply
    Bookmark Topic Watch Topic
  • New Topic