Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

where to create helper objects

 
Mahesh Asrani
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All

I have to access some helper objects in my enterprise beans.
I would like to know where it would be most appropriate to create instances of these helper classes inside the EJB i.e. exactly in which method.

Should i create all the instances in the business method or should it go in the ejbcreate() method or would it be in other ejb methods like setSessionContext or ejbactivate.

I am using SLSB which invokes these plain Java classes.

Bye
Mahesh
 
Valentin Tanase
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mahes,


Should i create all the instances in the business method or should it go in the ejbcreate() method or would it be in other ejb methods like setSessionContext or ejbactivate.

I am using SLSB which invokes these plain Java classes.

Let�s see now� A SLSB has only two states: Does Not Exist state and Method-Ready Pool state. Usually the container creates a number of SLSB instances and moves them to the pool (this could be configured through deployment descriptors). When the container moves the SLSB to the pool, three thinks happen (in this order):
  • The container invokes Class.newInstance() on the SLSB class.
  • The container invokes the setSessionContext() method.
  • The container invokes the ejbCreate() method.


  • As you can see, SLSB are never activated/passivated, since they have no state. Hence the two corresponding ejb methods are ignored in the case of SLSBs. The next question to ask is whether the instance variables are either local to the method, or global (being used by more than one method). If the first one is true then you might consider creating the instances locally. Otherwise you have to choose between initializing them either inside of setSessionContext or ejbCreate. I would take the ejbCreate, because of two reasons:
  • It makes a lot of sense from a design standpoint; it looks like the internal state is initialized when the bean is instantiated.
  • The bean�s context is initialized and you can access all information available in the SessionContext object.


  • Regards.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic