John Topley's Knowledgebase

Using A MySQL Data Source With Jakarta Tomcat

Sunday, 21 November 2004

These instructions describe how to configure the Jakarta Tomcat web container to use a MySQL data source. They've been tested using Tomcat 5.0.28 and MySQL 4.0.18, but should work with Tomcat 4 and other versions of MySQL too. Note that CATALINA_HOME refers to the root of the Tomcat installation folder.

  • The configuration is a three step process:
  1. Copy the MySQL JDBC driver JAR file to CATALINA_HOME/common/lib.
  2. Edit the CATALINA_HOME/conf/server.xml file so that it includes a context definition for the web application that will use the data source. The first code block below shows the main elements of server.xml and the second shows the detail of the context definition for a fictious web application called MyWebApp:
    <Server>
      <Service>
        <Engine>
          <Host>
            :
            <Context>
              <!-- Existing context definitions. -->
            </Context>
            :
            <!--
              | Paste in context definition shown below right here.
              +-->
          </Host>
        </Engine>
      </Service>
    </Server>
    <!--
       | Begin MyWebApp context definition.
       +-->
    <Context path="/MyWebApp" docBase="MyWebApp"
        reloadable="true">

      <Logger className="org.apache.catalina.logger.FileLogger"
          prefix="localhost_MyWebApp." suffix=".txt" timestamp="true"/>

      <Resource name="jdbc/MyWebAppDS" auth="Container"
          type="javax.sql.DataSource"/>

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

        <!--
          | The JDBC connection URL for connecting to your MySQL DB.
          | The autoReconnect=true argument to the URL makes sure that the
          | 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:mysql://localhost:3306/mywebapp_db_name?
              autoReconnect=true</value>
        </parameter>

        <!--
          | MySQL username and password for DB connections.
          +-->
        <parameter>
          <name>username</name>
          <value>username</value>
        </parameter>
        <parameter>
          <name>password</name>
          <value>password</value>
        </parameter>

        <!--
          | Class name for MySQL JDBC driver.
          +-->
        <parameter>
          <name>driverClassName</name>
          <value>com.mysql.jdbc.Driver</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>100</value>
        </parameter>

        <!--
          | Maximum number of idle DB connections to retain in pool.
          | Set to 0 for no limit.
          +-->
        <parameter>
          <name>maxIdle</name>
          <value>30</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>
      </ResourceParams>
    </Context>
    <!--
      | End MyWebApp context definition.
      +-->
  3. Edit the web application's web.xml file to add a resource reference definition for the JNDI data source:
    <resource-ref>
      <description>MyWebApp data source.</description>
      <res-ref-name>jdbc/MyWebAppDS</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
    </resource-ref>

top | index | previous | next | comments ()

home | archive | kb | media | about | contact | accessibility
Copyright © 2003 - 2005 John Topley. Made with CityDesk.