Discussions

EJB design: Use of EJBHomeFactory from other EJBs?

  1. Use of EJBHomeFactory from other EJBs? (1 messages)

    In the patterns book, it says that the EJBHomeFactory class can be used from other EJBs. It goes on to state that "EJBs usually simply cache the required home in setSession/Entity/MessageContext method". Being relatively new to EJBs, I'm not sure that this means. Can someone provide me a little more detail? Thanks

    Chuck
  2. Broadly, the safest solution is to create instance variables holding home interface references in the bean class, knowing the EJBs that the EJB in question is likely to access. This means that the time taken to do the lookups is localised to the initialisation time and does not occur during the business methods for a bean.

    The code for a session EJB would look some thing like:

    public class anEJB extends SessionBean
    {
      private anotherEJBHome;

      ...

      public void setSessionContext(SessionContext ctx) throws EJBException, java.rmi.RemoteException
      {
        ...
        anotherEJBHome = AnEJBFactory.lookup();
      }

      public void aBusinessMethod()
      {
        ...
        anotherEJBRemote remote = anotherEJBHome.create()
        remote.doBusinessMethod();
        ...
      }
    }

    Of course this only makes sense for EJBs that are likely to have a lifetime that makes it worthwhile to do the initialisation up front. For example stateless session beans are usually created and pooled by an EJB container and thus an instance is used extensively, thus it makes sense to place the lookups to the initialisation step.