Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Thred Safety when Injecting an EJB into servlet using @EJB  RSS feed

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi , i have read that

when EJBs were injected into servlets using @EJB it is said that there will be no Thread Safety here ??

Is this true.
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37230
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Just like any other instance variable in a servlet.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much .
Then is the jndi lookup is the appropiate way to call a Bean from Servlet ??
 
Manuel Alberto Quero
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why isn´t it thread-safe??
Is not the EJB container responsible for assigning an available bean instance to every client (in our case, the servlet)? I think the EJB container assures you that the ejb instance won't be used by anybody else as long as you hold the reference....
 
Lorand Komaromi
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manuel Alberto Quero wrote:Why isn´t it thread-safe??


Because a single instance of the servlet will process all the requests coming from different threads.
 
Manuel Alberto Quero
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK Lorand, thanks...

The I think that the answer to the question of Ravi Kiran V:
"Then is the jndi lookup is the appropiate way to call a Bean from Servlet ??"

Is yes (for stateful session beans), but without caching the reference. So every time you execute the servlet you need to perform:

ejb = (ExampleStatefulSession)ctx.lookup("EnterpriseApp/ExampleStatefulSession");

Regards
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!