Java 如何在我的 application.properties 文件中的 Spring Boot 应用程序中配置 HikariCP?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26490967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I configure HikariCP in my Spring Boot app in my application.properties files?
提问by Kevin M
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)?
我正在尝试在我的 Spring Boot (1.2.0.M1) 应用程序中设置 HikariCP,以便我可以使用它代替 Tomcat DBCP 进行测试。我想在我的 application.properties 文件中配置连接池,就像我在 Tomcat 中所做的那样,但我不知道我应该怎么做。我发现的所有示例都显示 JavaConfig 样式,或使用单独的 HikariCP 属性文件。有人可以帮我找出属性名称以在 application.properties 中配置它吗?我还想从使用 driverClassName 方法切换到 DataSourceClassName 方法,因为它看起来更干净并且值得推荐。这在我的 application.properties 文件中是否也可能?
Here's what I had for Tomcat DBCP (just some basic config, not fully flushed out)
这是我对 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
And I'm currently using driverClassName and jdbc url to set up the connection:
我目前正在使用 driverClassName 和 jdbc url 来设置连接:
spring.datasource.url=jdbc:mysql://localhost:3306/myDb
spring.datasource.driverClassName=com.mysql.jdbc.Driver
采纳答案by Sergey Bulavkin
@Configuration
@ConfigurationProperties(prefix = "params.datasource")
public class JpaConfig extends HikariConfig {
@Bean
public DataSource dataSource() throws SQLException {
return new HikariDataSource(this);
}
}
application.yml
应用程序.yml
params:
datasource:
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/myDb
username: login
password: password
maximumPoolSize: 5
UPDATED! Since version Spring Boot 1.3.0:
更新!从 Spring Boot 1.3.0 版本开始:
- Just add HikariCP to dependencies
- Configure application.yml
- 只需将 HikariCP 添加到依赖项
- 配置应用程序.yml
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
UPDATED! Since version Spring Boot 2.0.0:
更新!从 Spring Boot 2.0.0 版本开始:
The default connection pool has changed from Tomcat to Hikari :)
默认连接池已从 Tomcat 更改为 Hikari :)
回答by Kevin M
So it turns out that almost all the default settings for HikariCP work for me except the number of DB connections. I set that property in my application.properties:
所以事实证明,除了数据库连接数之外,几乎所有 HikariCP 的默认设置都对我有用。我在 application.properties 中设置了该属性:
spring.datasource.maximumPoolSize=20
And Andy Wilkinson is correct as far as I can tell in that you can't use the dataSourceClassName configuration approach for HikariCP with Spring Boot.
Andy Wilkinson 是正确的,据我所知,您不能使用带有 Spring Boot 的 HikariCP 的 dataSourceClassName 配置方法。
回答by Jesús Zazueta
This works for my boot application in case it helps. This class tells you what properties the config object is looking for:
这适用于我的启动应用程序,以防万一。这个类告诉你配置对象正在寻找哪些属性:
I think multiple datasources could be supporting by adding datasource_whatever
to the property keys in the source config file. Cheers!
我认为可以通过添加datasource_whatever
到源配置文件中的属性键来支持多个数据源。干杯!
@Configuration
class DataSourceConfig {
@Value('${spring.datasource.username}')
private String user;
@Value('${spring.datasource.password}')
private String password;
@Value('${spring.datasource.url}')
private String dataSourceUrl;
@Value('${spring.datasource.dataSourceClassName}')
private String dataSourceClassName;
@Value('${spring.datasource.connectionTimeout}')
private int connectionTimeout;
@Value('${spring.datasource.maxLifetime}')
private int maxLifetime;
@Bean
public DataSource primaryDataSource() {
Properties dsProps = [url: dataSourceUrl, user: user, password: password]
Properties configProps = [
connectionTestQuery: 'select 1 from dual',
connectionTimeout: connectionTimeout,
dataSourceClassName: dataSourceClassName,
dataSourceProperties: dsProps,
maxLifetime: maxLifetime
]
// A default max pool size of 10 seems reasonable for now, so no need to configure for now.
HikariConfig hc = new HikariConfig(configProps)
HikariDataSource ds = new HikariDataSource(hc)
ds
}
}
回答by Shahid Yousuf
you can't use dataSourceClassName approach in application.properties configurations as said by @Andy Wilkinson. if you want to have dataSourceClassName anyway you can use Java Config as:
正如@Andy Wilkinson 所说,您不能在 application.properties 配置中使用 dataSourceClassName 方法。如果你想要 dataSourceClassName 无论如何你可以使用 Java Config 作为:
@Configuration
@ComponentScan
class DataSourceConfig {
@Value("${spring.datasource.username}")
private String user;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.datasource.url}")
private String dataSourceUrl;
@Value("${spring.datasource.dataSourceClassName}")
private String dataSourceClassName;
@Value("${spring.datasource.poolName}")
private String poolName;
@Value("${spring.datasource.connectionTimeout}")
private int connectionTimeout;
@Value("${spring.datasource.maxLifetime}")
private int maxLifetime;
@Value("${spring.datasource.maximumPoolSize}")
private int maximumPoolSize;
@Value("${spring.datasource.minimumIdle}")
private int minimumIdle;
@Value("${spring.datasource.idleTimeout}")
private int idleTimeout;
@Bean
public DataSource primaryDataSource() {
Properties dsProps = new Properties();
dsProps.put("url", dataSourceUrl);
dsProps.put("user", user);
dsProps.put("password", password);
dsProps.put("prepStmtCacheSize",250);
dsProps.put("prepStmtCacheSqlLimit",2048);
dsProps.put("cachePrepStmts",Boolean.TRUE);
dsProps.put("useServerPrepStmts",Boolean.TRUE);
Properties configProps = new Properties();
configProps.put("dataSourceClassName", dataSourceClassName);
configProps.put("poolName",poolName);
configProps.put("maximumPoolSize",maximumPoolSize);
configProps.put("minimumIdle",minimumIdle);
configProps.put("minimumIdle",minimumIdle);
configProps.put("connectionTimeout", connectionTimeout);
configProps.put("idleTimeout", idleTimeout);
configProps.put("dataSourceProperties", dsProps);
HikariConfig hc = new HikariConfig(configProps);
HikariDataSource ds = new HikariDataSource(hc);
return ds;
}
}
reason you cannot use dataSourceClassName because it will throw and exception
不能使用 dataSourceClassName 的原因,因为它会抛出异常
Caused by: java.lang.IllegalStateException: both driverClassName and dataSourceClassName are specified, one or the other should be used.
which mean spring boot infers from spring.datasource.url property the Driver and at the same time setting the dataSourceClassName creates this exception. To make it right your application.properties should look something like this for HikariCP datasource:
这意味着 spring boot 从 spring.datasource.url 属性推断驱动程序,同时设置 dataSourceClassName 会创建此异常。为了使它正确,您的 application.properties 对于 HikariCP 数据源应该如下所示:
# hikariCP
spring.jpa.databasePlatform=org.hibernate.dialect.MySQLDialect
spring.datasource.url=jdbc:mysql://localhost:3306/exampledb
spring.datasource.username=root
spring.datasource.password=
spring.datasource.poolName=SpringBootHikariCP
spring.datasource.maximumPoolSize=5
spring.datasource.minimumIdle=3
spring.datasource.maxLifetime=2000000
spring.datasource.connectionTimeout=30000
spring.datasource.idleTimeout=30000
spring.datasource.pool-prepared-statements=true
spring.datasource.max-open-prepared-statements=250
Note: Please check if there is any tomcat-jdbc.jar or commons-dbcp.jar in your classpath added most of the times by transitive dependency. If these are present in classpath Spring Boot will configure the Datasource using default connection pool which is tomcat. HikariCP will only be used to create the Datasource if there is no other provider in classpath. there is a fallback sequence from tomcat -> to HikariCP -> to Commons DBCP.
注意:请检查您的类路径中是否有任何 tomcat-jdbc.jar 或 commons-dbcp.jar 大部分时间通过传递依赖添加。如果这些存在于类路径中,Spring Boot 将使用默认连接池(tomcat)配置数据源。如果类路径中没有其他提供程序,HikariCP 将仅用于创建数据源。有一个从 tomcat -> 到 HikariCP -> 到 Commons DBCP 的后备序列。
回答by Sanghyun Lee
You don't need redundant code for putting property values to variables. You can set properties with a properties file directly.
您不需要多余的代码来将属性值放入变量。您可以直接使用属性文件设置属性。
Put hikari.properties
file in the classpath.
将hikari.properties
文件放在类路径中。
driverClassName=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/myDb
connectionTestQuery=SELECT 1
maximumPoolSize=20
username=...
password=...
And make a datasource bean like this.
并像这样制作一个数据源bean。
@Bean(destroyMethod = "close")
public DataSource dataSource() throws SQLException {
HikariConfig config = new HikariConfig("/hikari.properties");
HikariDataSource dataSource = new HikariDataSource(config);
return dataSource;
}
回答by ydemartino
You can use the dataSourceClassName approach, here is an example with MySQL. (Tested with spring boot 1.3 and 1.4)
您可以使用 dataSourceClassName 方法,这里是 MySQL 的示例。(用弹簧靴 1.3 和 1.4 测试)
First you need to exclude tomcat-jdbc from the classpath as it will be picked in favor of hikaricp.
首先,您需要从类路径中排除 tomcat-jdbc,因为它将被选择以支持 hikaricp。
pom.xml
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>
</dependency>
application.properties
应用程序属性
spring.datasource.dataSourceClassName=com.mysql.jdbc.jdbc2.optional.MysqlDataSource
spring.datasource.dataSourceProperties.serverName=localhost
spring.datasource.dataSourceProperties.portNumber=3311
spring.datasource.dataSourceProperties.databaseName=mydb
spring.datasource.username=root
spring.datasource.password=root
Then just add
然后只需添加
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
I created a test project here: https://github.com/ydemartino/spring-boot-hikaricp
我在这里创建了一个测试项目:https: //github.com/ydemartino/spring-boot-hikaricp
回答by user3544765
You could simply make use of application.yml/application.properties only. There is no need to explicitly create any DataSource
Bean
您可以仅使用 application.yml/application.properties。无需显式创建任何DataSource
Bean
You need to exclude tomcat-jdbc as mentioned by ydemartino
您需要排除 ydemartino 提到的 tomcat-jdbc
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>
</dependency>
As you won't create DataSource
bean, you have to explicitly specify using Hikari through spring.datasource.type
with value com.zaxxer.hikari.HikariDataSource
in application.yml / application.properties
由于您不会创建DataSource
bean,您必须在 application.yml / application.properties 中明确指定 using Hikari through spring.datasource.type
with valuecom.zaxxer.hikari.HikariDataSource
spring:
datasource:
hikari:
connection-test-query: SELECT 1 FROM DUAL
minimum-idle: 1
maximum-pool-size: 5
pool-name: yourPoolName
auto-commit: false
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/myDb
username: login
password: password
type: com.zaxxer.hikari.HikariDataSource
In your application.yml / application.properties, you could configure Hikari specific parameters such as pool size etc in spring.datasource.hikari.*
在您的 application.yml / application.properties 中,您可以配置 Hikari 特定参数,例如池大小等 spring.datasource.hikari.*
回答by Raf
I came across HikariCP
and I was amazed by the benchmarks and I wanted to try it instead of my default choice C3P0
and to my surprise I struggled to get the configurations
right probably because the configurations differ based on what combination of tech stack you are using.
我遇到了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.
I have used Gradle
as build tool and I would like to share what worked for me for the following assumptions:
我已经设置了Spring Boot
带有JPA, Web, Security
starters(使用Spring Initializer)的项目PostgreSQL
,HikariCP
用作连接池的数据库。
我已用作Gradle
构建工具,我想分享以下假设对我有用的内容:
- Spring Boot Starter JPA (Web & Security - optional)
- Gradle build too
- PostgreSQL running and setup with a database (i.e. schema, user, db)
- Spring Boot Starter JPA(Web & Security - 可选)
- Gradle 也构建
- 使用数据库(即模式、用户、数据库)运行和设置 PostgreSQL
You need the following build.gradle
if you are using Gradle
or equivalent pom.xml
if you are using maven
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'
}
}
There are a bunch of excludes in the above build.gradle
and that's because
上面有一堆排除项build.gradle
,那是因为
- First exclude, instructs gradle that exclude the
jdbc-tomcat
connection pool when downloading thespring-boot-starter-data-jpa
dependencies. This can be achieved by setting up thespring.datasource.type=com.zaxxer.hikari.HikariDataSource
also but, I don't want an extra dependency if I don't need it - Second exclude, instructs gradle to exclude
hibernate-core
when downloadingcom.zaxxer
dependency and that's becausehibernate-core
is already downloaded bySpring Boot
and we don't want to end up with different versions. - Third exclude, instructs gradle to exclude
hibernate-core
when downloadinghibernate-hikaricp
module which is needed in order to make HikariCP useorg.hibernate.hikaricp.internal.HikariCPConnectionProvider
as connection provider instead of deprecatedcom.zaxxer.hikari.hibernate.HikariConnectionProvider
- 首先排除,指示gradle
jdbc-tomcat
在下载spring-boot-starter-data-jpa
依赖时排除连接池。这可以通过设置也来实现,spring.datasource.type=com.zaxxer.hikari.HikariDataSource
但是,如果我不需要它,我不想要额外的依赖 - 第二个排除,指示 gradle
hibernate-core
在下载com.zaxxer
依赖项时排除,这是因为hibernate-core
已经下载了Spring Boot
,我们不想最终得到不同的版本。 - 第三次排除,指示 gradle
hibernate-core
在下载hibernate-hikaricp
模块时排除,以便使 HikariCPorg.hibernate.hikaricp.internal.HikariCPConnectionProvider
用作连接提供者而不是弃用com.zaxxer.hikari.hibernate.HikariConnectionProvider
Once I figured out the build.gradle
and what to keep and what to not, I was ready to copy/paste a datasource
configuration into my application.properties
and expected everything to work with flying colors but, not really and I stumbled upon the following issues
一旦我弄清楚build.gradle
要保留什么和不保留什么,我就准备将datasource
配置复制/粘贴到我的配置中,application.properties
并希望一切都能正常工作,但并非如此,我偶然发现了以下问题
- Spring boot failing to find out database details (i.e. url, driver) hence, not able to setup jpa and hibernate (because I didn't name the property key values right)
- HikariCP falling back to
com.zaxxer.hikari.hibernate.HikariConnectionProvider
- After instructing Spring to use new connection-provider for when auto-configuring hibernate/jpa then HikariCP failed because it was looking for some
key/value
in theapplication.properties
and was complaining aboutdataSource, dataSourceClassName, jdbcUrl
. I had to debug intoHikariConfig, HikariConfigurationUtil, HikariCPConnectionProvider
and found out thatHikariCP
could not find the properties fromapplication.properties
because it was named differently.
- Spring boot 无法找到数据库详细信息(即 url、驱动程序),因此无法设置 jpa 和 hibernate(因为我没有正确命名属性键值)
- HikariCP 回落到
com.zaxxer.hikari.hibernate.HikariConnectionProvider
- 后指示Spring使用新的连接提供商时自动配置休眠/ JPA然后HikariCP失败,因为它是寻找一些
key/value
在application.properties
并抱怨dataSource, dataSourceClassName, jdbcUrl
。我不得不调试HikariConfig, HikariConfigurationUtil, HikariCPConnectionProvider
并发现HikariCP
无法找到属性,application.properties
因为它的命名不同。
Anyway, this is where I had to rely on trial and error and make sure that HikariCP
is able to pick the properties (i.e. data source that's db details, as well as pooling properties) as well as Sping Boot behave as expected and I ended up with the following application.properties
file.
无论如何,这是我必须依靠反复试验并确保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
As shown above the configurations are divided into categories based on following naming patterns
如上所示,配置根据以下命名模式分为几类
- spring.datasource.x(Spring auto-configure will pick these, so will HikariCP)
- spring.datasource.hikari.x(HikariCP picks these to setup the pool, make a note of the camelCase field names)
- spring.jpa.hibernate.connection.provider_class(Instructs Spring to use new HibernateConnectionProvider)
- spring.jpa.properties.hibernate.x(Used by Spring to auto-configure JPA, make a note of the field names with underscores)
- 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,用下划线记下字段名)
It's hard to come across a tutorial or post or some resource that shows how the above properties file is used and how the properties should be named. Well, there you have it.
很难找到说明如何使用上述属性文件以及如何命名属性的教程或帖子或某些资源。好吧,你有它。
Throwing the above application.properties
with build.gradle
(or at least similar) into a Spring Boot JPA project version (1.5.8) should work like a charm and connect to your pre-configured database (i.e. in my case it's PostgreSQL that both HikariCP & Spring
figure out from the spring.datasource.url
on which database driver to use).
投掷上面application.properties
用build.gradle
(或至少相似),在Spring的引导JPA项目版本(1.5.8)应该工作就像一个魅力,并连接到您的预配置的数据库(即在我的情况下,它的PostgreSQL的这两个HikariCP & Spring
数字出从spring.datasource.url
上要使用的数据库驱动程序)。
I did not see the need to create a DataSource
bean and that's because Spring Boot is capable of doing everything for me just by looking into application.properties
and that's neat.
我没有看到需要创建一个DataSource
bean,那是因为 Spring Boot 能够为我做任何事情,只需查看即可application.properties
,这很整洁。
The articlein HikariCP's github wikishows how to setup Spring Boot with JPA but, lacks explanation and details.
HikariCP 的 github wiki中的文章展示了如何使用 JPA 设置 Spring Boot,但缺乏解释和细节。
The above two file is also availble as a public gist https://gist.github.com/rhamedy/b3cb936061cc03acdfe21358b86a5bc6
以上两个文件也可作为公共要点https://gist.github.com/rhamedy/b3cb936061cc03acdfe21358b86a5bc6
回答by Andy Brown
With the later spring-boot releases switching to Hikari can be done entirely in configuration. I'm using 1.5.6.RELEASE
and this approach works.
随着后来的 spring-boot 版本切换到 Hikari,可以完全在配置中完成。我正在使用1.5.6.RELEASE
并且这种方法有效。
build.gradle:
构建.gradle:
compile "com.zaxxer:HikariCP:2.7.3"
application YAML
应用程序 YAML
spring:
datasource:
type: com.zaxxer.hikari.HikariDataSource
hikari:
idleTimeout: 60000
minimumIdle: 2
maximumPoolSize: 20
connectionTimeout: 30000
poolName: MyPoolName
connectionTestQuery: SELECT 1
Change connectionTestQuery
to suit your underlying DB. That's it, no code required.
更改connectionTestQuery
以适合您的基础数据库。就是这样,不需要代码。
回答by leventunver
Here is the good news. HikariCP is the default connection pool now with Spring Boot 2.0.0.
这是好消息。HikariCP 现在是 Spring Boot 2.0.0 的默认连接池。
Spring Boot 2.0.0 Release Notes
The default database pooling technology in Spring Boot 2.0 has been switched from Tomcat Pool to HikariCP. We've found that Hakari offers superior performance, and many of our users prefer it over Tomcat Pool.
Spring Boot 2.0 中默认的数据库池技术已经从 Tomcat Pool 切换到 HikariCP。我们发现 Hakari 提供了卓越的性能,与 Tomcat Pool 相比,我们的许多用户更喜欢它。