如何在我的 application.properties 文件中的 Spring Boot 应用程序中配置 HikariCP?

新手上路,请多包涵

我正在尝试在我的 Spring Boot (1.2.0.M1) 应用程序中设置 HikariCP,以便我可以测试使用它来代替 Tomcat DBCP。我想在我的 application.properties 文件中配置连接池,就像我在 Tomcat 中所做的那样,但我不知道我应该怎么做。我发现的所有示例都显示了 JavaConfig 样式,或者使用了单独的 HikariCP 属性文件。有人可以帮我找出属性名称以在 application.properties 中配置它吗?我还想从使用 driverClassName 方法切换到 DataSourceClassName 方法,因为它看起来更干净并且被推荐。这在我的 application.properties 文件中是否也可行?

这是我对 Tomcat DBCP 的配置(只是一些基本配置,没有完全清除)

 spring.datasource.validation-query=SELECT 1
spring.datasource.max-active=10
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=5
spring.datasource.test-on-borrow=true
spring.datasource.test-on-return=true

我目前正在使用 driverClassName 和 jdbc url 来设置连接:

 spring.datasource.url=jdbc:mysql://localhost:3306/myDb
spring.datasource.driverClassName=com.mysql.jdbc.Driver

原文由 Kevin M 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 667
2 个回答
@Configuration
@ConfigurationProperties(prefix = "params.datasource")
public class JpaConfig extends HikariConfig {

    @Bean
    public DataSource dataSource() throws SQLException {
        return new HikariDataSource(this);
    }

}

应用.yml

 params:
  datasource:
    driverClassName: com.mysql.jdbc.Driver
    jdbcUrl: jdbc:mysql://localhost:3306/myDb
    username: login
    password: password
    maximumPoolSize: 5


更新!从版本 Spring Boot 1.3.0 开始

  1. 只需将 HikariCP 添加到依赖项
  2. 配置application.yml

应用.yml

 spring:
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    url: jdbc:h2:mem:TEST
    driver-class-name: org.h2.Driver
    username: username
    password: password
    hikari:
      idle-timeout: 10000


更新!从版本 Spring Boot 2.0.0 开始

默认连接池已从 Tomcat 更改为 Hikari :)

原文由 Sergey Bulavkin 发布,翻译遵循 CC BY-SA 4.0 许可协议

我遇到了 HikariCP 我对基准测试感到惊讶,我想尝试它而不是我的默认选择 C3P0 令我惊讶的是我努力获得 configurations 正确可能是因为配置因您使用的技术堆栈组合而异。

I have setup Spring Boot project with JPA, Web, Security starters (Using Spring Initializer ) to use PostgreSQL as a database with HikariCP as connection pooling.

我使用 Gradle 作为构建工具,我想分享一下对我有用的假设:

  1. Spring Boot Starter JPA(Web 和安全 - 可选)
  2. Gradle 也构建
  3. PostgreSQL 运行并设置数据库(即模式、用户、数据库)

