Skip to content

Commit 947ac8d

Browse files
committed
Map MinIdle and MaxValidationTime to R2DBC pools
Closes gh-34724
1 parent 52789cb commit 947ac8d

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ ConnectionPool connectionFactory(R2dbcProperties properties, ResourceLoader reso
103103
map.from(pool.getMaxSize()).to(builder::maxSize);
104104
map.from(pool.getValidationQuery()).whenHasText().to(builder::validationQuery);
105105
map.from(pool.getValidationDepth()).to(builder::validationDepth);
106+
map.from(pool.getMinIdle()).to(builder::minIdle);
107+
map.from(pool.getMaxValidationTime()).to(builder::maxValidationTime);
106108
return new ConnectionPool(builder.build());
107109
}
108110

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -136,6 +136,13 @@ public String determineUniqueName() {
136136

137137
public static class Pool {
138138

139+
/**
140+
* Minimal number of idle connections.
141+
*
142+
* @since 2.7.12
143+
*/
144+
private int minIdle = 0;
145+
139146
/**
140147
* Maximum amount of time that a connection is allowed to sit idle in the pool.
141148
*/
@@ -153,6 +160,14 @@ public static class Pool {
153160
*/
154161
private Duration maxAcquireTime;
155162

163+
/**
164+
* Maximum time to validate a connection from the pool. By default, wait
165+
* indefinitely.
166+
*
167+
* @since 2.7.12
168+
*/
169+
private Duration maxValidationTime;
170+
156171
/**
157172
* Maximum time to wait to create a new connection. By default, wait indefinitely.
158173
*/
@@ -183,6 +198,14 @@ public static class Pool {
183198
*/
184199
private boolean enabled = true;
185200

201+
public int getMinIdle() {
202+
return this.minIdle;
203+
}
204+
205+
public void setMinIdle(int minIdle) {
206+
this.minIdle = minIdle;
207+
}
208+
186209
public Duration getMaxIdleTime() {
187210
return this.maxIdleTime;
188211
}
@@ -199,6 +222,14 @@ public void setMaxLifeTime(Duration maxLifeTime) {
199222
this.maxLifeTime = maxLifeTime;
200223
}
201224

225+
public Duration getMaxValidationTime() {
226+
return this.maxValidationTime;
227+
}
228+
229+
public void setMaxValidationTime(Duration maxValidationTime) {
230+
this.maxValidationTime = maxValidationTime;
231+
}
232+
202233
public Duration getMaxAcquireTime() {
203234
return this.maxAcquireTime;
204235
}

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,25 @@ void configureWithUrlCreateConnectionPoolByDefault() {
7777
void configureWithUrlAndPoolPropertiesApplyProperties() {
7878
this.contextRunner
7979
.withPropertyValues("spring.r2dbc.url:r2dbc:h2:mem:///" + randomDatabaseName(),
80-
"spring.r2dbc.pool.max-size=15", "spring.r2dbc.pool.max-acquire-time=3m")
80+
"spring.r2dbc.pool.max-size=15", "spring.r2dbc.pool.max-acquire-time=3m",
81+
"spring.r2dbc.pool.min-idle=1", "spring.r2dbc.pool.max-validation-time=1s",
82+
"spring.r2dbc.pool.initial-size=0")
8183
.run((context) -> {
8284
assertThat(context).hasSingleBean(ConnectionFactory.class)
8385
.hasSingleBean(ConnectionPool.class)
8486
.hasSingleBean(R2dbcProperties.class);
8587
ConnectionPool connectionPool = context.getBean(ConnectionPool.class);
86-
PoolMetrics poolMetrics = connectionPool.getMetrics().get();
87-
assertThat(poolMetrics.getMaxAllocatedSize()).isEqualTo(15);
88-
assertThat(connectionPool).hasFieldOrPropertyWithValue("maxAcquireTime", Duration.ofMinutes(3));
88+
connectionPool.warmup().block();
89+
try {
90+
PoolMetrics poolMetrics = connectionPool.getMetrics().get();
91+
assertThat(poolMetrics.idleSize()).isEqualTo(1);
92+
assertThat(poolMetrics.getMaxAllocatedSize()).isEqualTo(15);
93+
assertThat(connectionPool).hasFieldOrPropertyWithValue("maxAcquireTime", Duration.ofMinutes(3));
94+
assertThat(connectionPool).hasFieldOrPropertyWithValue("maxValidationTime", Duration.ofSeconds(1));
95+
}
96+
finally {
97+
connectionPool.close().block();
98+
}
8999
});
90100
}
91101

0 commit comments

Comments
 (0)