Configuring Tomcat Connection pool and Hikari Connection pool in Spring Boot Application
One key component of spring boot starter dependencies is spring-boot-starter-data-JPA. This allows us to use JPA and work with production databases by using some popular JDBC connection pooling implementations, such as HikariCP or Tomcat JDBC Connection Pool.
Tomcat Connection Pooling
Spring Boot will look for HikariCP on the classpath and use it by default when present.
To configure a Tomcat JDBC connection pool instead of the default HikariCP, we'll exclude HikariCP from the spring-boot-starter-data-JPA dependency and add the tomcat-JDBC Maven dependency.
Maven (pom.xml)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <exclusions> <exclusion> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> </exclusion> </exclusions></dependency><dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jdbc</artifactId></dependency>
Gradle (build.gradle)
implementation('org.springframework.boot:spring-boot-starter-data-jpa') { exclude group: 'com.zaxxer', module: 'HikariCP'}
implementation('org.apache.tomcat:tomcat-jdbc')
For optimizing its performance we can add additional properties
# initialSize is the number of initial connections # created when the pool is startedspring.datasource.tomcat.initial-size=10
# maxWait is the maximum number of milliseconds # that the pool waits to return a connection # before throwing an exception.spring.datasource.tomcat.max-wait=15000
# maxActive is the maximum number of active # connections that the pool can allocate at # the same time.spring.datasource.tomcat.max-active=40
#maxIdle is the maximum number of connections # that should remain in the pool at any timespring.datasource.tomcat.max-idle=15
# minIdle is the minimum number of connections # that should remain in the pool at any time.spring.datasource.tomcat.min-idle=8
# The defaultAutoCommit property is used to # configure the default autocommit state of # connections created by this pool.spring.datasource.tomcat.default-auto-commit=true
Please notice that we've configured a few additional connection pooling properties, such as the pool's initial size, and the maximum and the minimum number of idle connections.
How to know which connection pool is used?
@SpringBootApplicationpublic class KnowledgefactorydemoApplication implements CommandLineRunner { @Autowired private DataSource dataSource;
public static void main(String[] args) { SpringApplication. run(KnowledgefactorydemoApplication.class, args); }
@Override public void run(String...args) throws Exception { System.out.println("DATASOURCE = " + dataSource); }
Start Spring boot application
On the console system will print,
"DATASOURCE = class org.apache.tomcat.jdbc.pool.DataSource"
"DATASOURCE = class org.apache.tomcat.jdbc.pool.DataSource"
Hikari Connection Pooling
Spring Boot will look for HikariCP on the classpath and use it by default when presentMaven (pom.xml)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId></dependency>
Gradle (build.gradle)
implementation('org.springframework.boot:spring-boot-starter-data-jpa')
One of Hikari's advantages over other DataSource implementations is the fact that it offers a lot of configuration parameters.
We can specify the values for these parameters by using the prefix spring.datasource.hikari and appending the name of the Hikari parameter:
For optimizing its performance we can add additional properties
# maximum number of milliseconds that a client # will wait for a connectionspring.datasource.hikari.connection-timeout = 20000
# minimum number of idle connections maintained # by HikariCP in a connection poolspring.datasource.hikari.minimum-idle= 10
# maximum pool sizespring.datasource.hikari.maximum-pool-size= 10
# maximum idle time for connectionspring.datasource.hikari.idle-timeout=10000
# maximum lifetime in milliseconds of # a connection in the pool after it is closed.spring.datasource.hikari.max-lifetime= 1000
#default auto-commit behavior.spring.datasource.hikari.auto-commit =true
How to know which connection pool is used?
@SpringBootApplicationpublic class KnowledgefactorydemoApplication implements CommandLineRunner {
@Autowired private DataSource dataSource;
public static void main(String[] args) { SpringApplication. run(KnowledgefactorydemoApplication.class, args); }
@Override public void run(String...args) throws Exception { System.out.println("DATASOURCE = " + dataSource); }
Start Spring boot application
At the console, the system will print,
"DATASOURCE = class com.zaxxer.hikari.HikariDataSource"