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 :
- Just add HikariCP to dependencies
- 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
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.
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
Showing Morning, Afternoon, Evening, Night Message Based on Time in Java
Replace Whitespace in Json Keys
Createprocess Error=2, the System Cannot Find the File Specified
Reverse a String Without Affecting Special Characters
Could Not Read Json: Can Not Deserialize Instance of Hello.Country[] Out of Start_Object Token
Retrieving Data from Biometric Fingerprint Attendance Device
Flush/Clear System.In (Stdin) Before Reading
Gradle: How to Exclude a Particular Package from a Jar
How to Get Numbers from Given Gcd and Lcm
Spring Boot JPA Unknown Column in Field List
Junit 5 - No Parameterresolver Registered for Parameter
Check If One List Contains Element from the Other
Java.Exe Finished With Non-Zero Exit Value 1
Convert String With Dot or Comma to Float Number
Regex to Match Substring After Nth Occurence of Pipe Character
Tomcat Server Error - Port 8080 Already in Use
Java 8 Convert Utc Time to Edt/Est So That Date Remains the Same