Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

EntityManager thread safe?

 
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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. " ?
 
I got this tall by not having enough crisco in my diet as a kid. This ad looks like it had plenty of shortening:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic