1 Reply Latest reply on Sep 13, 2005 12:37 PM by milx

    Question about DAO's

    fourierxform

      Hi,

      I was wondering what is the best way to return DAO's? In other words, let's say I have the following:

      public class FirstExampleServlet extends HttpServlet {
       public static FirstExampleDAO getFirstExampleDAO() {
       try {
       InitialContext ctx = new InitialContext();
       return (FirstExampleDAO) ctx.lookup(FirstExampleDAO.class.getName());
       } catch (Exception e) {
       }
       }
      
       public void doPost(...) {
       SomeObject obj = getFirstExampleDAO().getSomeObject();
       ...
       ...
       }
      }
      
      public class SecondExampleServlet extends HttpServlet {
       public static SecondExampleDAO getSecondExampleDAO() {
       try {
       InitialContext ctx = new InitialContext();
       return (SecondExampleDAO) ctx.lookup(SecondExampleDAO.class.getName());
       } catch (Exception e) {
       }
       }
      
       public void doPost(...) {
       SomeObject obj = getSecondExampleDAO().getSomeObject();
       ...
       ...
       }
      }
      


      Is it possible to have a generic method or class that just returns the DAO I want based on what parameter I pass or perhaps something like a DAO factory?

      public class DAOFactory {
       public Object getDAO(String className) {
       try {
       InitialContect ctx = new InitialContext();
       ...
       ...
       }
       }
      
       public static FirstExampleDAO getFirstExampleDAO() {
       return (FirstExampleDAO) getDAO("FirstExampleDAO.class");
       }
      
       public static SecondExampleDAO getSecondExampleDAO() {
       return (SecondExampleDAO) getDAO("SecondExampleDAO.class");
       }
      }
      


      Any help is much appreciated.

      Thanks!

        • 1. Re: Question about DAO's
          milx

          I suppose your DAOs are stateless session beans.

          I?m currently using the latter approach of your post, where I have a DAOFactory interface, with local, remote and embedded implementations:

          public interface DAOFactory{
           public DAO1 getDAO1();
           public DAO2 getDAO2();
          }
          

          public DAOFactoryLocal implements DAOFactory{
           public DAO1 getDAO1(){
           return (DAO1)lookup(DAO1Local.class.getName());
           }
           public DAO2 getDAO2(){
           return (DAO2)lookup(DAO2Local.class.getName());
           }
           private Object lookup(String name){
           InitialContext ctx = new InitialContext();
           return ctx.lookup(name);
           }
          }
          

          The remote implementation is simply a copy of the local, the only difference is that is uses the classname of the remote interface instead of the local for the JNDI lookup. The embedded implementation obtains the InitialContext from a class that makes sure the embeddable Jboss EJB3 system is up and running before returning it.

          This approach is rather ugly, and leads to a lot of copy & paste of code. It would be nice to be able to inject session beans into servlets running in the same container (VM) as the DAOs, thereby eliminating the need for the local implementation of the DAOFactory, but I think this is not supported by the specification yet.