How to Configure HikariCP in Spring Boot Application
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
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<!-- hikari --> <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> </dependency> <!-- this one is need for Hikari Configuration, because we will use prefix configuration --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <!-- I use postgres --> <dependency> <groupId>postgresql</groupId> <artifactId>postgresql</artifactId> <version>9.4.1208-jdbc42-atlassian-hosted</version> </dependency> |
Step 2: application.yml
Just as all spring-boot application, configure your connectivity in properties file.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
spring: datasource: jdbc-url: jdbc:postgresql://localhost:5432/my_db username: my_db password: my_pass driver-class-name: org.postgresql.Driver hikari: connection-test-query: SELECT 1 jpa: hibernate: ddl-auto: none show-sql: true database-platform: org.hibernate.dialect.PostgreSQLDialect |
Step 3: JpaConfig.java
The last one, create a @Configuration
bean for database connectivity. This bean should extends com.zaxxer.hikari.HikariConfig
class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
@Configuration @ConfigurationProperties(prefix = "spring.datasource") public class JpaConfig extends HikariConfig { /** * Public constructor. */ public JpaConfig() { } /** * Getting {@link HikariDataSource} instance. * * @return {@link HikariDataSource} * @throws SQLException * exception */ @Bean public DataSource dataSource() throws SQLException { return new HikariDataSource(this); } } |
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!
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…
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.