How to Configure Hikaricp in My Spring Boot App in My Application.Properties Files

How do I configure HikariCP in my Spring Boot app in my application.properties files?

@Configuration
@ConfigurationProperties(prefix = "params.datasource")
public class JpaConfig extends HikariConfig {

@Bean
public DataSource dataSource() throws SQLException {
return new HikariDataSource(this);
}

}

application.yml

params:
datasource:
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/myDb
username: login
password: password
maximumPoolSize: 5

UPDATED! Since version Spring Boot 1.3.0 :

  1. Just add HikariCP to dependencies
  2. Configure application.yml

application.yml

spring:
datasource:
type: com.zaxxer.hikari.HikariDataSource
url: jdbc:h2:mem:TEST
driver-class-name: org.h2.Driver
username: username
password: password
hikari:
idle-timeout: 10000

UPDATED! Since version Spring Boot 2.0.0 :

The default connection pool has changed from Tomcat to Hikari :)

Spring Boot 2: How to configure HikariCP using application.properties file

Remove PersistenceConfiguration class it’s not required. Spring Boot autoconfigures the data sources for you and flyway to use.

HikariCP is now the default pool implementation in spring boot 2.

Also remove all the data source related properties from application.properties except spring.datasource.url.

How do I configure HikariCP in my Spring Boot app in my applicationContext files?

In your case, HikariCP will be configured by default so you just need to configure the following optional properties related to hikari as below:

spring.datasource.url=jdbc:mysql://localhost:3306/{databasename}
spring.datasource.username={user}
spring.datasource.password={password}

spring.datasource.hikari.maximum-pool-size=12
spring.datasource.hikari.idle-timeout=300000

Spring Boot Hikari properties override programmatically

Do not extend HikariConfig.

Use the default constructor of HikariConfig() to take the default configuration properties of Hikari and then override what you need according to your properties file as you have already done for your jpa properties.

@Bean
public HikariDataSource verticaDataSource() {
HikariConfig hikariConfig = new HikariConfig();

hikariConfig.setConnectionTimeout(env.getProperty("vertica.datasource.hikari.connectionTimeout", Long.class));

hikariConfig.setIdleTimeout(env.getProperty("vertica.datasource.hikari.idleTimeout", Long.class));

hikariConfig.setMaxLifetime(env.getProperty("vertica.datasource.hikari.maxLifetime", Long.class));

hikariConfig.setKeepaliveTime(env.getProperty("vertica.datasource.hikari.keepaliveTime", Long.class));

hikariConfig.setMaximumPoolSize​(env.getProperty("vertica.datasource.hikari.maximumPoolSize", Integer.class));

hikariConfig.setMinimumIdle​(env.getProperty("vertica.datasource.hikari.minimumIdle", Integer.class));

return new HikariDataSource(hikariConfig);

}

Spring Boot Hikari configuration

  1. HikariCP is a connection pool, and a very good one. We've been using it in several projects in production and it's fast and just works.

  2. If you want to use HikariCP you use HikariDataSource. Spring Boot has started to use it as a default and recommends it (for the same reasons: it's fast and solid).

If you just use the default configuration with spring.datasource.url, it will use HikariCP and should work out-of-the-box.

However, when you manually configure your datasource(s), there is a small issue with Spring Boot 2 and HikariCP. HikariCP expects jdbcUrl or dataSourceClassName, but the Spring Boot configuration property uses url.
See the documentation or this question for that.

Default HikariCP connection pool starting Spring Boot application

That's no a connection leak but the desired behavior. Spring Boot Data uses HikariCP as a connection pool.

You can configure the max pool size as property. For example:

spring.datasource.hikari.maximum-pool-size=5


Related Topics



Leave a reply



Submit