Java - Connection Pooling Frameworks

Java - Connection Pooling Frameworks

Connection Pooling is a cache of database connections maintained so that the connections can be reused when future requests to the database are required. Connection pools are used to enhance the performance of executing commands on a database

Connection Pooling Frameworks

  • HikariCP
  • Tomcat Pooling
  • Commons DBCP2

Spring Boot's Autoconfiguration

For a pooling DataSource to be created, Spring Boot verifies that a valid Driver class is available. If we set spring.datasource.driver-class-name property then that mentioned driver class has to be loadable.

Spring boot tries to find and configure connection pooling first HikariCP, second Tomcat pooling, and then finally Commons DBCP2. HikariCP comes inbuilt with spring-boot-starter-jdbc or spring-boot-starter-data-jpa starters

Customize Connection Pooling Settings

It is also possible to fine-tune implementation-specific settings by using their respective prefix:

  • spring.datasource.hikari.*
  • spring.datasource.tomcat.*
  • spring.datasource.dbcp2.*

For example, we can use below properties to customize a DBCP2 connection pool.

application.properties
spring.datasource.dbcp2.initial-size = 50
spring.datasource.dbcp2.max-idle = 50
spring.datasource.dbcp2.default-query-timeout = 10000
spring.datasource.dbcp2.default-auto-commit = true