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;
}
}
-
EJB and a single instance of DAO (7 messages)
- Posted by: Jerson Chua
- Posted on: January 15 2001 09:15 EST
Threaded Messages (7)
- EJB and a single instance of DAO by Jerson Chua on January 15 2001 09:18 EST
- EJB and a single instance of DAO by Dave Wolf on January 15 2001 10:03 EST
-
EJB and a single instance of DAO by Jerson Chua on January 16 2001 10:25 EST
-
EJB and a single instance of DAO by Dave Wolf on January 16 2001 10:54 EST
-
EJB and a single instance of DAO by Jerson Chua on January 17 2001 02:14 EST
-
EJB and a single instance of DAO by Dave Wolf on January 17 2001 08:52 EST
- EJB and a single instance of DAO by Jerson Chua on January 18 2001 08:27 EST
-
EJB and a single instance of DAO by Dave Wolf on January 17 2001 08:52 EST
-
EJB and a single instance of DAO by Jerson Chua on January 17 2001 02:14 EST
-
EJB and a single instance of DAO by Dave Wolf on January 16 2001 10:54 EST
-
EJB and a single instance of DAO by Jerson Chua on January 16 2001 10:25 EST
- EJB and a single instance of DAO by Dave Wolf on January 15 2001 10:03 EST
-
EJB and a single instance of DAO[ Go to top ]
- Posted by: Jerson Chua
- Posted on: January 15 2001 09:18 EST
- in response to Jerson Chua
sorry, the getOracleDAO() and getSybaseDAO() should be declared as static. -
EJB and a single instance of DAO[ Go to top ]
- Posted by: Dave Wolf
- Posted on: January 15 2001 10:03 EST
- in response to Jerson Chua
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
-
EJB and a single instance of DAO[ Go to top ]
- Posted by: Jerson Chua
- Posted on: January 16 2001 22:25 EST
- in response to Dave Wolf
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 -
EJB and a single instance of DAO[ Go to top ]
- Posted by: Dave Wolf
- Posted on: January 16 2001 22:54 EST
- in response to Jerson Chua
Whats the advantage of this being a singleton?
Dave Wolf
Internet Applications Division
Sybase
-
EJB and a single instance of DAO[ Go to top ]
- Posted by: Jerson Chua
- Posted on: January 17 2001 02:14 EST
- in response to Dave Wolf
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 -
EJB and a single instance of DAO[ Go to top ]
- Posted by: Dave Wolf
- Posted on: January 17 2001 08:52 EST
- in response to Jerson Chua
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
-
EJB and a single instance of DAO[ Go to top ]
- Posted by: Jerson Chua
- Posted on: January 18 2001 08:27 EST
- in response to Dave Wolf
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