Where to call session bean in the Servlet


EJB design: Where to call session bean in the Servlet

  1. Where to call session bean in the Servlet (5 messages)

    hi, i hope anyone can help me with this. i'm using a session bean in a servlet. anyone can advise me should i call the bean's create() in the servlet init() or in the doService()? thx a lot. Herman
  2. Never access session beans directly from a servlet. There is a J2EE design pattern by name Business Delegate which you can use which provides another layer of abstraction to invoke session bean services from a client (servlet). So ideally it would be Servlet (Presentation Tier) -> Business Delegate (Presentation Tier J2EE Design Pattern) -> Session Bean. The purpose of using Business Delegate is to wrap the session bean methods and from a clients perspective (servlet), the servlet is invoking a method on a Business Delegate which would delegate the request to a session bean method. From the servlets perspective, the servlet is invoking a local method call on a Business Delegate, but eventually the Business Delegate makes a remote call to the session bean methods.
  3. Thanks. So for the purpose of performance, should I save the Business Delegate object in the session or create it in every servlet?
  4. There is no reason why you cannot call the session bean directly from the servlet or Struts action or whatever. Maybe we should have something like "denormalization for performance" to borrow a tem from DB design, applied to Java/J2EE programming and call it "Depatternize for ease of maintenance and code readabilty". It all depends on the application and its complexity. If you know that the Session bean is only going to be used in this webapp and only from a servlet, there is no need to add another class in front of it. Easier to have a base servlet or a service locator that does all the work of getting a local/remote reference to the Session bean for you to call a method on it. This way you do not have to write the plumbing every time you want a reference to the bean. As for performance, the container will pool Session beans. There is no need for you to store a reference to it anywhere. Just get a reference to it in the Service method of your servlet, call whatever method on it, and let it go.
  5. You need not have to store BusinessDelegate object in the session. BusinessDelegate is a POJO (Plain Old Java Object) or in other words its a plain Java Class which you can instantiate from a Servlet. The BusinessDelegate would contain methods identical to a session bean remote interface. You can use ServiceLocator J2EE design pattern to look up the EJBHome and EJBObject from the JNDI Naming Server and cache it in a Java object (ideally a java.util.Map object) so that these objects are looked up only once and reused for improving performance as you know that these objects are network objects and performance intensive. So your application flow would be something like this. JSP (View) -> Servlet (Controller) -> Business Delegate (J2EE Design Pattern) -> Service Locator (J2EE Design Pattern) -> Session Bean (Business Tier)
  6. Never put your business logic in servlet.