Java Spring-Boot:如何设置 JDBC 池属性,例如最大连接数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25573034/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:41:09  来源:igfitidea点击:

Spring-Boot: How do I set JDBC pool properties like maximum number of connections?

javaspringjdbcspring-boottomcat-jdbc

提问by JBCP

Spring-Boot is a pretty awesome tool, but the documentation is a bit sparse when it comes to more advanced configuration. How can I set properties like the maximum size for my database connection pool?

Spring-Boot 是一个非常棒的工具,但是当涉及到更高级的配置时,文档有点稀少。如何设置数据库连接池的最大大小等属性?

Spring-Boot supports tomcat-jdbc, HikariCPand Commons DBCPnatively are they all configured the same way?

Spring-Boot 支持tomcat-jdbcHikariCP并且Commons DBCP它们本身的配置方式都相同吗?

采纳答案by JBCP

It turns out setting these configuration properties is pretty straight forward, but the official documentationis more general so it might be hard to find when searching specifically for connection pool configuration information.

事实证明,设置这些配置属性非常简单,但官方文档更通用,因此在专门搜索连接池配置信息时可能很难找到。

To set the maximum pool size for tomcat-jdbc, set this property in your .properties or .yml file:

要为 tomcat-jdbc 设置最大池大小,请在 .properties 或 .yml 文件中设置此属性:

spring.datasource.maxActive=5

You can also use the following if you prefer:

如果您愿意,也可以使用以下内容:

spring.datasource.max-active=5

You can set any connection pool property you want this way. Here is a complete list of properties supported by tomcat-jdbc.

您可以通过这种方式设置您想要的任何连接池属性。这里是 支持的属性的完整列表tomcat-jdbc

To understand how this works more generally you need to dig into the Spring-Boot code a bit.

要更广泛地了解这是如何工作的,您需要深入研究 Spring-Boot 代码。

Spring-Boot constructs the DataSource like this (see here, line 102):

Spring-Boot 像这样构造 DataSource(参见此处,第 102 行):

@ConfigurationProperties(prefix = DataSourceAutoConfiguration.CONFIGURATION_PREFIX)
@Bean
public DataSource dataSource() {
    DataSourceBuilder factory = DataSourceBuilder
            .create(this.properties.getClassLoader())
            .driverClassName(this.properties.getDriverClassName())
            .url(this.properties.getUrl())
            .username(this.properties.getUsername())
            .password(this.properties.getPassword());
    return factory.build();
}

The DataSourceBuilder is responsible for figuring out which pooling library to use, by checking for each of a series of know classes on the classpath. It then constructs the DataSource and returns it to the dataSource()function.

DataSourceBuilder 负责通过检查类路径上的一系列已知类中的每一个来确定要使用的池库。然后它构造 DataSource 并将其返回给dataSource()函数。

At this point, magic kicks in using @ConfigurationProperties. This annotation tells Spring to look for properties with prefix CONFIGURATION_PREFIX(which is spring.datasource). For each property that starts with that prefix, Spring will try to call the setter on the DataSource with that property.

在这一点上,魔术开始使用@ConfigurationProperties。该注解告诉 Spring 查找带有前缀CONFIGURATION_PREFIX(即spring.datasource)的属性。对于以该前缀开头的每个属性,Spring 将尝试使用该属性调用 DataSource 上的 setter。

The Tomcat DataSource is an extension of DataSourceProxy, which has the method setMaxActive().

Tomcat的数据源是的延伸DataSourceProxy,其具有方法setMaxActive()

And that's how your spring.datasource.maxActive=5gets applied correctly!

这就是您spring.datasource.maxActive=5正确应用的方式!

What about other connection pools

其他连接池呢

I haven't tried, but if you are using one of the other Spring-Boot supported connection pools (currently HikariCP or Commons DBCP) you should be able to set the properties the same way, but you'll need to look at the project documentation to know what is available.

我还没有尝试过,但是如果您使用的是其他 Spring-Boot 支持的连接池之一(当前是 HikariCP 或 Commons DBCP),您应该能够以相同的方式设置属性,但您需要查看项目文档以了解可用的内容。

回答by kinjelom

Different connections pools have different configs.

不同的连接池有不同的配置。

For example Tomcat (default) expects:

例如 Tomcat(默认)期望:

spring.datasource.ourdb.url=...

and HikariCP will be happy with:

和 HikariCP 会很高兴:

spring.datasource.ourdb.jdbc-url=...

We can satisfy both without boilerplate configuration:

我们可以在没有样板配置的情况下满足两者:

spring.datasource.ourdb.jdbc-url=${spring.datasource.ourdb.url}


There is no property to define connection pool provider.

没有定义连接池提供程序的属性。

Take a look at source DataSourceBuilder.java

看一下源DataSourceBuilder.java

If Tomcat, HikariCP or Commons DBCP are on the classpath one of them will be selected (in that order with Tomcat first).

如果 Tomcat、HikariCP 或 Commons DBCP 在类路径中,将选择其中之一(以 Tomcat 为先的顺序)。

... so, we can easily replace connection pool provider using this maven configuration (pom.xml):

...因此,我们可以使用此 Maven 配置 (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>       

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

回答by Daniel Nuss

At the current version of Spring-Boot (1.4.1.RELEASE) , each pooling datasource implementation has its own prefix for properties.

在当前版本的 Spring-Boot (1.4.1.RELEASE) 中,每个池化数据源实现都有自己的属性前缀。

For instance, if you are using tomcat-jdbc:

例如,如果您使用的是 tomcat-jdbc:

spring.datasource.tomcat.max-wait=10000

You can find the explanation out here

你可以在这里找到解释

spring.datasource.max-wait=10000

this have no effect anymore.

这不再有效。

回答by Mariano LEANCE

In spring boot 2.x you need to reference provider specific properties.

在 spring boot 2.x 中,您需要引用提供程序特定的属性。

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html#boot-features-connect-to-production-database

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html#boot-features-connect-to-production-database

The default, hikari can be set with spring.datasource.hikari.maximum-pool-size.

默认情况下,hikari 可以设置为spring.datasource.hikari.maximum-pool-size.

回答by Pravin

Based on your application type/size/load/no. of users ..etc - u can keep following as your production properties

基于您的应用程序类型/大小/负载/编号。用户 .. 等 - 您可以继续关注您的生产属性

spring.datasource.tomcat.initial-size=50
spring.datasource.tomcat.max-wait=20000
spring.datasource.tomcat.max-active=300
spring.datasource.tomcat.max-idle=150
spring.datasource.tomcat.min-idle=8
spring.datasource.tomcat.default-auto-commit=true