Spring-Boot:如何设置JDBC池属性,如最大连接数? [英] Spring-Boot: How do I set JDBC pool properties like maximum number of connections?

查看:737
本文介绍了Spring-Boot:如何设置JDBC池属性,如最大连接数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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支持 tomcat-jdbc HikariCP Commons DBCP 本机是否都以相同的方式配置?

Spring-Boot supports tomcat-jdbc, HikariCP and Commons DBCP natively are they all configured the same way?

推荐答案


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

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

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

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

spring.datasource.maxActive=5

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

You can also use the following if you prefer:

spring.datasource.max-active=5

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

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

要了解其工作原理,您需要深入了解一下Spring-Boot代码。

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

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

Spring-Boot constructs the DataSource like this (see here, line 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();
}

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

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.

此时,魔术开始了使用 @ConfigurationProperties 。这个注释告诉Spring查找前缀为 CONFIGURATION_PREFIX 的属性(这是 spring.datasource )。对于以该前缀开头的每个属性,Spring将尝试使用该属性调用DataSource上的setter。

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.

Tomcat DataSource是DataSourceProxy ,其方法为 setMaxActive ()

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

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

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

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

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:如何设置JDBC池属性,如最大连接数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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