JPA Buddy
announcement - icon

JPA is huge! It covers nearly every aspect of communication between relational databases and the Java application and is deeply integrated into all major frameworks.

If you're using IntelliJ, JPA Buddy is super helpful. The plugin gently guides you through the subtleties of the most popular JPA implementations, visually reminds you of JPA features, generates code that follows best practices, and integrates intelligent inspections to improve your existing persistence code.

More concretely, it provides powerful tooling to generate Spring Data JPA repositories and methods, Flyway Versioned Migrations, Liquibase Differential Changelogs, DDL and SQL statements, DTO objects, and MapStruct interfaces.

Oh, and it actually generates JPA entities from an existing database and gradually update the data model as the database evolves! Yeah.

>> Become a lot more productive with JPA Buddy

Persistence top

Get started with Spring Data JPA through the reference Learn Spring Data JPA course:


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:


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- [built 08-December-2015 22:06:04 -0800; debug? true; trace: 10]
Initializing c3p0 pool... [email protected] [ ... 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:


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 file can contain the same settings:


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- [built 08-December-2015 22:06:04 -0800; debug? true; trace: 10]
Initializing c3p0 pool... [email protected] [ ... 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.

As usual, the complete source code for this tutorial is available on GitHub.

Persistence bottom
Get started with Spring Data JPA through the reference Learn Spring Data JPA course: >> CHECK OUT THE COURSE
Persistence footer banner
Comments are closed on this article!