Problem using connection pooling in Tomcat 4.1.18

Tomcat gurus,

I'm hoping someone might be able to help me in configuring a connection pool (via JNDI) in Tomcat 4.1.18.  I've been working on this problem for the last six hours and I can't seem to figure-out what I'm doing wrong.  Here's my situation:

I have a servlet with which I want to use a connection pool to query a SQL Server 2000 database.  I'm using Tomcat 4.1.18 as the servlet container.  The <tomcat>/conf/server.xml and <webapp>/WEB-INF/web.xml files are configured as shown below:

--<tomcat>/conf/server.xml--
<Context path="/test" docBase="test" reloadable="true">
  <Resource name="jdbc/test" auth="Container" type="javax.sql.DataSource" />
  <ResourceParams name="jdbc/test">
    <parameter>
      <name>username</name>
      <value>myusername</value>
    </parameter>
    <parameter>
      <name>password</name>
      <value>mypassword</value>
    </parameter>
    <parameter>
      <name>driverClassName</name>
      <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
    </parameter>
    <parameter>
      <name>url</name>
      <value>jdbc:microsoft:sqlserver://mars:1433;DatabaseName=myTestDB</value>
    </parameter>
    <parameter>
      <name>maxActive</name>
      <value>8</value>
    </parameter>
    <parameter>
      <name>maxIdle</name>
      <value>4</value>
    </parameter>
  </ResourceParams>
</Context>

--web.xml--
  <servlet>
    <servlet-name>test</servlet-name>
    <servlet-class>test.test</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>test</servlet-name>
    <url-pattern>/test</url-pattern>
  </servlet-mapping>
  <resource-ref>
    <res-ref-name>jdbc/test</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>

The application code that creates a connection is as follows:
      Context initCtx = new InitialContext();
      Context envCtx = (Context) initCtx.lookup("java:comp/env");
      DataSource ds = (DataSource) envCtx.lookup("jdbc/test");

      connection = ds.getConnection();
      statement = connection.createStatement();

The problem that I have with the current code/configuration is that when I execute the servlet, a javax.naming.NamingException gets thrown.  The exact error is:
javax.naming.NamingException: Cannot create resource instance
        at org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:189)
        at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:301)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:834)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:181)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:822)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:194)
        at test.test.init(test.java:44)
        at javax.servlet.GenericServlet.init(GenericServlet.java:256)
        at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.
        ...

I know that the driver,url,username, and password parameters are all OK because I've modified the servlet to use DriverManager to create a connection and everything works fine.  Per the instructions in Tomcat's JNDI how-to, I have made sure that the SQL Server driver is in the <tomcat>/common/lib directory.  Additionally, I have recreated the example with a mySQL database (just to see if the problem happened to be specific to SQL Server) and I still receive the same javax.naming.NamingException.

What am I doing wrong?  Is there a component that I am forgetting to install or a configuration that I am forgetting to make?

Any suggestions will be helpful.
dnickelsAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
NetWizeConnect With a Mentor Commented:
it's supposed to be in the jakarta-commons-project:

"DBCP uses the Jakarta-Commons Database Connection Pool. It relies on number of Jakarta-Commons componenets:

Jakarta-Commons DBCP 1.0
Jakarta-Commons Collections 2.0
Jakarta-Commons Pool 1.0 "

(from tomcat 4.1 docs)
0
 
NetWizeCommented:
<parameter>
  <name>factory</name>
  <value>
    org.apache.commons.dbcp.BasicDataSourceFactory
  </value>
</parameter>      

to be added in the definition in your server.xml in <ResourceParams>

You tell Tomcat to create a "DataSource" but with your code you bound a "Connection" class i think.
0
 
dnickelsAuthor Commented:
Thanks for the suggestion NetWize.  I tried adding the parameter that you suggested specifying the factory, but I received another error:

javax.naming.NamingException: Could not create resource factory, ClassNotFoundException:org.apache.commons.dbcp.BasicDataSourceFactory
        at org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:127)
        at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:301)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:834)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:181)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:822)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:181)
...

It seems that Tomcat cannot find the BasicDataSourceFactory class.  I know that Tomcat's documentation states that Tomcat uses the BasicDataSourceFactory for JDBC connection pooling by default, but I don't understand how it does so if the file is not included with Tomcat (I have searched all of the jar files included with Tomcat and I have not found the BasicDataSourceFactory class in any of them).  Am I supposed to download the BasicDataSourceFactory from the Jakarta web site?  If anybody knows which jar file in which the BasicDataSourceFactory is supposed to be located, can you please pass along the info.  What should have been a relatively simple task to accomplish is becoming quite tedious and frustrating!
0
 
dnickelsAuthor Commented:
Thanks for the suggestion NetWize.  I tried adding the parameter that you suggested specifying the factory, but I received another error:

javax.naming.NamingException: Could not create resource factory, ClassNotFoundException:org.apache.commons.dbcp.BasicDataSourceFactory
        at org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:127)
        at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:301)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:834)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:181)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:822)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:181)
...

It seems that Tomcat cannot find the BasicDataSourceFactory class.  I know that Tomcat's documentation states that Tomcat uses the BasicDataSourceFactory for JDBC connection pooling by default, but I don't understand how it does so if the file is not included with Tomcat (I have searched all of the jar files included with Tomcat and I have not found the BasicDataSourceFactory class in any of them).  Am I supposed to download the BasicDataSourceFactory from the Jakarta web site?  If anybody knows which jar file in which the BasicDataSourceFactory is supposed to be located, can you please pass along the info.  What should have been a relatively simple task to accomplish is becoming quite tedious and frustrating!
0
 
NetWizeCommented:
So good old reload again... If you want to refresh a question use the "reload question" link in top left corner. Reloading via browser-refresh reposts your message (again and again and again...) :-)
0
All Courses

From novice to tech pro — start learning today.