How to Configure HikariCP in Spring Boot Application

Sharing is caring!

Overview

In Java world, when it comes to database connectivity, there are two main approaches to deal with. First one is direct JDBC connection by using DriverManager. The other one is by using data source. The latter is more popular due to its abstraction and scalability (related with connection pooling).

Because data source is so popular, there are many libraries out there. Such as c3p0, DBCP, Tomcat and HikariCP. In this article, I will choose HikariCP because its amazing performance. I said it is amazing because it is a very lightweight (at roughly 130Kb) and lightning fast JDBC connection pooling frameworkยน. If you visit the HikariCP page, you will see fascinating benchmark in comparison with other libraries.

Step-by-step

As I stated in the title, I will use spring-boot as a framework and HikariCP as its connection pooling. Use start.spring.io to generate spring-boot project with JPA dependency.

Step 1: pom.xml

In order to include HikariCP in your application, first you need to add library dependencies inside your pom.xml.

Step 2: application.yml

Just as all spring-boot application, configure your connectivity in properties file.

Step 3: JpaConfig.java

The last one, create a @Configuration bean for database connectivity. This bean should extends com.zaxxer.hikari.HikariConfig class.

That’s all. Now you can execute mvn spring-boot:run, then you will see in the output the database successfully connected. The rest of your tasks are to create spring-data-repository in order to make a CRUD operation.

Conclusion

Spring boot never fail to impress me so far. Every thing is very straightforward and transparent. This HikariCP connectivity is one of the proof of its simplicity. I can make a database connection pooling in three simple steps. So far I am very satisfied with it.

That’s all from me for now, Merry Christmas and Happy Holiday to the world!

Author: ru rocker

I have been a professional software developer since 2004. Java, Python, NodeJS, and Go-lang are my favorite programming languages. I also have an interest in DevOps. I hold professional certifications: SCJP, SCWCD, PSM 1, AWS Solution Architect Associate, and AWS Solution Architect Professional.

3 thoughts on “How to Configure HikariCP in Spring Boot Application”

  1. Nice. But what if I want to get the DB connection user & pwd interactively? HikariCP it’s not very friendly to stop and start the connections as far as I’ve seen…

    1. As far as I know, the configuration purpose is needed when you bootstrap your application. And it is better to make it start as a service and make it auto start (systemctl enable service). Therefore, I don’t see any need for make it interactively. But it depends your need. Maybe you can pass it when execute the program by embedding the -D options.

Leave a Reply

Your email address will not be published. Required fields are marked *