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

查看:113
本文介绍了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-jdbcHikariCPCommons 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() 函数.

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 数据源是DataSourceProxy<的扩展/a>,它具有方法 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天全站免登陆