Setting Up a Connection Pool on the Tomcat Web Server

See Also

A JDBC connection pool is a group of reusable connections that a web server or application server maintains for a particular database.  Applications requesting a connection to a database obtain that connection from the pool. When an application closes a connection, the connection is returned to the pool. Connection pool properties may vary with different database vendors. Some common properties are the URL for the database name, user name, and password.

For detailed information on Tomcat's database connection pooling functionality, see:

The first step in creating a Tomcat database connection pool is to create JDBC resource (also called a data source). A JDBC resource provides applications with a connection to a database. Typically, there is at least one JDBC resource for each database accessed by an application deployed in a domain. It is possible to have more than one JDBC resource for a database. You can create a JDBC resource manually in your server.xml or you can use Tomcat's Administration Tool to do so.

To add a JDBC resource using the Tomcat Web Server Administration Tool:

  1. Access the Tomcat Web Server Administration Tool.
  2. In the Administration Tool, click Data Sources.
  3. Choose Create New Data Source from the Data Source Actions drop down on the right.
  4. Type values such as the following to define your data source:
  5. Click Save. Then click Commit Changes. Then click Log Out.
  6. Reference the JDBC resource from your web application as described below.

To add a JDBC resource manually in the server.xml file:
Be aware that you hand-edit the server.xml file at your own risk; the IDE cannot repair a damaged server.xml file. You are strongly encouraged to create a backup version of your working server.xml file before beginning to edit by hand.

  1. In the Runtime window, expand the Servers node and the Tomcat 5 Servers node.
  2. Stop the server by right-clicking the Tomcat instance node and choosing Stop.
  3. Right-click the Tomcat instance node and choose Edit server.xml from the contextual menu to open the server.xml file in the Source Editor.
  4. Make your changes.
  5. Reference the JDBC resource from your web application as described below.

To reference a JDBC resource from a web application:

  1. Expand the project node in the Projects window. Then expand the Web Pages node and the WEB-INF node. Double-click the web.xml node and use the Source Editor to add your resource reference to the web.xml file as follows:

        	  <resource-ref>
    	      <description>Tomcat DBCP</description>
    	      <res-ref-name>jdbc/poolDB</res-ref-name>
    	      <res-type>javax.sql.DataSource</res-type>
    	      <res-auth>Container</res-auth>
    	  </resource-ref>
  2. Expand the META-INF node. Right-click the context.xml node, choose Edit from the contextual menu and use the Source Editor to add the following resource link between the <context> tags in the context.xml file:
              <ResourceLink name="jdbc/poolDB" type="javax.sql.DataSource" global="jdbc/poolDB"/>
    Note: Do not double-click the context.xml file. If you do so, the IDE opens the context.xml file in the Context Editor instead of the Source Editor. You cannot add a resource link in the Context Editor. As you cannot open the context.xml file in both the Source Editor and the Context Editor at the same time, the IDE disables Edit in the contextual menu if the context.xml file is opened in the Context Editor.

    Your web application's META-INF/context.xml file should now look similar to the following:

        	  <Context path="/Employees">
    	      <ResourceLink name="jdbc/poolDB" type="javax.sql.DataSource" 
    	       global="jdbc/poolDB"/>
    	      <Logger className="org.apache.catalina.logger.FileLogger" 
    	       prefix="Employees" suffix=".log" timestamp="true"/>
    	  </Context>
  3. Finally, use the JDBC resource in your web application.
See Also
About the Tomcat Web Server
Setting a Username and Password for the Tomcat Manager
Defining a Tomcat Database Connection Pool in a JSP File

Legal Notices