postgres r2dbc-pool 的连接池大小

Connection pool size with postgres r2dbc-pool

我无法使用 spring-webflux 和 r2dbc(使用 r2dbc-pool 驱动程序 0.8.0.M8)打开超过 10 个连接。我的配置如下:

@Configuration
public class PostgresConfig extends AbstractR2dbcConfiguration {

  @Override
  @Bean
  public ConnectionFactory connectionFactory() {
    ConnectionFactory connectionFactory = ConnectionFactories.get(ConnectionFactoryOptions.builder()
        .option(DRIVER, "pool")
        .option(PROTOCOL, "postgresql")
        .option(HOST, host)
        .option(USER, user)
        .option(PASSWORD, password)
        .option(DATABASE, database)
        .build());
    ConnectionPoolConfiguration configuration = ConnectionPoolConfiguration.builder(connectionFactory)
        .maxIdleTime(Duration.ofMinutes(30))
        .initialSize(initialSize)
        .maxSize(maxSize)
        .maxCreateConnectionTime(Duration.ofSeconds(1))
        .build();
    return new ConnectionPool(configuration);
  }
}

当我指定超过 10 个连接时,我会收到如下错误:

org.springframework.dao.DataAccessResourceFailureException: 
Failed to obtain R2DBC Connection; nested exception is 
java.util.concurrent.TimeoutException: 
Did not observe any item or terminal signal within 1000ms in 'lift' 
(and no fallback has been configured)
    at org.springframework.data.r2dbc.connectionfactory.ConnectionFactoryUtils
    .lambda$getConnection[=11=](ConnectionFactoryUtils.java:71)

此外,连接数与初始大小相同。未创建新连接。

好的,MAX_SIZE 也应该指定为 ConnectionFactoryOptions。否则连接池大小仍然是 10.

import static io.r2dbc.pool.PoolingConnectionFactoryProvider.MAX_SIZE;

    ConnectionFactory connectionFactory = ConnectionFactories.get(ConnectionFactoryOptions.builder()
        .option(DRIVER, "pool")
        .option(PROTOCOL, "postgresql")
        .option(HOST, host)
        .option(USER, user)
        .option(PASSWORD, password)
        .option(DATABASE, database)
        .option(MAX_SIZE, maxSize)
        .build());

如果您使用的是 spring-boot-starter-data-r2dbc,则最小值和最大值可在 application.properties

中配置
spring.r2dbc.pool.initialSize=2
spring.r2dbc.pool.maxSize=2

org.springframework.boot.autoconfigure.r2dbc.R2dbcPropertiesclass

请注意,您可以使用发布版本 0.8.4.RELEASE(这是最新的)https://mvnrepository.com/artifact/io.r2dbc/r2dbc-postgresql/0.8.4.RELEASE,它不需要您实例化 ConnectionFactory

Spring 引导(至少 2.3.4)在 properties/yaml 设置时有一个关于池大小的棘手的“陷阱”。如果您在数据库中包含“pool”url,那么设置的大小(初始大小或最大大小)将没有任何影响,将使用 r2dbc 池的默认值,10 和 10。

这是由于 ConnectionFactoryConfigurations.java 中的 PooledConnectionFactoryCondition 在 spring.r2dbc.pool.enabled=true 时失败,如果 r2dbc-pool 依赖项在类路径上 “池”是 spring.r2dbc.url 属性.

的一部分

来自 PooledConnectionFactoryCondition 文档:

Condition that checks that a ConnectionPool is requested. The condition matches if pooling was opt-in via configuration and the r2dbc url does not contain pooling-related options.

这反过来会导致无法创建 ConnectionPool bean。

跳过 r2dbc url 属性 中的“pool”关键字并具有 r2dbc-pool 依赖项,然后您将获得正确配置的池。

以下是我的配置 spring-boot-starter-data-r2dbc 检查是否对您有帮助:

spring:
  r2dbc:
    url: r2dbc:postgresql://127.0.0.1:5432/test?schema=public
    username: postgres
    password: postgres
    pool:
      name: TEST-POOL
      initial-size: 1
      max-size: 10
      max-idle-time: 30m