I came across HikariCP
and I was amazed by the benchmarks and I wanted to try it instead of my default choice C3P0
and to my surprise I struggled to get the configurations
right probably because the configurations differ based on what combination of tech stack you are using.
I have setup Spring Boot
project with JPA, Web, Security
starters (Using Spring Initializer) to use PostgreSQL
as a database with HikariCP
as connection pooling.
I have used Gradle
as build tool and I would like to share what worked for me for the following assumptions:
- Spring Boot Starter JPA (Web & Security - optional)
- Gradle build too
- PostgreSQL running and setup with a database (i.e. schema, user, db)
You need the following build.gradle
if you are using Gradle
or equivalent pom.xml
if you are using maven
buildscript {
ext {
springBootVersion = '1.5.8.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'
group = 'com'
version = '1.0'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-aop')
// Exclude the tomcat-jdbc since it's used as default for connection pooling
// This can also be achieved by setting the spring.datasource.type to HikariCP
// datasource see application.properties below
compile('org.springframework.boot:spring-boot-starter-data-jpa') {
exclude group: 'org.apache.tomcat', module: 'tomcat-jdbc'
}
compile('org.springframework.boot:spring-boot-starter-security')
compile('org.springframework.boot:spring-boot-starter-web')
runtime('org.postgresql:postgresql')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.security:spring-security-test')
// Download HikariCP but, exclude hibernate-core to avoid version conflicts
compile('com.zaxxer:HikariCP:2.5.1') {
exclude group: 'org.hibernate', module: 'hibernate-core'
}
// Need this in order to get the HikariCPConnectionProvider
compile('org.hibernate:hibernate-hikaricp:5.2.11.Final') {
exclude group: 'com.zaxxer', module: 'HikariCP'
exclude group: 'org.hibernate', module: 'hibernate-core'
}
}
There are a bunch of excludes in the above build.gradle
and that's because
- First exclude, instructs gradle that exclude the
jdbc-tomcat
connection pool when downloading the spring-boot-starter-data-jpa
dependencies. This can be achieved by setting up the spring.datasource.type=com.zaxxer.hikari.HikariDataSource
also but, I don't want an extra dependency if I don't need it
- Second exclude, instructs gradle to exclude
hibernate-core
when downloading com.zaxxer
dependency and that's because hibernate-core
is already downloaded by Spring Boot
and we don't want to end up with different versions.
- Third exclude, instructs gradle to exclude
hibernate-core
when downloading hibernate-hikaricp
module which is needed in order to make HikariCP use org.hibernate.hikaricp.internal.HikariCPConnectionProvider
as connection provider instead of deprecated com.zaxxer.hikari.hibernate.HikariConnectionProvider
Once I figured out the build.gradle
and what to keep and what to not, I was ready to copy/paste a datasource
configuration into my application.properties
and expected everything to work with flying colors but, not really and I stumbled upon the following issues
- Spring boot failing to find out database details (i.e. url, driver) hence, not able to setup jpa and hibernate (because I didn't name the property key values right)
- HikariCP falling back to
com.zaxxer.hikari.hibernate.HikariConnectionProvider
- After instructing Spring to use new connection-provider for when auto-configuring hibernate/jpa then HikariCP failed because it was looking for some
key/value
in the application.properties
and was complaining about dataSource, dataSourceClassName, jdbcUrl
. I had to debug into HikariConfig, HikariConfigurationUtil, HikariCPConnectionProvider
and found out that HikariCP
could not find the properties from application.properties
because it was named differently.
Anyway, this is where I had to rely on trial and error and make sure that HikariCP
is able to pick the properties (i.e. data source that's db details, as well as pooling properties) as well as Sping Boot behave as expected and I ended up with the following application.properties
file.
server.contextPath=/
debug=true
# Spring data source needed for Spring boot to behave
# Pre Spring Boot v2.0.0.M6 without below Spring Boot defaults to tomcat-jdbc connection pool included
# in spring-boot-starter-jdbc and as compiled dependency under spring-boot-starter-data-jpa
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.url=jdbc:postgresql://localhost:5432/somedb
spring.datasource.username=dbuser
spring.datasource.password=dbpassword
# Hikari will use the above plus the following to setup connection pooling
spring.datasource.hikari.minimumIdle=5
spring.datasource.hikari.maximumPoolSize=20
spring.datasource.hikari.idleTimeout=30000
spring.datasource.hikari.poolName=SpringBootJPAHikariCP
spring.datasource.hikari.maxLifetime=2000000
spring.datasource.hikari.connectionTimeout=30000
# Without below HikariCP uses deprecated com.zaxxer.hikari.hibernate.HikariConnectionProvider
# Surprisingly enough below ConnectionProvider is in hibernate-hikaricp dependency and not hibernate-core
# So you need to pull that dependency but, make sure to exclude it's transitive dependencies or you will end up
# with different versions of hibernate-core
spring.jpa.hibernate.connection.provider_class=org.hibernate.hikaricp.internal.HikariCPConnectionProvider
# JPA specific configs
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.use_sql=true
spring.jpa.properties.hibernate.id.new_generator_mappings=false
spring.jpa.properties.hibernate.default_schema=dbschema
spring.jpa.properties.hibernate.search.autoregister_listeners=false
spring.jpa.properties.hibernate.bytecode.use_reflection_optimizer=false
# Enable logging to verify that HikariCP is used, the second entry is specific to HikariCP
logging.level.org.hibernate.SQL=DEBUG
logging.level.com.zaxxer.hikari.HikariConfig=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
As shown above the configurations are divided into categories based on following naming patterns
- spring.datasource.x (Spring auto-configure will pick these, so will HikariCP)
- spring.datasource.hikari.x (HikariCP picks these to setup the pool, make a note of the camelCase field names)
- spring.jpa.hibernate.connection.provider_class (Instructs Spring to use new HibernateConnectionProvider)
- spring.jpa.properties.hibernate.x (Used by Spring to auto-configure JPA, make a note of the field names with underscores)
It's hard to come across a tutorial or post or some resource that shows how the above properties file is used and how the properties should be named. Well, there you have it.
Throwing the above application.properties
with build.gradle
(or at least similar) into a Spring Boot JPA project version (1.5.8) should work like a charm and connect to your pre-configured database (i.e. in my case it's PostgreSQL that both HikariCP & Spring
figure out from the spring.datasource.url
on which database driver to use).
I did not see the need to create a DataSource
bean and that's because Spring Boot is capable of doing everything for me just by looking into application.properties
and that's neat.
The article in HikariCP's github wiki shows how to setup Spring Boot with JPA but, lacks explanation and details.
The above two file is also availble as a public gist https://gist.github.com/rhamedy/b3cb936061cc03acdfe21358b86a5bc6