aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes EJB 3.1 injection vs JNDI lookup Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "EJB 3.1 injection vs JNDI lookup" Watch "EJB 3.1 injection vs JNDI lookup" New topic
Author

EJB 3.1 injection vs JNDI lookup

Claude Moore
Ranch Hand

Joined: Jun 24, 2005
Posts: 493
    
    1

Hello everyone,

I really need your help to understand a thing I uncounsciously always considerated as obvious...but that's not really clear in my mind.

So, I knew for sure that a good characteristic of EJB is the fact that the container is able to "scale" their instance number with respect
to actual request load it has to fulfill.


So, in a servlet, I used to adopt this schema before EJB 3.1 adoption:




At each request, I used to lookup required SLSB and do some work. The more requests were send, the more the number of instances the container created
(I was able to verify this fact using my appserver admin console, which reported the number of ejbs created / destroyed...of course i was able to simulate
a variable number of request of service).

Now, with injection mechanism, I usually write:



So, in this last scenario, if I'm not wrong and I understood both servlets' and ejbs' lifecycle, there will be
a single servlet instance which doGet, doPost methods are concurrentially executed by threads asociated
to the web requests, and in each request the very same instance of MyEjb is accessed and used.

Is that wrong ? If not, it seems that with injection we eventually have a single instance of an ejb for each artifact
that requires a reference to it; if so, the property to scale with respect to the load is still valid ?

Please help me to understand.

Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10266
    
168

Irrespective of whether you do a manual lookup or inject the bean, underneath, a proxy is returned/injected. This proxy isn't really the bean instance. Invocation on this proxy ultimately leads to a point, where the bean instance "selection" happens, which depends on various factors including the type of the bean and like you say the "load" on the bean.

So injecting a bean doesn't mean you'll inject a single instance of the bean always.

[My Blog] [JavaRanch Journal]
Claude Moore
Ranch Hand

Joined: Jun 24, 2005
Posts: 493
    
    1

Thank you for your reply...
So, if I understood what you are telling me, I got a reference (which is one in the Servlet injection case), but under the hood the actual instance of the EJB the reference points to may change at
every request ?
This would explain a lot of things...
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10266
    
168

Claude Moore wrote:
So, if I understood what you are telling me, I got a reference (which is one in the Servlet injection case), but under the hood the actual instance of the EJB the reference points to may change at
every request ?

Yes that's correct.
Claude Moore
Ranch Hand

Joined: Jun 24, 2005
Posts: 493
    
    1

Thanks Jaikiran.... It's enough for me. I think that under the hood, the EJB container creates a pools of EJBs and manages its size and assignment to "references" or "proxies".
Thanks again for your help.
Michael Remijan
Author
Ranch Hand

Joined: May 29, 2002
Posts: 126
    
    5

Servlets aren't singletons either. Not sure if that configuration was removed in EE 7 but you can have multiple instances of servlets pooled just like EJBs to handle incoming requests. This pooling is all handled by the app server.


Java EE Evangelist — Author, EJB 3 in Action 2nd Edition — Java Community Process Member
Claude Moore
Ranch Hand

Joined: Jun 24, 2005
Posts: 493
    
    1

Yes, that's true... thanks for your reply Michael.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EJB 3.1 injection vs JNDI lookup