Skip to content

Commit 921c362

Browse files
committed
Add 'spring.r2dbc.pool.acquire-retry' property
Add a new property to configure the pool acquire retry value. Closes gh-44017
1 parent c1cb4c5 commit 921c362

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ ConnectionPool connectionFactory(R2dbcProperties properties,
108108
map.from(pool.getMaxIdleTime()).to(builder::maxIdleTime);
109109
map.from(pool.getMaxLifeTime()).to(builder::maxLifeTime);
110110
map.from(pool.getMaxAcquireTime()).to(builder::maxAcquireTime);
111+
map.from(pool.getAcquireRetry()).to(builder::acquireRetry);
111112
map.from(pool.getMaxCreateConnectionTime()).to(builder::maxCreateConnectionTime);
112113
map.from(pool.getInitialSize()).to(builder::initialSize);
113114
map.from(pool.getMaxSize()).to(builder::maxSize);

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ public static class Pool {
158158
*/
159159
private Duration maxAcquireTime;
160160

161+
/**
162+
* Number of acquire retries if the first acquire attempt fails.
163+
*/
164+
private int acquireRetry = 1;
165+
161166
/**
162167
* Maximum time to validate a connection from the pool. By default, wait
163168
* indefinitely.
@@ -234,6 +239,14 @@ public void setMaxAcquireTime(Duration maxAcquireTime) {
234239
this.maxAcquireTime = maxAcquireTime;
235240
}
236241

242+
public int getAcquireRetry() {
243+
return this.acquireRetry;
244+
}
245+
246+
public void setAcquireRetry(int acquireRetry) {
247+
this.acquireRetry = acquireRetry;
248+
}
249+
237250
public Duration getMaxCreateConnectionTime() {
238251
return this.maxCreateConnectionTime;
239252
}

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 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.
@@ -35,6 +35,7 @@
3535
import org.assertj.core.api.InstanceOfAssertFactory;
3636
import org.assertj.core.api.ObjectAssert;
3737
import org.junit.jupiter.api.Test;
38+
import reactor.core.publisher.Mono;
3839

3940
import org.springframework.beans.factory.BeanCreationException;
4041
import org.springframework.boot.autoconfigure.AutoConfigurations;
@@ -47,6 +48,7 @@
4748
import org.springframework.context.annotation.Bean;
4849
import org.springframework.context.annotation.Configuration;
4950
import org.springframework.r2dbc.core.DatabaseClient;
51+
import org.springframework.test.util.ReflectionTestUtils;
5052

5153
import static org.assertj.core.api.Assertions.assertThat;
5254

@@ -82,8 +84,8 @@ void configureWithUrlAndPoolPropertiesApplyProperties() {
8284
this.contextRunner
8385
.withPropertyValues("spring.r2dbc.url:r2dbc:h2:mem:///" + randomDatabaseName(),
8486
"spring.r2dbc.pool.max-size=15", "spring.r2dbc.pool.max-acquire-time=3m",
85-
"spring.r2dbc.pool.min-idle=1", "spring.r2dbc.pool.max-validation-time=1s",
86-
"spring.r2dbc.pool.initial-size=0")
87+
"spring.r2dbc.pool.acquire-retry=5", "spring.r2dbc.pool.min-idle=1",
88+
"spring.r2dbc.pool.max-validation-time=1s", "spring.r2dbc.pool.initial-size=0")
8789
.run((context) -> {
8890
assertThat(context).hasSingleBean(ConnectionFactory.class)
8991
.hasSingleBean(ConnectionPool.class)
@@ -96,6 +98,9 @@ void configureWithUrlAndPoolPropertiesApplyProperties() {
9698
assertThat(poolMetrics.getMaxAllocatedSize()).isEqualTo(15);
9799
assertThat(connectionPool).hasFieldOrPropertyWithValue("maxAcquireTime", Duration.ofMinutes(3));
98100
assertThat(connectionPool).hasFieldOrPropertyWithValue("maxValidationTime", Duration.ofSeconds(1));
101+
Mono<?> create = (Mono<?>) ReflectionTestUtils.getField(connectionPool, "create");
102+
assertThat(create.getClass().getName()).endsWith("MonoRetry");
103+
assertThat(create).hasFieldOrPropertyWithValue("times", 5L);
99104
}
100105
finally {
101106
connectionPool.close().block();

0 commit comments

Comments
 (0)