Skip to content

Commit c8b67be

Browse files
committed
Polish "Add additional properties to configure R2DBC pool"
See gh-21219
1 parent 0d41596 commit c8b67be

File tree

4 files changed

+35
-34
lines changed

4 files changed

+35
-34
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/r2dbc/ConnectionFactoryConfigurations.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
3030
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
3131
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
32+
import org.springframework.boot.context.properties.PropertyMapper;
3233
import org.springframework.context.annotation.Bean;
3334
import org.springframework.context.annotation.Condition;
3435
import org.springframework.context.annotation.ConditionContext;
@@ -69,15 +70,16 @@ ConnectionPool connectionFactory(R2dbcProperties properties, ResourceLoader reso
6970
ConnectionFactory connectionFactory = createConnectionFactory(properties, resourceLoader.getClassLoader(),
7071
customizers.orderedStream().collect(Collectors.toList()));
7172
R2dbcProperties.Pool pool = properties.getPool();
72-
ConnectionPoolConfiguration.Builder builder = ConnectionPoolConfiguration.builder(connectionFactory)
73-
.initialSize(pool.getInitialSize()).maxSize(pool.getMaxSize()).maxIdleTime(pool.getMaxIdleTime())
74-
.maxLifeTime(pool.getMaxLifeTime()).maxAcquireTime(pool.getMaxAcquireTime())
75-
.maxCreateConnectionTime(pool.getMaxCreateConnectionTime())
76-
.validationDepth(pool.getValidationDepth());
77-
78-
if (StringUtils.hasText(pool.getValidationQuery())) {
79-
builder.validationQuery(pool.getValidationQuery());
80-
}
73+
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
74+
ConnectionPoolConfiguration.Builder builder = ConnectionPoolConfiguration.builder(connectionFactory);
75+
map.from(pool.getMaxIdleTime()).to(builder::maxIdleTime);
76+
map.from(pool.getMaxLifeTime()).to(builder::maxLifeTime);
77+
map.from(pool.getMaxAcquireTime()).to(builder::maxAcquireTime);
78+
map.from(pool.getMaxCreateConnectionTime()).to(builder::maxCreateConnectionTime);
79+
map.from(pool.getInitialSize()).to(builder::initialSize);
80+
map.from(pool.getMaxSize()).to(builder::maxSize);
81+
map.from(pool.getValidationQuery()).whenHasText().to(builder::validationQuery);
82+
map.from(pool.getValidationDepth()).to(builder::validationDepth);
8183
return new ConnectionPool(builder.build());
8284
}
8385

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/r2dbc/R2dbcProperties.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,19 +142,21 @@ public static class Pool {
142142
private Duration maxIdleTime = Duration.ofMinutes(30);
143143

144144
/**
145-
* Max lifetime.
145+
* Maximum lifetime of a connection in the pool. By default, connections have an
146+
* infinite lifetime.
146147
*/
147-
private Duration maxLifeTime = Duration.ofMinutes(0L);
148+
private Duration maxLifeTime;
148149

149150
/**
150-
* Max acquire time.
151+
* Maximum time to acquire a connection from the pool. By default, wait
152+
* indefinitely.
151153
*/
152-
private Duration maxAcquireTime = Duration.ofMinutes(0L);
154+
private Duration maxAcquireTime;
153155

154156
/**
155-
* Max create connection time.
157+
* Maximum time to wait to create a new connection. By default, wait indefinitely.
156158
*/
157-
private Duration maxCreateConnectionTime = Duration.ofMinutes(0L);
159+
private Duration maxCreateConnectionTime;
158160

159161
/**
160162
* Initial connection pool size.

spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1469,7 +1469,7 @@
14691469
"description": "Whether pooling is enabled. Enabled automatically if \"r2dbc-pool\" is on the classpath."
14701470
},
14711471
{
1472-
"name": "spring.r2dbc.pool.validationDepth",
1472+
"name": "spring.r2dbc.pool.validation-depth",
14731473
"defaultValue": "local"
14741474
},
14751475
{

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/r2dbc/R2dbcAutoConfigurationTests.java

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import io.r2dbc.pool.PoolMetrics;
3030
import io.r2dbc.spi.ConnectionFactory;
3131
import io.r2dbc.spi.Option;
32-
import io.r2dbc.spi.ValidationDepth;
3332
import org.junit.jupiter.api.Test;
3433

3534
import org.springframework.beans.factory.BeanCreationException;
@@ -63,27 +62,25 @@ void configureWithUrlCreateConnectionPoolByDefault() {
6362

6463
@Test
6564
void configureWithUrlAndPoolPropertiesApplyProperties() {
66-
this.contextRunner
67-
.withPropertyValues("spring.r2dbc.url:r2dbc:h2:mem:///" + randomDatabaseName(),
68-
"spring.r2dbc.pool.initial-size=5", "spring.r2dbc.pool.max-size=15",
69-
"spring.r2dbc.pool.max-idle-time=1ms", "spring.r2dbc.pool.max-life-time=2s",
70-
"spring.r2dbc.pool.max-acquire-time=3m", "spring.r2dbc.pool.max-create-connection-time=4h",
71-
"spring.r2dbc.pool.validation-query=SELECT 1", "spring.r2dbc.pool.validation-depth=remote")
72-
.run((context) -> {
65+
this.contextRunner.withPropertyValues("spring.r2dbc.url:r2dbc:h2:mem:///" + randomDatabaseName(),
66+
"spring.r2dbc.pool.max-size=15", "spring.r2dbc.pool.max-acquire-time=3m").run((context) -> {
7367
assertThat(context).hasSingleBean(ConnectionFactory.class).hasSingleBean(ConnectionPool.class)
7468
.hasSingleBean(R2dbcProperties.class);
75-
PoolMetrics poolMetrics = context.getBean(ConnectionPool.class).getMetrics().get();
69+
ConnectionPool connectionPool = context.getBean(ConnectionPool.class);
70+
PoolMetrics poolMetrics = connectionPool.getMetrics().get();
7671
assertThat(poolMetrics.getMaxAllocatedSize()).isEqualTo(15);
72+
assertThat(connectionPool).hasFieldOrPropertyWithValue("maxAcquireTime", Duration.ofMinutes(3));
73+
});
74+
}
7775

78-
R2dbcProperties properties = context.getBean(R2dbcProperties.class);
79-
assertThat(properties.getPool().getInitialSize()).isEqualTo(5);
80-
assertThat(properties.getPool().getMaxSize()).isEqualTo(15);
81-
assertThat(properties.getPool().getMaxIdleTime()).isEqualTo(Duration.ofMillis(1));
82-
assertThat(properties.getPool().getMaxLifeTime()).isEqualTo(Duration.ofSeconds(2));
83-
assertThat(properties.getPool().getMaxAcquireTime()).isEqualTo(Duration.ofMinutes(3));
84-
assertThat(properties.getPool().getMaxCreateConnectionTime()).isEqualTo(Duration.ofHours(4));
85-
assertThat(properties.getPool().getValidationQuery()).isEqualTo("SELECT 1");
86-
assertThat(properties.getPool().getValidationDepth()).isEqualTo(ValidationDepth.REMOTE);
76+
@Test
77+
void configureWithUrlAndDefaultDoNotOverrideDefaultTimeouts() {
78+
this.contextRunner.withPropertyValues("spring.r2dbc.url:r2dbc:h2:mem:///" + randomDatabaseName())
79+
.run((context) -> {
80+
assertThat(context).hasSingleBean(ConnectionFactory.class).hasSingleBean(ConnectionPool.class)
81+
.hasSingleBean(R2dbcProperties.class);
82+
ConnectionPool connectionPool = context.getBean(ConnectionPool.class);
83+
assertThat(connectionPool).hasFieldOrPropertyWithValue("maxAcquireTime", Duration.ZERO);
8784
});
8885
}
8986

0 commit comments

Comments
 (0)