Hikari Connection Pooling in Spring Boot

Hikari Connection Pooling in Spring Boot | Pool is a group of Objects that are of the same type. Example: Admin Pool = Admin Objects, Spring Constant Pool = String Objects, Product Pool = Product Objects, ..etc. Connection Pool = Database Connection.

By default, Spring Boot uses HikariConfig which comes with default auto-configuration. When we add Data JPA (or) JDBC API then by default below dependency is added and comes with auto-configuration.

<dependency>
  <groupId>com.zaxxer</groupId>
  <artifactId>HikariCP</artifactId>
  <version>3.4.5</version>
  <scope>compile</scope>
</dependency>

Spring Boot 1.x was supporting Tomcat Connection Pooling, now it is moved to HikariCP (since Spring Boot 2.x). Tomcat Connection Pooling is not recommended because it is server based and if we move from one server to another then it might not work.

  • HikariConfig(C):- It comes with all default values for Configuration.
  • HikariDataSource(C):- It is an implementation class for DataSource(I). Here DataSource means Database Connection.
# connection pooling details
spring.datasource.hikari.pool-name=my-hikari-cp

# default stated with 10 connection
spring.datasource.hikari.minimum-idle=15

spring.datasource.hikari.idle-timeout=6000000

# should be more than 250 milli seconds; 180000ms = 3 minute
spring.datasource.hikari.connection-timeout=180000

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

HikariCP Configuration Properties Details

  1. Provide a name to the Connection Pool Object
    spring.datasource.hikari.pool-name=my-hikari-cp
  2. By Default CP started with 10 Connections and later it took our configuration. ie default Pool size. (DEFAULT_POOL_SIZE = 10)
  3. Max number of Connections created at Pool (int number) It can not be <1.
    spring.datasource.hikari.maximum-pool-size=20
  4. Max number of non-used/no work connections to be kept in Pool.
    spring.datasource.hikari.minimum-idle=15
  5. Time in MillSec, for a Connection Timeout of A SQL query execution
    spring.datasource.hikari.connection-timeout=180000
  6. For Idle connection detection time to be considered (in Mill Sec)
    spring.datasource.hikari.idle-timeout=600000

Default values:-

  • IDLE_TIMEOUT = 10 Mins
  • MAX LIFETIME of a Connection = 30 min
  • CONNECTION TIMEOUT = 30 sec
  • Connection validation time out VALIDATION_TIMEOUT = 5 sec

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *