Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

It’s quite expensive to establish database connections. Database connection pooling is a well-established way to lower this expenditure.

In this tutorial, we’ll discuss how to use c3p0 with Hibernate to pool connections.

2. What Is c3p0?

c3p0 is a Java library that provides a convenient way for managing database connections.

In short, it achieves this by creating a pool of connections. It also effectively handles the cleanup of Statements and ResultSets after use. This cleanup is necessary to ensure that resource usage is optimized and avoidable deadlocks do not occur.

This library integrates seamlessly with various traditional JDBC drivers. Additionally, it provides a layer for adapting DriverManager-based JDBC drivers to the newer javax.sql.DataSource scheme.

And, because Hibernate supports connecting to databases over JDBC, it’s simple to use Hibernate and c3p0 together.

3. Configuring c3p0 With Hibernate

Let’s now look at how to configure an existing Hibernate application to use c3p0 as its database connection manager.

3.1. Maven Dependencies

Firstly, we’ll first need to add the hibernate-c3p0 maven dependency:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-c3p0</artifactId>
    <version>5.3.6.Final</version>
</dependency>

With Hibernate 5, just adding the above dependency is enough to enable c3p0. This is true as long as no other JDBC connection pool manager is specified.

Therefore, after we add the dependency, we can run our application and check the logs:

Initializing c3p0-0.9.5.2 [built 08-December-2015 22:06:04 -0800; debug? true; trace: 10]
Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@34d0bdb9 [ ... default settings ... ]

If another JDBC connection pool manager is being used, we can force our application to use c3p0. We just need to set the provider_class to C3P0ConnectionProvider in our properties file:

hibernate.connection.provider_class=org.hibernate.connection.C3P0ConnectionProvider

3.2. Connection Pool Properties

Eventually, we will need to override the default configuration. We can add custom properties to the hibernate.cfg.xml file:

<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.acquire_increment">5</property>
<property name="hibernate.c3p0.timeout">1800</property>

Likewise, the hibernate.properties file can contain the same settings:

hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.acquire_increment=5
hibernate.c3p0.timeout=1800

The min_size property specifies the minimum number of connections it should maintain at any given time. By default, it will maintain at least three connections. This setting also defines the initial size of the pool.

The max_size property specifies the maximum number of connections it can maintain at any given time. By default, it will keep a maximum of 15 connections.

The acquire_increment property specifies how many connections it should try to acquire if the pool runs out of available connections. By default, it will attempt to acquire three new connections.

The timeout property specifies the number of seconds an unused connection will be kept before being discarded. By default, connections will never expire from the pool.

We can verify the new pool settings by checking the logs again:

Initializing c3p0-0.9.5.2 [built 08-December-2015 22:06:04 -0800; debug? true; trace: 10]
Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@b0ad7778 [ ... new settings ... ]

These are the basic connection pool properties. In addition, other configuration properties can be found in the official guide.

5. Conclusion

In this article, we’ve discussed how to use c3p0 with Hibernate. We’ve looked at some common configuration properties and added c3p0 to a test application.

For most environments, we recommend using a connection pool manager such as c3p0 or HikariCP instead of traditional JDBC drivers.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW