Setting Connection Pool in Tomcat 6.x

December 7, 2009

Hi,

Recently I was configuring a connection pool for Oracle database on Tomcat server. Hence thought of sharing the same with you all.  I am sure that everyone out there must have done this and found it simple. The reason for posting this is that most of my colleagues missed putting the driver JAR file in the server lib directory which is needed by tomcat to refer the driver classes at the start-up.

  1. Make sure you have the JDBC Drivers placed in the {tomcat.home}/lib directory. For Oracle 10g use ojdbc14_g.jar
  2. Create a file named context.xml in <<yourwebapp>>\WebContent\META-INF directory.
  3. Use Resource element to configure the connection pool as shown below.
  4. Sample context.xml file

    <?xml version="1.0" encoding="UTF-8"?>
    <Context antiResourceLocking="false" privileged="true" cookies="true">
    <Resource name="jdbc/mypool" auth="Container"
    type="javax.sql.DataSource"
    driverClassName="oracle.jdbc.OracleDriver"
    url="jdbc:oracle:thin:@<<host:port>>:<<sid>>"
    username=<<username goes here>>
    password=<<password goes here>>
    maxActive="20" maxIdle="10" maxWait="-1"/>
    </Context>
    
  5. Add the resource reference to the DataSource created in Step#3 to the web-application using web.xml.
  6. Refer the DataSource using “<res-ref-name>” as the JNDI name in your source.
  7. E.g.

    <resource-ref>
    <description>DataSource for Web Application</description>
    <res-ref-name>jdbc/mypool</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    </resource-ref>
    

Hope this helps you setting up the connection pool.

Note: Resource element used to configure the connection pool has lot of configurable properties. Only base minimum properties are used above. For further information on DBCP configuration you can refer DBCP Configuration Parameters

Advertisements