你需要以下 build.gradle 如果你使用 Gradle 或等效 pom.xml 如果你使用maven

 buildscript {
    ext {
        springBootVersion = '1.5.8.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'

group = 'com'
version = '1.0'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-aop')

    // Exclude the tomcat-jdbc since it's used as default for connection pooling
    // This can also be achieved by setting the spring.datasource.type to HikariCP
    // datasource see application.properties below
    compile('org.springframework.boot:spring-boot-starter-data-jpa') {
        exclude group: 'org.apache.tomcat', module: 'tomcat-jdbc'
    }
    compile('org.springframework.boot:spring-boot-starter-security')
    compile('org.springframework.boot:spring-boot-starter-web')
    runtime('org.postgresql:postgresql')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile('org.springframework.security:spring-security-test')

    // Download HikariCP but, exclude hibernate-core to avoid version conflicts
    compile('com.zaxxer:HikariCP:2.5.1') {
        exclude group: 'org.hibernate', module: 'hibernate-core'
    }

    // Need this in order to get the HikariCPConnectionProvider
    compile('org.hibernate:hibernate-hikaricp:5.2.11.Final') {
        exclude group: 'com.zaxxer', module: 'HikariCP'
        exclude group: 'org.hibernate', module: 'hibernate-core'
    }
}

上面有一堆排除 build.gradle 那是因为

  1. 首先排除,指示gradle在下载 spring-boot-starter-data-jpa 依赖项时排除 jdbc-tomcat 连接池。这可以通过设置 spring.datasource.type=com.zaxxer.hikari.HikariDataSource 来实现,但是,如果我不需要它,我不想要额外的依赖
  2. Second exclude, instructs gradle to exclude hibernate-core when downloading com.zaxxer dependency and that’s because hibernate-core is already downloaded by Spring Boot and we don’t想要结束不同的版本。
  3. Third exclude, instructs gradle to exclude hibernate-core when downloading hibernate-hikaricp module which is needed in order to make HikariCP use org.hibernate.hikaricp.internal.HikariCPConnectionProvider as connection provider instead of deprecated com.zaxxer.hikari.hibernate.HikariConnectionProvider

一旦我弄清楚 build.gradle 以及保留什么和不保留什么,我就准备复制/粘贴一个 datasource 配置到我的 application.properties 使用鲜艳的颜色,但不是真的,我偶然发现了以下问题

  • Spring boot 无法找到数据库详细信息(即 url、驱动程序),因此无法设置 jpa 和 hibernate(因为我没有正确命名属性键值)
  • HikariCP 回落到 com.zaxxer.hikari.hibernate.HikariConnectionProvider
  • 在指示 Spring 在自动配置休眠/jpa 时使用新的连接提供程序之后,HikariCP 失败了,因为它正在寻找一些 key/valueapplication.properties 并抱怨 dataSource, dataSourceClassName, jdbcUrl 。我不得不调试到 HikariConfig, HikariConfigurationUtil, HikariCPConnectionProvider 并发现 HikariCP 找不到来自 application.properties 的属性,因为它的命名不同。

无论如何,这是我不得不依靠反复试验并确保 HikariCP 能够选择属性(即数据库详细信息的数据源,以及池属性)以及 Sping Boot 行为的地方正如预期的那样,我最终得到了以下 application.properties 文件。

 server.contextPath=/
debug=true

# Spring data source needed for Spring boot to behave
# Pre Spring Boot v2.0.0.M6 without below Spring Boot defaults to tomcat-jdbc connection pool included
# in spring-boot-starter-jdbc and as compiled dependency under spring-boot-starter-data-jpa
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.url=jdbc:postgresql://localhost:5432/somedb
spring.datasource.username=dbuser
spring.datasource.password=dbpassword

# Hikari will use the above plus the following to setup connection pooling
spring.datasource.hikari.minimumIdle=5
spring.datasource.hikari.maximumPoolSize=20
spring.datasource.hikari.idleTimeout=30000
spring.datasource.hikari.poolName=SpringBootJPAHikariCP
spring.datasource.hikari.maxLifetime=2000000
spring.datasource.hikari.connectionTimeout=30000

# Without below HikariCP uses deprecated com.zaxxer.hikari.hibernate.HikariConnectionProvider
# Surprisingly enough below ConnectionProvider is in hibernate-hikaricp dependency and not hibernate-core
# So you need to pull that dependency but, make sure to exclude it's transitive dependencies or you will end up
# with different versions of hibernate-core
spring.jpa.hibernate.connection.provider_class=org.hibernate.hikaricp.internal.HikariCPConnectionProvider

# JPA specific configs
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.use_sql=true
spring.jpa.properties.hibernate.id.new_generator_mappings=false
spring.jpa.properties.hibernate.default_schema=dbschema
spring.jpa.properties.hibernate.search.autoregister_listeners=false
spring.jpa.properties.hibernate.bytecode.use_reflection_optimizer=false

# Enable logging to verify that HikariCP is used, the second entry is specific to HikariCP
logging.level.org.hibernate.SQL=DEBUG
logging.level.com.zaxxer.hikari.HikariConfig=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

如上所示,配置根据以下命名模式分为几类

  • spring.datasource.x (Spring 自动配置会选择这些,HikariCP 也会)
  • spring.datasource.hikari.x (HikariCP 选择这些来设置池,记下 camelCase 字段名称)
  • spring.jpa.hibernate.connection.provider_class (指示 Spring 使用新的 HibernateConnectionProvider)
  • spring.jpa.properties.hibernate.x (Spring用来自动配置JPA,记下带下划线的字段名)

很难找到说明如何使用上述属性文件以及应如何命名属性的教程或帖子或某些资源。那么,你有它。

将上面的 application.propertiesbuild.gradle (或至少类似)扔到 Spring Boot JPA 项目版本(1.5.8)中应该像魅力一样工作并连接到您的预配置数据库(即在我的例子中,它是 PostgreSQL — 从 spring.datasource.url HikariCP & Spring 找出要使用的数据库驱动程序)。

我没有看到需要创建一个 DataSource bean,那是因为 Spring Boot 只需查看 application.properties 以为我做所有事情,这很好。

HikariCP 的 github wiki 中的 文章 展示了如何使用 JPA 设置 Spring Boot,但缺乏解释和细节。

以上两个文件也可作为公共要点 https://gist.github.com/rhamedy/b3cb936061cc03acdfe21358b86a5bc6

原文由 Raf 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题