Configuring a Hikari Connection Pool with Spring Boot – 用Spring Boot配置Hikari连接池

最后修改: 2018年 8月 21日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Hikari is a JDBC DataSource implementation that provides a connection pooling mechanism.

Hikari是一个JDBC DataSource实现,提供了一个连接池机制。

Compared to other implementations, it promises to be lightweight and better performing. For an introduction to Hikari, see this article.

与其他实现相比,它有望成为轻量级和性能更好的。关于Hikari的介绍,请参阅这篇文章

This quick tutorial shows how we can configure a Spring Boot 2 or Spring Boot 1 application to use the Hikari DataSource.

这个快速教程展示了我们如何配置Spring Boot 2或Spring Boot 1应用程序以使用Hikari DataSource

2. Configuring Hikari With Spring Boot 2.x

2.用Spring Boot 2.x配置Hikari

In Spring Boot 2, Hikari is the default DataSource implementation.

在Spring Boot 2中,Hikari是默认的数据源实现。

However, to use the latest version, we need to add the Hikari dependency in the pom.xml explicitly:

然而,为了使用最新的版本,我们需要在pom.xml中明确添加Hikari的依赖性。

<dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
    <version>4.0.3</version>
</dependency>

This is what’s changed from Spring Boot 1.x:

这是与Spring Boot 1.x相比的变化。

  • The dependency to Hikari is now automatically included in spring-boot-starter-data-jpa and spring-boot-starter-jdbc.
  • The discovery algorithm that automatically determines a DataSource implementation now prefers Hikari over TomcatJDBC (see the reference manual).

So, we have nothing to do if we want to use Hikari in an application based on Spring Boot 2.x, unless we want to use its latest version.

因此,如果我们想在基于Spring Boot 2.x的应用程序中使用Hikari,除非我们想使用其最新版本,否则我们无能为力。

3. Tuning Hikari Configuration Parameters

3.调整Hikari配置参数

One of Hikari’s advantages over other DataSource implementations is the fact that it offers a lot of configuration parameters.

与其他DataSource实现相比,Hikari的优势之一是它提供了大量的配置参数。

We can specify the values for these parameters by using the prefix spring.datasource.hikari and appending the name of the Hikari parameter:

我们可以通过使用前缀spring.datasource.hikari并附加Hikari参数的名称来指定这些参数的值。

spring.datasource.hikari.connectionTimeout=30000 
spring.datasource.hikari.idleTimeout=600000 
spring.datasource.hikari.maxLifetime=1800000 
...

A list of all Hikari parameters with a good explanation is available on the Hikari GitHub site as well as in the Spring docs.

Hikari GitHub网站以及Spring文档中,有一个所有Hikari参数的列表,并有很好的解释。

4. Configuring Hikari With Spring Boot 1.x

4.用Spring Boot 1.x配置Hikari

Spring Boot 1.x uses the Tomcat JDBC Connection Pool by default.

Spring Boot 1.x 默认使用Tomcat JDBC 连接池

As soon as we include spring-boot-starter-data-jpa into our pom.xml, we’ll transitively include a dependency to the Tomcat JDBC implementation. During runtime, Spring Boot will then create a Tomcat DataSource for us to use.

一旦我们将spring-boot-starter-data-jpa纳入我们的pom.xml,我们就会顺便纳入对Tomcat JDBC实现的依赖。在运行时,Spring Boot将创建一个Tomcat的DataSource供我们使用。

To configure Spring Boot to use the Hikari Connection Pool instead, we have two options.

要将Spring Boot配置为使用Hikari连接池,我们有两个选择。

4.1. Maven Dependency

4.1.Maven的依赖性

First, we need to include the dependency on Hikari in our pom.xml:

首先,我们需要在我们的pom.xml中包括对Hikari的依赖性。

<dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
    <version>4.0.3</version>
</dependency>

The most current version can be found on Maven Central.

最新的版本可以在Maven Central上找到。

4.2. Explicit Configuration

4.2.明确配置

The safest way to tell Spring Boot to use Hikari is configuring the DataSource implementation explicitly.

告诉Spring Boot使用Hikari的最安全方式是明确配置DataSource实现。

To do this, we simply set the property spring.datasource.type to the fully qualified name of the DataSource implementation we want to use:

要做到这一点,我们只需将属性spring.datasource.type设为我们想要使用的DataSource实现的完全合格名称

@RunWith(SpringRunner.class)
@SpringBootTest(
    properties = "spring.datasource.type=com.zaxxer.hikari.HikariDataSource"
)
public class HikariIntegrationTest {

    @Autowired
    private DataSource dataSource;

    @Test
    public void hikariConnectionPoolIsConfigured() {
        assertEquals("com.zaxxer.hikari.HikariDataSource", dataSource.getClass().getName());
    }
}

4.3. Removing the Tomcat JDBC Dependency

4.3.删除Tomcat JDBC的依赖关系

The second option is to let Spring Boot find the Hikari DataSource implementation itself.

第二个选择是让Spring Boot自己找到Hikari DataSource的实现。

If Spring Boot cannot find the Tomcat DataSource in the classpath, it will automatically look for the Hikari DataSource next. The discovery algorithm is described in the reference manual.

如果Spring Boot无法在classpath中找到Tomcat DataSource,它将自动寻找Hikari DataSource发现算法在参考手册中描述。

To remove the Tomcat Connection Pool from the classpath, we can exclude it in our pom.xml:

为了从classpath中删除Tomcat连接池,我们可以在pom.xml中排除它。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jdbc</artifactId>
         </exclusion>
     </exclusions>
</dependency>

Now the test from the previous section will also work without setting the spring.datasource.type property.

现在,上一节的测试也将在不设置spring.datasource.type属性的情况下工作。

5. Conclusion

5.结论

In this article, we configured the Hikari DataSource implementation in a Spring Boot 2.x application. And we learned how to leverage Spring Boot’s autoconfiguration.

在本文中,我们在Spring Boot 2.x应用程序中配置了Hikari DataSource实现。我们还学习了如何利用Spring Boot的自动配置功能。

We also had a look at the changes required to configure Hikari when using Spring Boot 1.x.

我们还看了一下使用Spring Boot 1.x时配置Hikari所需的变化。

The code for the Spring Boot 1.x example is available here, and the code for the Spring Boot 2.x example is available here.

Spring Boot 1.x示例的代码可在这里获得,Spring Boot 2.x示例的代码可在这里获得。