From 947ac8df7300a9cad3312d6726bdcd536964822d Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Wed, 10 May 2023 10:05:11 +0200 Subject: [PATCH] Map MinIdle and MaxValidationTime to R2DBC pools Closes gh-34724 --- .../ConnectionFactoryConfigurations.java | 2 ++ .../autoconfigure/r2dbc/R2dbcProperties.java | 33 ++++++++++++++++++- .../r2dbc/R2dbcAutoConfigurationTests.java | 18 +++++++--- 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/r2dbc/ConnectionFactoryConfigurations.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/r2dbc/ConnectionFactoryConfigurations.java index a4c636d2a1..25ec1ea363 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/r2dbc/ConnectionFactoryConfigurations.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/r2dbc/ConnectionFactoryConfigurations.java @@ -103,6 +103,8 @@ abstract class ConnectionFactoryConfigurations { map.from(pool.getMaxSize()).to(builder::maxSize); map.from(pool.getValidationQuery()).whenHasText().to(builder::validationQuery); map.from(pool.getValidationDepth()).to(builder::validationDepth); + map.from(pool.getMinIdle()).to(builder::minIdle); + map.from(pool.getMaxValidationTime()).to(builder::maxValidationTime); return new ConnectionPool(builder.build()); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/r2dbc/R2dbcProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/r2dbc/R2dbcProperties.java index bcaab87b89..fb662fba69 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/r2dbc/R2dbcProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/r2dbc/R2dbcProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -136,6 +136,13 @@ public class R2dbcProperties { public static class Pool { + /** + * Minimal number of idle connections. + * + * @since 2.7.12 + */ + private int minIdle = 0; + /** * Maximum amount of time that a connection is allowed to sit idle in the pool. */ @@ -153,6 +160,14 @@ public class R2dbcProperties { */ private Duration maxAcquireTime; + /** + * Maximum time to validate a connection from the pool. By default, wait + * indefinitely. + * + * @since 2.7.12 + */ + private Duration maxValidationTime; + /** * Maximum time to wait to create a new connection. By default, wait indefinitely. */ @@ -183,6 +198,14 @@ public class R2dbcProperties { */ private boolean enabled = true; + public int getMinIdle() { + return this.minIdle; + } + + public void setMinIdle(int minIdle) { + this.minIdle = minIdle; + } + public Duration getMaxIdleTime() { return this.maxIdleTime; } @@ -199,6 +222,14 @@ public class R2dbcProperties { this.maxLifeTime = maxLifeTime; } + public Duration getMaxValidationTime() { + return this.maxValidationTime; + } + + public void setMaxValidationTime(Duration maxValidationTime) { + this.maxValidationTime = maxValidationTime; + } + public Duration getMaxAcquireTime() { return this.maxAcquireTime; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/r2dbc/R2dbcAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/r2dbc/R2dbcAutoConfigurationTests.java index 8c475362c1..87ae445521 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/r2dbc/R2dbcAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/r2dbc/R2dbcAutoConfigurationTests.java @@ -77,15 +77,25 @@ class R2dbcAutoConfigurationTests { void configureWithUrlAndPoolPropertiesApplyProperties() { this.contextRunner .withPropertyValues("spring.r2dbc.url:r2dbc:h2:mem:///" + randomDatabaseName(), - "spring.r2dbc.pool.max-size=15", "spring.r2dbc.pool.max-acquire-time=3m") + "spring.r2dbc.pool.max-size=15", "spring.r2dbc.pool.max-acquire-time=3m", + "spring.r2dbc.pool.min-idle=1", "spring.r2dbc.pool.max-validation-time=1s", + "spring.r2dbc.pool.initial-size=0") .run((context) -> { assertThat(context).hasSingleBean(ConnectionFactory.class) .hasSingleBean(ConnectionPool.class) .hasSingleBean(R2dbcProperties.class); ConnectionPool connectionPool = context.getBean(ConnectionPool.class); - PoolMetrics poolMetrics = connectionPool.getMetrics().get(); - assertThat(poolMetrics.getMaxAllocatedSize()).isEqualTo(15); - assertThat(connectionPool).hasFieldOrPropertyWithValue("maxAcquireTime", Duration.ofMinutes(3)); + connectionPool.warmup().block(); + try { + PoolMetrics poolMetrics = connectionPool.getMetrics().get(); + assertThat(poolMetrics.idleSize()).isEqualTo(1); + assertThat(poolMetrics.getMaxAllocatedSize()).isEqualTo(15); + assertThat(connectionPool).hasFieldOrPropertyWithValue("maxAcquireTime", Duration.ofMinutes(3)); + assertThat(connectionPool).hasFieldOrPropertyWithValue("maxValidationTime", Duration.ofSeconds(1)); + } + finally { + connectionPool.close().block(); + } }); }