?
Solved

Problem using connection pooling in Tomcat 4.1.18

Posted on 2003-03-12
7
Medium Priority
?
1,230 Views
Last Modified: 2012-05-04
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.
0
Comment
Question by:dnickels
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
7 Comments
 
LVL 3

Expert Comment

by:NetWize
ID: 8125864
<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
 

Author Comment

by:dnickels
ID: 8130472
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
 
LVL 3

Accepted Solution

by:
NetWize earned 300 total points
ID: 8130599
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
 

Author Comment

by:dnickels
ID: 8131377
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
 
LVL 3

Expert Comment

by:NetWize
ID: 8131533
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

Featured Post

Flexible connectivity for any environment

The KE6900 series can extend and deploy computers with high definition displays across multiple stations in a variety of applications that suit any environment. Expand computer use to stations across multiple rooms with dynamic access.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Convert websphere application server default chained Certificates from 1024 to 2048 keysize or higher size and also you can change signatureAlgorithm . Please make sure Websphere Application Server fixpack 7.0.0.23 or Above. The following steps a…
ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month9 days, 22 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question