Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

@EJB (basic) questions

 
Mark Garland
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Sorry if these are basic questions, but I wanted to get these straight in my head before going too far!

Let's assume @EJB is annotating an instance variable. As I understand it, it's just the same as you doing a manual JNDI lookup with the name/type inferred from the instance variable. I'm ok with that.

1) When is this lookup performed?
When the bean instance is created?
For stateful - when the bean is initialised between clients? i.e. instance is reused for a 2nd client so is re-initialised to remove client state? How about when the bean is activated having been passivated previously?

2) What happens if the lookup fails?
What about if JNDI experiences an issue - do we get some sort of InitialisationException when the container tries to instantiate the bean?
What about if we get the name wrong - presumably we just get "null", just like if we got the name wrong in a JNDI lookup.

Phew. That's a lot of small questions there.

Any help appreciated.

Cheers,

MG
 
Mark Garland
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone?
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your questions are quite advanced. Basically, DI happens when a bean instance is created by the container.

If looking up fail, you should get an Exception, I don't think you will just get null, because you explicitly state that you want to use DI, but I'm not 100% sure about this.
When passivate/activate, the container should manage DI for you, again I'm not 100% sure.
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Session Bean life cycle heplps you answer these questions. I simply forgot them. Please check out the specs!
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I remember very vaguely from the specs that says if the DI fails, the beaninstance is discarded, but just forgot when DI happens i.e., when the container deploys them or when the actual call for the bean is made.
 
Promod kumar
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For 1, the dependency injection is guaranteed to be done before the bean actually starts responding to the method calls from clients. Also for SFSB the same instance cannot be used for a different client. A SFSB once attached to a client is attached to the same client for its life time.
 
Ralph Jaus
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark wrote:
What happens if the lookup fails?

Jothi is right: "If dependency injection fails, the bean instance is discarded." (core spec 4.3.2 and 5.4.3).

Jothi wrote:
... just forgot when DI happens i.e., when the container deploys them or when the actual call for the bean is made

DI is performed after the bean instance is created: At first EJBContext will be injected (if requested) and then the other DI variables and properties (see core spec 4.4 figure 5, 4.5.1 figure 6 and 5.5 figure 8).

Note: DI is nothing other than a JNDI lookup performed by the container and the result is assigned to the corresponding instance variable (or passed as a parameter to the corresponding property). So this has to be done during runtime.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic