Hi, i am using
Tomcat 4.1.31 to run a simple web application. I am looking for advice on coding my DAOs. For example, when I access a DAO to retrieve a single column value, the first thing in my code is:
try {
context = new InitialContext();
datasource = (DataSource) context.lookup("java:comp/env/jdbc/testPortletDB");
} catch (NamingException ne) {
LOG.debug("Unable to lookup datasource. " + ne.getMessage());
}
try {
conn = datasource.getConnection();
} catch (SQLException sqlex) {
LOG.debug("Unable to open database connection. " + sqlex.getMessage());
}
The above code works fine but I am suspecting that I should not be looking up the datasource each time the DAO is called. Should the above code be placed in the constructor? Or should I have a static method that would return a reference to the datasource?
Any advice would be great. Thanks.