Skip to content

Commit b7381ac

Browse files
goetasmp911de
authored andcommitted
Prepare connection pool.
We now prepare the pool when the connection factory is started respective the pool has been instantiated. Closes #3072
1 parent e3c91e9 commit b7381ac

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/main/java/org/springframework/data/redis/connection/lettuce/LettucePoolingConnectionProvider.java

+13-2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
*
5656
* @author Mark Paluch
5757
* @author Christoph Strobl
58+
* @author Asmir Mustafic
5859
* @since 2.0
5960
* @see #getConnection(Class)
6061
*/
@@ -90,8 +91,18 @@ class LettucePoolingConnectionProvider implements LettuceConnectionProvider, Red
9091
public <T extends StatefulConnection<?, ?>> T getConnection(Class<T> connectionType) {
9192

9293
GenericObjectPool<StatefulConnection<?, ?>> pool = pools.computeIfAbsent(connectionType, poolType -> {
93-
return ConnectionPoolSupport.createGenericObjectPool(() -> connectionProvider.getConnection(connectionType),
94-
poolConfig, false);
94+
95+
GenericObjectPool<StatefulConnection<?, ?>> newPool = ConnectionPoolSupport
96+
.createGenericObjectPool(() -> connectionProvider.getConnection(connectionType), poolConfig, false);
97+
98+
try {
99+
newPool.preparePool();
100+
101+
} catch (Exception ex) {
102+
throw new PoolException("Could not prepare the pool", ex);
103+
}
104+
105+
return newPool;
95106
});
96107

97108
try {

src/test/java/org/springframework/data/redis/connection/lettuce/LettucePoolingConnectionProviderUnitTests.java

+20
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,21 @@
2020
import io.lettuce.core.api.StatefulRedisConnection;
2121
import io.lettuce.core.api.async.RedisAsyncCommands;
2222

23+
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
2324
import org.junit.jupiter.api.BeforeEach;
2425
import org.junit.jupiter.api.Test;
2526
import org.junit.jupiter.api.extension.ExtendWith;
2627
import org.mockito.Mock;
2728
import org.mockito.junit.jupiter.MockitoExtension;
2829
import org.mockito.junit.jupiter.MockitoSettings;
2930
import org.mockito.quality.Strictness;
31+
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration.LettucePoolingClientConfigurationBuilder;
3032

3133
/**
3234
* Unit tests for {@link LettucePoolingConnectionProvider}.
3335
*
3436
* @author Mark Paluch
37+
* @author Asmir Mustafic
3538
*/
3639
@ExtendWith(MockitoExtension.class)
3740
@MockitoSettings(strictness = Strictness.LENIENT)
@@ -70,4 +73,21 @@ void shouldDiscardTransactionOnReleaseOnActiveTransaction() {
7073

7174
verify(commandsMock).discard();
7275
}
76+
77+
@Test
78+
void shouldPrepareThePool() {
79+
80+
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
81+
poolConfig.setMinIdle(5);
82+
poolConfig.setMaxIdle(8);
83+
poolConfig.setMaxTotal(10);
84+
85+
LettucePoolingClientConfiguration config = new LettucePoolingClientConfigurationBuilder().poolConfig(poolConfig)
86+
.build();
87+
88+
LettucePoolingConnectionProvider provider = new LettucePoolingConnectionProvider(connectionProviderMock, config);
89+
90+
provider.getConnection(StatefulRedisConnection.class);
91+
verify(connectionProviderMock, times(5)).getConnection(any());
92+
}
7393
}

0 commit comments

Comments
 (0)