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

查看:1154
本文介绍了如何在我的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添加到dependecies

  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天全站免登陆