Partner – JPA Buddy – NPI (tag=JPA/Hibernate)
announcement - icon

The right tools can and will save a lot of time. As long as you are using Hibernate and IntelliJ IDEA you can boost your coding speed and quality with JPA Buddy. It will help in a lot of the day-to-day work:

  • Creating JPA entities that follow best practices for efficient mapping
  • Creating DTOs from entities and MapStruct mappers using convenient visual tools
  • Generating entities from the existing database or Swagger-generated POJOs
  • Visually composing methods for Spring Data JPA repositories
  • Generating differential SQL to update your schema in accordance with your changes in entities
  • Autogenerating Flyway migrations and Liquibase changelogs comparing entities with the database or two databases
  • … and a lot more

Simply put, you'll learn and use the best practices of Hibernate and surrounding technology and become a lot more!

Definitely visit the JPA Buddy site to see its features in action closer.

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:

Persistence footer banner
Comments are closed on this article!