HikariCP is a light-weight production ready JDBC connection pool. Learn about various options for configuring the HikariCP with Spring boot JPA and hibernate.
1. Maven
Given below are minimum dependencies, we need to provide through maven.
- spring-boot-starter-data-jpa : It helps in persisting data in SQL Databases with JPA using Spring Data and Hibernate.
- mysql-connector-java : It includes MySQL JDBC and R2DBC driver.
- spring-boot-starter-web : It helps in writing web layer and REST APIs for client interaction.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.0</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.howtodoinjava</groupId> <artifactId>hibernate-examples</artifactId> <version>0.0.1-SNAPSHOT</version> <name>hibernate-examples</name> <description>Demo project for Spring Boot</description> <properties> <java.version>15</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2. Configuring HikariCP with Spring boot and Hibernate
- Below are most common used properties for configuring the datasource and HikariCP connection pool. I will highly recommend to understand the usage of each property option and its impact on the application.
Use these properties on need basis, and define their values as per requirements.
- Go through HikariCP official github page for explanation of various configuration options.
- Use logging level related properties only in development mode.
Spring Boot configures Hibernate as the default JPA provider; so we don’t need to configure its related beans until we want to customize them.
Also, Till Spring Boot 1.x, the default connection pool was Tomcat. Now it has been changed to HikariCP with Spring Boot 2.x.
Bookmark this page listing all application.properties
option available for quick reference.
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect spring.jpa.hibernate.connection.provider_class=com.zaxxer.hikari.hibernate.HikariConnectionProvider spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/test_db spring.datasource.username=root spring.datasource.password=password spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.hikari.connection-timeout=50000 spring.datasource.hikari.idle-timeout=300000 spring.datasource.hikari.max-lifetime=900000 spring.datasource.hikari.maximum-pool-size=10 spring.datasource.hikari.minimum-idle=10 spring.datasource.hikari.pool-name=ConnPool spring.datasource.hikari.connection-test-query=select 1 from dual spring.datasource.hikari.data-source-properties.cachePrepStmts=true spring.datasource.hikari.data-source-properties.prepStmtCacheSize=250 spring.datasource.hikari.data-source-properties.prepStmtCacheSqlLimit=2048 spring.datasource.hikari.data-source-properties.useServerPrepStmts=true spring.datasource.hikari.data-source-properties.useLocalSessionState=true spring.datasource.hikari.data-source-properties.rewriteBatchedStatements=true spring.datasource.hikari.data-source-properties.cacheResultSetMetadata=true spring.datasource.hikari.data-source-properties.cacheServerConfiguration=true spring.datasource.hikari.data-source-properties.elideSetAutoCommits=true spring.datasource.hikari.data-source-properties.maintainTimeStats=false logging.level.com.zaxxer.hikari.HikariConfig=DEBUG logging.level.com.zaxxer.hikari=TRACE
3. Conclusion
This post is more of a self-note as well so I can refer to this page whenever want to explore different configuration options in a single page.
This example uses MySQL as backend database, so please use the appropriate property values if we are using a different database such as Oracle DB or Microsoft SQL Server.
Happy Learning !!