how to use tomcat datasources

an onjava article on the subject

tomcat reference

JDBC 2.0 tutorial

jdbc odbc bridge 1.4.2 to see if it supports data sources

<Context path="/dbcp" docBase="dbcp" debug="5"
reloadable="true" crossContext="true">

<Resource name="jdbc/TestDB" auth="Container"
   type="javax.sql.DataSource" removeAbandoned="true"
   removeAbandonedTimeout="30" maxActive="100"
   maxIdle="30" maxWait="10000" username="kunal"
   password="java_facier"
   driverClassName="com.mysql.jdbc.Driver"
   url="jdbc:mysql://localhost/dbcptest"/>

</Context>

<listener>
        <listener-class> com.onjava.dbcp.DBCPoolingListener</listener-class>
</listener>

<!-- This component has a dependency on an external resource-->
 <resource-ref>
      <description> DB Connection Pooling</description>
      <res-ref-name> jdbc/TestDB</res-ref-name>
      <res-type> javax.sql.DataSource</res-type>
      <res-auth> Container</res-auth>
  </resource-ref>

<servlet>
        <servlet-name> EnrolledStudents</servlet-name>
        <servlet-class> com.onjava.dbcp.CourseEnrollmentServlet</servlet-class>
        <load-on-startup> 1</load-on-startup>
</servlet>

<servlet-mapping>
        <servlet-name> EnrolledStudents</servlet-name>
        <url-pattern> /enrollment.do</url-pattern>
</servlet-mapping>

understand servlet context listener interface

    // Obtain our environment naming context
    Context envCtx = (Context) new InitialContext().
    lookup("java:comp/env");

    // Look up our data source
    DataSource  ds = (DataSource) envCtx.lookup
       ("jdbc/TestDB");

what is env context in jndi?

request.AppObjects.connectionManager.className=com.ai.db.cpjndi.ConnectionPoolConnectionManager5

Nothing else is required

So make sure the tomcat resource names are lower cased after the jdbc/

Follow the tomcat how to to place the needed jar files where they go

You need the build 23.5

No need to copy these jar files. The basic jars to check for are

dbcp
pool
commons collections
jndi naming jar files

Tomcat recommends that the jdbc jar files be in commons/lib and be named *.jar

How to preload the dbcp with a preset number of connections?

Have the initiaizer get the datasource before hand

Document each property of the dbcp pool. locate a ref document

at least use the preload data sources in aspire

I am using Tomcat5.0.28. I have also faced the same problem and could able to solve through the following steps.

1. Created Datasource using Manager application which in turn create datasource elements in server.xml under .

2. Navigate to D:\jakarta-tomcat-5.0.28\conf\Catalina\localhost\.xml file and inserted the following:

<ResourceLink name="jdbc/testDataSource" type="javax.sql.DataSource" global="jdbc/testDataSource"/>

and restarted the Tomcat, it is picking up now. It worked fine even after I deleted the datasource configuration from web.xml of the application.

<Context path="/akc" docBase="w:/akc" debug="0">
      
  <Resource name="jdbc/reportsdb"
               auth="Container"
               type="javax.sql.DataSource"/>

  <ResourceParams name="jdbc/reportsdb">
    <parameter>
      <name>factory</name>
      <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
    </parameter>

    <!-- Maximum number of dB connections in pool. Make sure you
         configure your mysqld max_connections large enough to handle
         all of your db connections. Set to 0 for no limit.
         -->
    <parameter>
      <name>maxActive</name>
      <value>10</value>
    </parameter>

    <!-- Maximum number of idle dB connections to retain in pool.
         Set to -1 for no limit.  See also the DBCP documentation on this
         and the minEvictableIdleTimeMillis configuration parameter.
         -->
    <parameter>
      <name>maxIdle</name>
      <value>5</value>
    </parameter>

    <!-- Maximum time to wait for a dB connection to become available
         in ms, in this example 10 seconds. An Exception is thrown if
         this timeout is exceeded.  Set to -1 to wait indefinitely.
         -->
    <parameter>
      <name>maxWait</name>
      <value>10000</value>
    </parameter>

    <!-- MySQL dB username and password for dB connections  -->
    <parameter>
     <name>username</name>
     <value>none</value>
    </parameter>
    <parameter>
     <name>password</name>
     <value>none</value>
    </parameter>

    <parameter>
       <name>driverClassName</name>
       <value>sun.jdbc.odbc.JdbcOdbcDriver</value>
    </parameter>
    
    <!-- The JDBC connection url for connecting to your MySQL dB.
         The autoReconnect=true argument to the url makes sure that the
         mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
         connection.  mysqld by default closes idle connections after 8 hours.
         -->
    <parameter>
      <name>url</name>
      <value>jdbc:odbc:akc-reportsDB</value>
    </parameter>
  </ResourceParams>

      
</Context>

dbcp documentation for the tags

Please note that JNDI resource configuration has changed somewhat between Tomcat 5.0.x and Tomcat 5.5.x. You will most likely need to modify your JNDI resource configurations to match the syntax in the example below in order to make them work in Tomcat 5.5.x.

tomcat 5028 comes with dbcp 121

that means it supports the initial set of of connections

here is the source code for the connectionpool wrapper in aspire