Connecttion Pool issues - Sample App
Hello,
I'm having problems getting my connection pool to initialize in the sample app in chps. 19-20.
I have it set up similar to how the book describes and I cannot get past this error:
java.lang.IllegalStateException: Pool not initialized.
at com.harrisalternatives.struts.util.ConnectionPool. getInstance(ConnectionPool.java:33)
I'm using the com.mysql.jdbc.Driver instead of the one in the book and the BasicDataSource instead of the GenericDataSource in my DBInitServlet class, but everything else is the same.
Can anyone help me? Below are snippets of my code:
*web.xml:
<servlet>
<servlet-name>dbInit</servlet-name>
<servlet-class>com.harrisalternatives.struts.util.DBInitSer vlet</servlet-class>
<init-param>
<param-name>driverClass</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</init-param>
<init-param>
<param-name>jdbcURL</param-name>
<param-value>jdbc:mysql://10.10.10.4:3306/testdb</param-value>
</init-param>
<init-param>
<param-name>minCount</param-name>
<param-value>1</param-value>
</init-param>
<init-param>
<param-name>maxCount</param-name>
<param-value>10</param-value>
</init-param>
<init-param>
<param-name>username</param-name>
<param-value>tomcat</param-value>
</init-param>
<init-param>
<param-name>password</param-name>
<param-value>tomcat</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
public class DBInitServlet extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
super.init(config);
try {
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName(getInitParameter("driverClas s"));
ds.setUrl(getInitParameter("jdbcURL"));
ds.setMaxActive(Integer.parseInt(getInitParameter( "maxActive")));
ds.setMaxWait(Integer.parseInt(getInitParameter("m axWait")));
ds.setDefaultAutoCommit(false);
ds.getConnection();
ConnectionPool.init(ds);
} catch (SQLException e) {
e.printStackTrace();
throw new ServletException("Unable to open datasource");
}
}
}
public class ConnectionPool {
private DataSource ds;
private static ConnectionPool mySelf;
private ConnectionPool(DataSource ds) {
this.ds = ds;
}
public static void init(DataSource ds) {
mySelf = new ConnectionPool(ds);
}
public static ConnectionPool getInstance() {
if (mySelf == null) {
throw new IllegalStateException("Pool not initialized.");
}
return mySelf;
}
public Connection getConnection() throws SQLException {
return ds.getConnection();
}
}
Any help would be greatly appreciated!
Thanks,
Mike
|