Database Connection Pooling in Tomcat

1. Open context.xml file, either in <tomcat installation directory>/conf or in META-INF folder of the web application for which you want to enable database connection pooling. If you do not have one in META-INF directory, create one and copy the contents of the context.xml in <tomcat installation directory>/conf.

2. Copy the following and paste with in the <context> element.  I have it for oracle10g database. You can modify it to suit your database. The following configuration takes care of resource leaks when you forget closing connections or result sets or so.

Connection Pooling will not work for databases with out password.

<Resource name=“Jdbc/myDB”




url=your database Connection URL

username=database user name

password=database password maxActive=“20” maxIdle=“10”




3.  Copy the following and paste it in web.xml of your

web application


<description>My connection Pooling</description>





4.  Code some thing like following to get the Connection

public Connection getMyDBConnection() {

Connection connection =null;


Context initCTX = new InitialContext();

Context envCtx = (Context) initCTX.lookup(“Jdbc/myDB “);

DataSource ds = (DataSource)envCtx.lookup(“Jdbc/QCToolDB”);

connection = ds.getConnection();

}catch(Exception e){



return connection;