🚀 Master Java, JDBC & Web Technologies!
Subscribe to Ram N Java for in-depth, beginner-friendly coding tutorials and architecture guides!
👉 Click Here to Subscribe NowWhy Use C3P0 Connection Pooling with MySQL?
Creating database connections on every user request leads to high latency and heavy memory usage. C3P0 solves this problem by maintaining a pool of pre-established database connections. Instead of opening a brand-new connection each time, your Java Servlet borrows an existing connection from the pool, runs the SQL queries, and releases it back to the pool immediately.
Step 1: Add Required Libraries to Tomcat
Make sure the following JAR files are placed inside the Apache Tomcat/lib folder:
- c3p0.jar & mchange-commons-java.jar: The core C3P0 connection pooling binaries.
- mysql-connector-java.jar: The MySQL JDBC driver to allow Tomcat to communicate with your MySQL server.
Step 2: Configure C3P0 DataSource in context.xml
Define the MySQL connection pool resource inside your Tomcat configuration (META-INF/context.xml or conf/context.xml):
auth="Container"
type="com.mchange.v2.c3p0.ComboPooledDataSource"
factory="org.apache.naming.factory.BeanFactory"
driverClass="com.mysql.jdbc.Driver"
jdbcUrl="jdbc:mysql://localhost:3306/testdb"
user="root"
password="root"
maxPoolSize="50"
minPoolSize="10"
acquireIncrement="5" />
Step 3: Declare the Resource Reference in web.xml
Inform your web application about the external DataSource resource in your web.xml deployment descriptor:
<description>MySQL Connection Pool</description>
<res-ref-name>jdbc/mysqldb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Step 4: Perform JNDI Lookup and Query Data in Servlet
Inside your servlet, look up the DataSource using JNDI and fetch records from MySQL:
Context envContext = (Context) initContext.lookup("java:comp/env");
DataSource ds = (DataSource) envContext.lookup("jdbc/mysqldb");
try (Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT id, username, email FROM users")) {
while (rs.next()) {
out.println(rs.getInt("id") + " : " + rs.getString("username"));
}
}
📺 Related MySQL & Connection Pooling Tutorials
Check out these related practical guides from our channel:
Click here to watch in Youtube : https://www.youtube.com/watch?v=5gSn9zt5qrA&list=UUhwKlOVR041tngjerWxVccw&index=2
Click the below Image to Enlarge
![]() |
| JDBC|Servlets : Tomcat C3PO Connection Pooling - Mysql |
![]() |
| JDBC|Servlets : Tomcat C3PO Connection Pooling - Mysql |
![]() |
| JDBC|Servlets : Tomcat C3PO Connection Pooling - Mysql |
![]() |
| JDBC|Servlets : Tomcat C3PO Connection Pooling - Mysql |
![]() |
| JDBC|Servlets : Tomcat C3PO Connection Pooling - Mysql |
![]() |
| JDBC|Servlets : Tomcat C3PO Connection Pooling - Mysql |
![]() |
| JDBC|Servlets : Tomcat C3PO Connection Pooling - Mysql |
![]() |
| JDBC|Servlets : Tomcat C3PO Connection Pooling - Mysql |
![]() |
| JDBC|Servlets : Tomcat C3PO Connection Pooling - Mysql |
![]() |
| JDBC|Servlets : Tomcat C3PO Connection Pooling - Mysql |
![]() |
| JDBC|Servlets : Tomcat C3PO Connection Pooling - Mysql |



























