• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Why not use Dependency Injection of Stateful session bean inside a stateless session bean?

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read in the book EJB 3 in Action, that use of Dependency Injection (DI) of statefull session beans should be avoided in stateless beans, servlets etc. The reason given is : Injected EJB instances are stored in an instance variable and are available globally for subsequent clients even if a stateless bean instance (of client) is returned to the pool, and an injected stateful bean instance may contain inaccurate state information that will be available to a different client.
Does this mean that the same statefull session bean in injected into various stateless client beans?
Also it has been stated that in such a situation, JNDI is to be used.
As per my understanding, both DI and JNDI do the same task of looking up for the remote interface and returning its reference. Then what difference would it make of using JNDI over DI?
 
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A simple answer to that will be : Your stateless beans will hold only business logic, and no session [or user particular] data will be stored within. Hence injecting a stateful bean into stateless bean will give you inconsistent data. Even when your stateless bean is returned to the pool, it still holds onto the stateful bean, which might be returned again from the pool to service another client request, whose data might be well and truly different from the one being held in the stateful bean injected.

Dawn.
 
amol l a lekurwale
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dawn,
In that case, why it has been asked to use JNDI instead of DI? Is it because, we do JNDI lookup locally and so everytime the client stateless is used, it will do a fresh look up of the stateful bean?
 
amol l a lekurwale
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any Updates?
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In My opinion, for Servlets,
when you do a look up inside doGet or doPost method hence it will be specific for each request thread and will not be at instance level hence not shared.

Same for Stateless Session Bean(SLSB), you should do a look up through JNDI or EJBCONTEXT inside a business method.

In such cases, JNDI is better than DI because DI is at the instance variable level.

For SLSB, there is no guarantee that stateful session bean(or any other client specific information) would be saved. Hence next users may or may not get the same Stateful session bean
 
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
DI means using instance variables which comprises the STATE.
In stateless ejb you are not supposed to maintain STATE i.e instance variables.

JNDI lookup used for initializing a local variable or reference i.e declared within the business method will not be available for next invocation of the SLSB method. Hence it is safe.

Does this clear your doubt?

Regds,
Amit
 
Whoever got anywhere by being normal? Just ask this exceptional tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic