Hikaricp connection pool of spring series


In the last two articles, we talked about how to configure single data sources and multiple data sources in spring. When configuring data sources, there are many choices for connection pools. In springboot 1.0, we use Tomcat datasource. In springboot 2.0, we use hikaricp as the default connection pool. This article talks about hikaricp.

Why does springboot 2.0 choose hikaricp as the default connection pool?

Let’s take a look at a comparison chart on the official website.
A connection cycle is defined as a single datasource getConnection()/ Connection. close()。 A statement cycle is defined as a single connection prepareStatement(), Statement. execute(), Statement. close()
As can be seen from the above figure, hikaricp has obvious advantages over common connection pools.

Why is hikaricp so fast? According to the official website, the following points are summarized

  • Bytecode simplification: bytecode level optimization (many methods are implemented through javaassist) until the compiled bytecode is minimized. In this way, the CPU cache can load more program code;
  • Optimize the proxy and Interceptor: reduce the code. For example, the statement proxy of hikaricp has only 100 lines of code, which is only one tenth of that of bonecp;
  • Custom array type: (faststatementlist) instead of ArrayList: avoid range check every time get() is called, and avoid scanning from beginning to end when remove() is called;
  • User defined collection type (concurrentbag: improve the efficiency of concurrent reading and writing;
  • Optimization of proxy classes (⽐ for example, ⽤ invokestatic replaces invokevirtual)
  • Other optimizations for bonecp defects, such as the research on method calls that take more than one CPU time slice (but no specific optimization is given).

Since hikaricp is so fast, let’s take a look at how to use hikaricp in spring?

In spring boot 2 In X
• default to ⽤ hikaricp
• configure spring datasource. hikari.* to configure

On spring boot 1 In X
• the default setting is “Tomcat connection pool”, and the Tomcat JDBC dependency needs to be removed
• in application Add spring datasource. type=com. zaxxer. hikari. HikariDataSource

Let’s take a look at how springboot2.0 configures hikaridatasource

Here is org springframework. boot. autoconfigure. jdbc. Related codes in datasourceconfiguration

*The following three annotations mean that hikaridatasource Class, and there is no bean configured with datasource in the spring context
*And spring datasource. The value of type is com zaxxer. hikari. When hikaridatasource is used, springboot automatically helps us select the default connection pool as hikaridatasource
@ConditionalOnProperty(name = {"spring.datasource.type"},havingValue = "com.zaxxer.hikari.HikariDataSource",matchIfMissing = true)
static class Hikari {
        Hikari() {

@ConfigurationProperties(prefix = "spring.datasource.hikari")
HikariDataSource dataSource(DataSourceProperties properties) {
            HikariDataSource dataSource = (HikariDataSource)DataSourceConfiguration.createDataSource(properties, HikariDataSource.class);
            if (StringUtils.hasText(properties.getName())) {
            return dataSource;

Finally, what are the parameters of hikaricp configuration?

#These four configurations are used for different data sources

#The following configuration items are unique to Hikari
#The maximum time (in milliseconds) to wait for a connection to be allocated by the connection pool. Sqlexception occurs for connections that are not available after this time. Default: 30 seconds
#Minimum connections
#Maximum connections
#Auto submit
#The maximum duration (in milliseconds) of a connection idle state. The timeout is released (retired). The default is 10 minutes
#Connection pool name
#The lifetime of a connection (in milliseconds) will be released if it times out and is not used. The default value is 30 minutes and 1800000ms. It is recommended to set it to be 60 seconds less than the database timeout
spring.datasource.hikari.connection-test-query=SELECT 1

#The following are the configuration parameters for MySQL driver
#The number of statements cached in each connection. The default value is a conservative value of 25. It is recommended to set it between 250-500
spring.datasource.hikari.prepStmtCacheSize = 300
#The maximum length of cached prepared SQL statements. The default value is 256. However, this length is often insufficient
spring.datasource.hikari.prepStmtCacheSqlLimit = 2048
#Cache switch. If it is set to false, neither of the above two parameters will take effect
spring.datasource.hikari.cachePrepStmts = true
#Newer versions of MySQL support server-side prepared statements, which can provide substantial performance improvements
spring.datasource.hikari.useServerPrepStmts = true

Hikaricp official address:https://github.com/brettwooldridge/HikariCP/wiki