Passing the session bean's "this Pointer" around


EJB design: Passing the session bean's "this Pointer" around

  1. Passing the session bean's "this Pointer" around (2 messages)


    I know that we shouldnt pass the Session bean's this pointer to another bean but use Context.getEJBObject() for this purpose so the container is not bypassed.

    However, I have seen the following recently and wonder what the likely impact could be - A session bean implements an interface say IInterface and implements a couple of log signatures. It then passes the "this pointer" as IInterface around to other POJOs which can use then use the Session bean's reference to log statements.

    This implementation surely violates the EJB spec. However, since it does not communicate with other EJBs but with local POJOs, it seems quite harmless. If it talked to other EJBs, then ofcourse it will pass the EJBObject to it.

    Does anyone see the likely drawback or harmfulness of this implementation ?

    Thanks for your comments.
  2. The problem with passing the this pointer around is that the Session Bean is not guaranteed exist at all times. This is especially true for stateless session beans. It is entirely up to the ejb container to create and destroy stateless session beans. I'm guessing that if you are passing the this reference to objects that are invoked by the session bean then it will likely be Ok, but if you are returning the this pointer to the caller you are heading for disaster. Either way I would suggest passing the reference returned from getEJBObject() which is the remote interface or the local remote interface.
    Also, you may want to re-think your logging strategy. In my experience, the logging mechanism is typically created and cached using a singleton and is POJO so any object can get and utilize this centralized logging mechanism. It seems strange that the logger would need to be used within the context of an EJB. Having to pass references around to be able to log is a pain. It's nice to be able to pull the logger out of thin air (singleton) and use it whenever you want.
  3. Thanks for your comments Kris. I share your thoughts entirely. Its just I got involved way after the project initiation and so have to go along.

    The session bean pass the this pointer to the objects invoked/created from within its method, so that way its ok.