使用Tomcat实现数据库连接池技术

1. Install Your JDBC Driver

Use of the JDBC Data Sources JNDI Resource Factory requires that you make an appropriate JDBC driver available to both Tomcat internal classes and to your web application. This is most easily accomplished by installing the driver's JAR file(s) into the $CATALINA_HOME/lib directory, which makes the driver available both to the resource factory and to your application.

2. Declare Your Resource Requirements

Next, modify the web application deployment descriptor (/WEB-INF/web.xml) to declare the JNDI name under which you will look up preconfigured data source. By convention, all such names should resolve to the jdbc subcontext (relative to the standard java:comp/env naming context that is the root of all provided resource factories. A typical web.xml entry might look like this: 
<resource-ref>
  <description>
        Resource reference to a factory for java.sql.Connection instances that may be used for talking to a particular     
        database that is configured in the <Context> configurartion for the web application. 
  </description>
  <res-ref-name>
        jdbc/EmployeeDB
  </res-ref-name>
  <res-type>
        javax.sql.DataSource
  </res-type>
  <res-auth>
         Container
  </res-auth>
</resource-ref>
 
WARNING - Be sure you respect the element ordering that is required by the DTD for web application deployment descriptors! See the Servlet Specification for details.

3. Code Your Application's Use Of This Resource

A typical use of this resource reference might look like this:
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup("jdbc/EmployeeDB");
Connection conn = ds.getConnection();
... use this connection to access the database ...
conn.close();
Note that the application uses the same resource reference name that was declared in the web application deployment descriptor. This is matched up against the resource factory that is configured in the <Context> element for the web application as described below.

4. Configure Tomcat's Resource Factory

To configure Tomcat's resource factory, add an element like this to the <Context> element for the web application.   在Tomcat服务器Server.xml的<Host>块中配置
<Context ...>
        ...
        <Resource name="jdbc/EmployeeDB" 
                        auth="Container" 
                        type="javax.sql.DataSource" 
                        username="dbusername" 
                        password="dbpassword" 
                        driverClassName="org.hsql.jdbcDriver" 
                        url="jdbc:HypersonicSQL:database" 
                        maxActive="8" 
                        maxIdle="4"/>
        ...
</Context>
Note that the resource name (here, jdbc/EmployeeDB) must match the value specified in the web application deployment descriptor.
This example assumes that you are using the HypersonicSQL database JDBC driver. Customize the driverClassName and driverName parameters to match your actual database's JDBC driver and connection URL.
The configuration properties for Tomcat's standard data source resource factory ( org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory) are as follows:
  • driverClassName - Fully qualified Java class name of the JDBC driver to be used.
  • maxActive - The maximum number of active instances that can be allocated from this pool at the same time.
  • maxIdle - The maximum number of connections that can sit idle in this pool at the same time.
  • maxWait - The maximum number of milliseconds that the pool will wait (when there are no available connections) for a connection to be returned before throwing an exception.
  • password - Database password to be passed to our JDBC driver.
  • url - Connection URL to be passed to our JDBC driver. (For backwards compatibility, the property driverName is also recognized.)
  • username - Database username to be passed to our JDBC driver.
  • validationQuery - SQL query that can be used by the pool to validate connections before they are returned to the application. If specified, this query MUST be an SQL SELECT statement that returns at least one row.