Why to use Home interface to create instance of bean? Instead direct lookup? (ejb 2.1)

StackOverflow https://stackoverflow.com/questions/18797065

  •  28-06-2022
  •  | 
  •  

Question

i can't understend why in all guides write to use this:

SayHiRemote sayHiRemote = InitialContext
            .<SayHiHomeRemote> doLookup(SayHiHomeRemote.JNDI_GLOBAL_NAME)
            .create();
sayHiRemote.hi();

Instead of just :

SayHiRemote sayHiRemote = InitialContext
            .<SayHiRemote> doLookup(SayHiRemote.JNDI_GLOBAL_NAME);
sayHiRemote.hi();

What profit of "create()" method if no arguments (Stateless Session Bean) required?

Was it helpful?

Solution

Prior to EJB 3, it was not possible to directly look up a stateless session bean. There is no real benefit other than consistency with other bean types, which is why it was removed in EJB 3.

There remains a benefit for stateful session beans, because the create method is the factory method, so if you switch to EJB, you lose type-safety (cast vs create() return type) and depending on the relative speed of JNDI vs home.create(), you probably lose some performance.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top