Monday, December 21, 2020

Connection Pooling Using C3P0 in Java

In this post we’ll see how to configure connection pooling using C3P0 datasource in your Java application. The DB we are connecting to is MySQL.

Jars needed for C3P0

You need the following jars in your project’s classpath, check the versions as per your Java and DB versions.


If you are using Maven then you can add the following dependency.


Connection pooling using C3P0 - Java Example

Properties file that is used to read DB configuration



In the Java example code for connection pooling using C3P0 there are two Java classes. We have a PooledDataSource class with a static block to create an instance of C3P0's ComboPooledDataSource.

There is another class DSConnection where we get the instance of ComboPooledDataSource and use it to get the Connection object.

import java.beans.PropertyVetoException;
import java.util.Properties;
import com.mchange.v2.c3p0.ComboPooledDataSource;

public class PooledDataSource {
  private static ComboPooledDataSource cpds;
  static {
    try {
      cpds = new ComboPooledDataSource();
      Properties properties = new Properties();
      // Loading properties file
      InputStream inputStream = new FileInputStream("resources/");   
      cpds.setDriverClass(properties.getProperty("DRIVER_CLASS")); //loads the jdbc driver            
      // the settings below are optional 
      // c3p0 can work with defaults
    }catch(IOException | PropertyVetoException e) {
  public static javax.sql.DataSource getDataSource() {
    return cpds;

In this class, apart from setting the DB properties, we have set some of the parameters for the connection pool like setMinPoolSize() that sets the initial size of the connection pool. These many connection will immediately be created and put to connection pool, setMaxPoolSize() to set the maximum limit on the connection pool.

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;

public class DSConnection {
  public static void main(String[] args) throws PropertyVetoException {
    DSConnection dsCon = new DSConnection();
    try {
    } catch (SQLException e) {
      // TODO Auto-generated catch block
  private void displayEmployee(int id) throws SQLException{		
    Connection connection = null; 
    String selectSQL = "Select * from employee where id = ?";
    PreparedStatement prepStmt = null;
    try {
      DataSource ds = PooledDataSource.getDataSource();
      connection = ds.getConnection();
      prepStmt = connection.prepareStatement(selectSQL);
      prepStmt.setInt(1, id);
      ResultSet rs = prepStmt.executeQuery();
        System.out.println("id: " + rs.getInt("id") + " Name: " 
            + rs.getString("name") + " Age: " + rs.getInt("age")); 
      if(prepStmt != null){
      if(connection != null){

That's all for this topic Connection Pooling Using C3P0 in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Programs Page

Related Topics

  1. Connection Pooling Using Apache DBCP in Java
  2. Java Program to Get All DB Schemas
  3. Java Program to Get All The Tables in a DB Schema
  4. DataSource in Java-JDBC
  5. Convert String to Byte Array Java Program

You may also like-

  1. How to Count Lines in a File in Java
  2. Creating Tar File And GZipping Multiple Files in Java
  3. How to Iterate a HashMap of ArrayLists of String in Java
  4. Find Largest and Second Largest Number in Given Array Java Program
  5. Fail-Fast Vs Fail-Safe Iterator in Java
  6. Externalizable Interface in Java
  7. Spring NamedParameterJdbcTemplate Insert, Update And Delete Example
  8. Ternary Operator in Java With Examples