Skip to main content

9.2 HikariCP Connection Pool Principles and Optimization

For a Java application to access an external database, it basically goes through a Java standard API called JDBC (Java Database Connectivity).

Understanding the Connection Pool​

Establishing and severing a connection to the database every time a client sends an API request to the server is a very heavy (costly) operation in terms of time and resources.

To solve this, we use an approach where a predetermined number of DB connections are established when the application loads and kept in a Pool.

  • When a request comes in, a connection is borrowed from the pool,
  • When the query execution is finished, it is returned to the pool.

Historically, Spring Boot uses HikariCP, the fastest and most stable open-source connection pool, as its Default.

application.yml Configuration​

For the application to connect to the DB via HikariCP, the Driver for the DB being used must first be added to the dependencies (build.gradle).

// MySQL
runtimeOnly 'com.mysql:mysql-connector-j'
// PostgreSQL
runtimeOnly 'org.postgresql:postgresql'

Next, fill out the connection information in the src/main/resources/application.yml file.

spring:
datasource:
# JDBC URL Setup
url: jdbc:mysql://localhost:3306/mydb?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver

# HikariCP Additional Settings (Vital for Performance)
hikari:
maximum-pool-size: 10 # Max number of connections in the pool
minimum-idle: 10 # Minimum number of idle connections
connection-timeout: 30000 # Max time to wait for a connection (ms)
idle-timeout: 600000 # Max time a connection can sit idle
max-lifetime: 1800000 # Max lifetime of a connection in the pool
pool-name: MyHikariCP # Name of the pool (useful for logging)

3. Separating Databases by Environment (Profiles)​

In real-world development, DB addresses for local, development (dev), and production (prod) servers are different. Spring's Profile feature allows you to separate these configurations.

  • application-dev.yml: DB settings for the development server
  • application-prod.yml: DB settings for the production server

You can decide which profile to use at runtime:

java -jar myapp.jar --spring.profiles.active=dev

4. Verifying Connection via Logs​

You can verify if Spring Boot has successfully connected to the database by checking the startup logs.

... o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http)
... com.zaxxer.hikari.HikariDataSource : MyHikariCP - Starting...
... com.zaxxer.hikari.HikariDataSource : MyHikariCP - Start completed.
... o.s.b.a.e.web.EndpointLinksResolver : Exposing 13 endpoint(s) beneath base path '/actuator'

If an error occurs after Starting..., check your JDBC URL, Username/Password, or network firewall settings.


tip

Thanks to Spring Boot's Auto Configuration, a DataSource bean is created automatically just by filling in the properties under spring.datasource. Not having to write manual Java boilerplate for DB setup is a major advantage of Spring Boot.