HikariCP is a “zero-overhead” production-quality connection pool.
Introduction
This is the second part of a series of posts focused on Hibernate and JPA. In this tutorial we are going to look at database
pooling using HikariCP for Hibernate and JPA.
A connection pool is a cache of database connections maintained so that the connections can be reused when future requests to the
database are required. Connecting to a data source can be time consuming. To minimize the cost of opening connections, you need to
implement a pooling mechanism in your application.
To add the HikariCP dependency to the project replace the hibernate-core dependency with hibernate-hikaricp in our
pom.xml file:
JPA Configuration
The next change will be our persistence.xml file.
file: src/main/resources/META-INF/persistence.xml:
We modified the persistence.xml file to have the following HikariCP specific configuration.
hibernate.hikari.minimumIdle
hibernate.hikari.maximumPoolSize
hibernate.hikari.idleTimeout
hibernate.connection.provider_class
We set the pool size to 10. In a production system you may set this value a little higher.
Another change from the original post is in the PersonRepository
class. This change is trivial and does not impact HikariCP pooling in any way. It is just an assertive switch to
Java8’s Optional to better handle null values.
In this post we understood the basics of setting up HikariCP with Hibernate and JPA with minimal configuration.
As usual you can find the full example to this guide in the github repository. Until the next post, keep doing cool things .