File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Why not use Dependency Injection of Stateful session bean inside a stateless session bean? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Why not use Dependency Injection of Stateful session bean inside a stateless session bean?" Watch "Why not use Dependency Injection of Stateful session bean inside a stateless session bean?" New topic
Author

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

amol l a lekurwale
Ranch Hand

Joined: Oct 16, 2008
Posts: 30
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?
Dawn Charangat
Ranch Hand

Joined: Apr 26, 2007
Posts: 249
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

Joined: Oct 16, 2008
Posts: 30
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

Joined: Oct 16, 2008
Posts: 30
Any Updates?
sandeeprajsingh tandon
Ranch Hand

Joined: Mar 06, 2009
Posts: 70
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
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 511
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
 
Don't get me started about those stupid light bulbs.
 
subject: Why not use Dependency Injection of Stateful session bean inside a stateless session bean?
 
Similar Threads
Doubt in SessionBean contract(ejb 3.0 core)
DI doubt in Session Bean
EntityManager question
Dependency injection and stateful session beans
Stateful session bean