Configuring C3p0 Connection Pooling with Hibernate

In this tutorial I will show you how to integrate third party connection pool – C3P0, with Hibernate. C3p0 is an easy-to-use library used for providing connection pooling capability.

First create a new Java Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml


1. Sql Script

Use the following Sql Script for creating table.

create table Employee(
    EMPLOYEE_ID int(10) primary key NOT NULL AUTO_INCREMENT,
    Name varchar(50));

2. C3P0 connection pool settings

Now create the hibernate configuration file hibernate.cfg.xml and add the c3p0 configuration details.

<property name="hibernate.c3p0.min_size">5</property>
    <property name="hibernate.c3p0.max_size">20</property>
    <property name="hibernate.c3p0.timeout">300</property>
    <property name="hibernate.c3p0.max_statements">50</property>
    <property name="hibernate.c3p0.idle_test_period">3000</property>

hibernate.c3p0.min_size – This is the minimum number of JDBC connections that C3P0 keeps ready at all times.
hibernate.c3p0.max_size – This is the maximum number of connections in the pool. An exception is thrown at runtime if this number is exhausted.
hibernate.c3p0.timeout – You specify the timeout period (in this case, 300 seconds) after which an idle connection is removed from the pool.
hibernate.c3p0.max_statements – A maximum of 50 prepared statements will be cached. Caching of prepared statements is essential for best performance with Hibernate.
hibernate.c3p0.idle_test_period – This is the idle time in seconds before a connection is automatically validated.

3. Pojo

Now create Employee class and write the following code.

package com.kruders.model.bean;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Table(name = "EMPLOYEE")
public class Employee {
	private Integer employeeId;

	private String name;
	public Employee() {
    public Employee(String name) { = name;
	public Integer getEmployeeId() {
		return employeeId;

	public void setEmployeeId(Integer employeeId) {
		this.employeeId = employeeId;

	public String getName() {
		return name;

	public void setName(String name) { = name;

3. Hibernate Configuration File

Hhibernate configuration file contains the following code.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
		"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
	<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
	<property name="hibernate.connection.username">root</property>
	<property name="hibernate.connection.password"></property>
	<property name="connection.pool_size">1</property>
	<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
	<property name="show_sql">true</property>
	<property name="hibernate.c3p0.min_size">5</property>
    <property name="hibernate.c3p0.max_size">20</property>
    <property name="hibernate.c3p0.timeout">300</property>
    <property name="hibernate.c3p0.max_statements">50</property>
    <property name="hibernate.c3p0.idle_test_period">3000</property>
	<mapping class="com.kruders.model.bean.Employee"></mapping>

4. Hibernate Utility Class

Now create HibernateUtil class. The HibernateUtil class helps in creating the SessionFactory from the Hibernate configuration file. A org.hibernate.SessionFactory is used to obtain org.hibernate.Session instances. A org.hibernate.Session represents a single-threaded unit of work. The org.hibernate.SessionFactory is a thread-safe global object that is instantiated once.

package com.kruders.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
    private static final SessionFactory sessionFactory = buildSessionFactory();
    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            return new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
    public static SessionFactory getSessionFactory() {
        return sessionFactory;

5. Run Program

Create class and run it as Java Application

package com.kruders.core;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.kruders.model.bean.Employee;
import com.kruders.util.HibernateUtil;

public class Main {
	public static void main(String args[]) throws InterruptedException {
		Session session = HibernateUtil.getSessionFactory().openSession();
	    Transaction transaction = null;
        try {
        	transaction = session.beginTransaction();
            Employee employee1 = new Employee("Puneet");
            Employee employee2 = new Employee("Sahil");
        }catch (HibernateException e) {
        } finally {

Checking the process list on mysql shows me 5 connections (my minimum pool size). To check process list use the following command :

show full processlist;

You will see the following output in MySql Workbench as shown in Figure 14.1

Figure 14.1 Figure 14.1

The folder structure of the example is shown below in Figure 14.2

Figure 14.2 Figure 14.2

You can download the source code of this example here.

No comments yet.

Leave a Reply