Discussions

EJB design: EJB and a single instance of DAO

  1. EJB and a single instance of DAO (7 messages)

    Hi...
    Will there be a problem (like transactional) if all EJB share one DAO
    instance? The DAO doesn't maintain any state which will be concurrently
    accessed.

    Thanks...
    Jerson
    -- code --
    public class DAOFactory {
            private static OracleDAO oracleDAO;
            private static SybaseDAO sybaseDAO;

            public static DAO getDAO() throws OrderDAOException {

                    try {
                    InitialContext ic = new InitialContext();
                        DataSource ds = (DataSource)ic.lookup("ds");
                        Connection conn = ds.getConnection();
                  String DBProductName =
                            conn.getMetaData().getDatabaseProductName().trim();
                    conn.close();

                    if (DBProductName.startsWith("Oracle")) {
                                    return getOracleDAO();
                    } else if (DBProductName.startsWith("SQL Server")) {
                                    return getSybaseDAO();
                            } else {
                                    throw new OrderDAOException("db not
    supported");
                            }
                    } catch (NamingException ne) {
                    throw new OrderDAOException("OrderDAOFactory.getDAO:
    NamingException while getting DB connection : \n" + ne.getMessage());
                    } catch (SQLException se) {
                    throw new OrderDAOException("OrderDAOFactory.getDAO:
    SQLException while getting DB connection : \n" + se.getMessage());
                    }
            }

            public OracleDAO getOracleDAO() {
                    if (oracleDAO == null) {
                            oracleDAO = new OracleDAO;
                    }
                    return oracleDAO;
            }

            public SybaseDAO getSybaseDAO() {
                    if (sybaseDAO == null) {
                            sybaseDAO = new sybaseDAO;
                    }
                    return sybaseDAO;
            }
    }

    Threaded Messages (7)

  2. EJB and a single instance of DAO[ Go to top ]

    sorry, the getOracleDAO() and getSybaseDAO() should be declared as static.
  3. EJB and a single instance of DAO[ Go to top ]

    There are a few issues

    1) Some bugs in your singleton code. You need some critical sections to prevent getting two instances in your static methods


            public OracleDAO getOracleDAO() {
               syncrhonized(DAOFactory.class) {
                    if (oracleDAO == null) {
                            oracleDAO = new OracleDAO;
                    }
                 }
                 return oracleDAO;
            }

    2) That said, singleton patterns do not work in most EJB app servers and are basically forbidden by the spec. The reason is most EJB servers use custom implementations of ClassLoader. Your singleton will only be a singleton within a single ClassLoader. You will have to assure that this class is loaded by a ClassLoader at least one step up the chain to assure this is a singleton as you expect. It will be container dependant how to manage this, which is why is it shunned so in the spec.

    Dave Wolf
    Internet Applications Division
    Sybase

  4. EJB and a single instance of DAO[ Go to top ]

    Thanks for replying...
    I have no problem even if the DAO is loaded more than once by different classloaders since my DAO doesn't maintain shared information. I don't really need to achieve singleton. I just don't want to create a new instance everytime an ejb requested for a DAO to reduce the number of objects in the memory.

    Here's a template of my DAO. As you can see, it doesn't share one connection. It's local to the method.

    public class MyDAO {
    // no shared attributes

    public void insertRecord(...) throws ... {
    Connection conn = null;
    try {
    InitialContext ic = new InitialContext();
    DataSource ds = ic.lookup("datasource");
    conn = ds.getConnection();
    } catch (...) {
    } finally {
    conn.close();
    }
    }
    }

    What do you think? will there be any transactional problem or other issues?

    Jerson
  5. EJB and a single instance of DAO[ Go to top ]

    Whats the advantage of this being a singleton?

    Dave Wolf
    Internet Applications Division
    Sybase
  6. EJB and a single instance of DAO[ Go to top ]

    Hi Dave...
    As i've said earlier, I just want to reduce the number of DAO objects in the memory (to reduce the load of the gc)cause in the Java Pet Store, I think the DAOFactory always create a new instance.

    So is this okay? Will there be a problem in transaction?

    thanks...
    Jerson
  7. EJB and a single instance of DAO[ Go to top ]

    Yes its fine, but Im not sure its the best answer. Wouldnt a Stateless SessionBean help? In this way you'd get the advantage of instance swapping/pooling reducint the number of instances and completely avoiding GC. PLus things like declarative tx management etc.

    Dave Wolf
    Internet Applications Division
    Sybase
  8. EJB and a single instance of DAO[ Go to top ]

    Hi...
    Actually, the DAO is going to be used by a stateless session bean just like the one in Java Petstore of Sun. We thought of using DAO and DAO factory pattern so we can easily port our application from one database to another.

    Thank you very much... now I'm sure that's it going to be fine. Your help is very much appreciated.

    Jerson