Java.HikariCP.How do you enable and configure HikariCP in a Spring Boot application?

<!-- For JDBC -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

<!-- Or for JPA -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

✅ 2. Configure application.properties or application.yml

Spring Boot auto-configures HikariCP when it detects the dependency. You can tune the pool using the spring.datasource.hikari.* properties.

Example application.properties:

# DB Connection
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=secret
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

# HikariCP Settings
spring.datasource.hikari.maximum-pool-size=10
spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.idle-timeout=30000
spring.datasource.hikari.max-lifetime=600000
spring.datasource.hikari.connection-timeout=30000
spring.datasource.hikari.pool-name=MyHikariCP

Or using application.yml:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mydb
    username: root
    password: secret
    driver-class-name: com.mysql.cj.jdbc.Driver
    hikari:
      maximum-pool-size: 10
      minimum-idle: 5
      idle-timeout: 30000
      max-lifetime: 600000
      connection-timeout: 30000
      pool-name: MyHikariCP

✅ 3. (Optional) Programmatic Configuration

If you need custom configuration in code:

@Bean
@ConfigurationProperties("spring.datasource.hikari")
public HikariDataSource dataSource() {
    return DataSourceBuilder.create().type(HikariDataSource.class).build();
}

✅ 4. Check It Works

You can enable debug logging to see HikariCP in logs:

logging.level.com.zaxxer.hikari=DEBUG

You should see something like:

HikariPool-1 - Starting...
HikariPool-1 - Start completed.

🧠 Bonus: Common Tuning Tips

  • maximum-pool-size: Set to number of concurrent connections your app needs.
  • minimum-idle: Minimum number of idle connections to keep in the pool.
  • idle-timeout: How long an idle connection lives before being removed.
  • max-lifetime: Lifetime of a connection before it’s recycled (avoid DB timeouts).
  • connection-timeout: How long to wait before failing to get a connection.
This entry was posted in Без рубрики. Bookmark the permalink.

Leave a Reply

Your email address will not be published.