I am refactoring others code. The one thing I notice is that of the manner on how the system is getting a connection from the connection pool.
Sample is like this.
Do it once in a ServletContextListener instead of everytime in init()
of many servlets. The contextInitialized()
method is executed only once during webapp's startup.
public class Config implements ServletContextListener {
private static final String ATTRIBUTE_NAME = "config";
private DataSource dataSource;
@Override
public void contextInitialized(ServletContextEvent event) {
ServletContext servletContext = event.getServletContext();
String databaseName = servletContext.getInitParameter("database.name");
try {
dataSource = (DataSource) new InitialContext().lookup(databaseName);
} catch (NamingException e) {
throw new RuntimeException("Config failed: datasource not found", e);
}
servletContext.setAttribute(ATTRIBUTE_NAME, this);
}
@Override
public void contextDestroyed(ServletContextEvent event) {
// NOOP.
}
public DataSource getDataSource() {
return dataSource;
}
public static Config getInstance(ServletContext servletContext) {
return (Config) servletContext.getAttribute(ATTRIBUTE_NAME);
}
}
Configure it as follows in web.xml
:
database.name
jdbc/mysqldb
com.example.Config
You can obtain it in your servlet as follows (init()
or doXXX()
method, you choose):
DataSource dataSource = Config.getInstance(getServletContext()).getDataSource();
I'd however refactor it a step further, JDBC code should preferably be placed in its own classes, not in servlets. Lookup the DAO pattern.