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... [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 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... [email protected] [ ... new settings ... ]
These are the basic connection pool properties. In addition, other configuration properties can be found in the official guide.
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.