如何在我的 application.properties 文件中的 Spring Boot 应用程序中配置 HikariCP? [英] How do I configure HikariCP in my Spring Boot app in my application.properties files?

查看:49
本文介绍了如何在我的 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 文件中是否也可能?

I'm trying to set up HikariCP in my Spring Boot (1.2.0.M1) app so I can test using it in place of Tomcat DBCP. I'd like to configure the connection pool in my application.properties file like I was doing with Tomcat, but I can't figure out how I should be doing it. All examples I've found show either JavaConfig style, or using a separate HikariCP properties file. Can someone help me figure out the property names to configure it in application.properties? I'd like to also switch from using the driverClassName approach to the DataSourceClassName approach since it looks cleaner and is recommended. Is this also possible in my application.properties file(s)?

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

Here's what I had for Tomcat DBCP (just some basic config, not fully flushed out)

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 来建立连接:

And I'm currently using driverClassName and jdbc url to set up the connection:

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

推荐答案

@Configuration
@ConfigurationProperties(prefix = "params.datasource")
public class JpaConfig extends HikariConfig {

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

}

application.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

application.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 :)

The default connection pool has changed from Tomcat to Hikari :)

这篇关于如何在我的 application.properties 文件中的 Spring Boot 应用程序中配置 HikariCP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