🚀 Master Java & Backend Development!
Subscribe to Ram N Java for clean, beginner-friendly coding tutorials and architecture guides!
👉 Click Here to Subscribe NowWhat is C3P0 Connection Pooling?
In high-traffic Java web applications, opening and closing physical database connections repeatedly creates massive performance overhead. C3P0 is an open-source JDBC connection pool library that pre-initializes a pool of reusable connections. When your servlet requires database access, it simply borrows a connection from the pool and returns it when finished, significantly improving response times.
Step 1: Add Required JAR Files to Tomcat
To enable Tomcat and your application to use C3P0 with Oracle Database, copy the following JAR files directly into your Tomcat server's lib folder:
- C3P0 JARs: Download the C3P0 library and place
c3p0.jarandmchange-commons-java.jarintoTomcat/lib. - Oracle JDBC Driver: Add
ojdbc6.jar(or compatible driver) intoTomcat/libso Tomcat can communicate with Oracle.
Step 2: Configure the C3P0 DataSource in context.xml
Define the connection pool resource inside Apache Tomcat's context.xml file so it registers with the JNDI naming service:
auth="Container"
type="com.mchange.v2.c3p0.ComboPooledDataSource"
factory="org.apache.naming.factory.BeanFactory"
driverClass="oracle.jdbc.OracleDriver"
jdbcUrl="jdbc:oracle:thin:@localhost:1521:XE"
user="hr"
password="hr"
maxPoolSize="50"
minPoolSize="10"
acquireIncrement="10" />
Step 3: Reference the Resource in web.xml
Declare the resource reference inside your web application's web.xml deployment descriptor:
<description>Oracle DB Connection</description>
<res-ref-name>jdbc/hrDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Step 4: Retrieve and Use Connections in the Servlet
Inside your Servlet's init() method, perform a JNDI lookup to obtain the DataSource. Then, inside doGet(), retrieve a connection from the pool, execute SQL queries, and return output to the client:
Context envContext = (Context) initContext.lookup("java:comp/env");
DataSource ds = (DataSource) envContext.lookup("jdbc/hrDB");
try (Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT employee_id, first_name, last_name, email FROM employees")) {
while (rs.next()) {
out.println(rs.getInt("employee_id") + " - " + rs.getString("first_name"));
}
}
📺 Related Database & Connection Pooling Tutorials
Continue enhancing your Java backend knowledge with these related tutorials from our channel:
Click here to watch in Youtube : https://www.youtube.com/watch?v=Jkkd9qSuock&list=UUhwKlOVR041tngjerWxVccw&index=1
Click the below Image to Enlarge
![]() |
| JDBC|Servlets : Tomcat C3PO Connection Pooling - Oracle |
![]() |
| JDBC|Servlets : Tomcat C3PO Connection Pooling - Oracle |
![]() |
| JDBC|Servlets : Tomcat C3PO Connection Pooling - Oracle |
![]() |
| JDBC|Servlets : Tomcat C3PO Connection Pooling - Oracle |
![]() |
| JDBC|Servlets : Tomcat C3PO Connection Pooling - Oracle |
![]() |
| JDBC|Servlets : Tomcat C3PO Connection Pooling - Oracle |
![]() |
| JDBC|Servlets : Tomcat C3PO Connection Pooling - Oracle |
![]() |
| JDBC|Servlets : Tomcat C3PO Connection Pooling - Oracle |
![]() |
| JDBC|Servlets : Tomcat C3PO Connection Pooling - Oracle |
![]() |
| JDBC|Servlets : Tomcat C3PO Connection Pooling - Oracle |
![]() |
| JDBC|Servlets : Tomcat C3PO Connection Pooling - Oracle |
![]() |
| JDBC|Servlets : Tomcat C3PO Connection Pooling - Oracle |
































