Prepare connection pool.

We now prepare the pool when the connection factory is started respective the pool has been instantiated.

Closes #3072
This commit is contained in:
Asmir Mustafic
2024-12-12 22:32:09 +01:00
committed by Mark Paluch
parent e3c91e94f1
commit b7381ac6bb
2 changed files with 33 additions and 2 deletions

View File

@@ -55,6 +55,7 @@ import org.springframework.util.Assert;
*
* @author Mark Paluch
* @author Christoph Strobl
* @author Asmir Mustafic
* @since 2.0
* @see #getConnection(Class)
*/
@@ -90,8 +91,18 @@ class LettucePoolingConnectionProvider implements LettuceConnectionProvider, Red
public <T extends StatefulConnection<?, ?>> T getConnection(Class<T> connectionType) {
GenericObjectPool<StatefulConnection<?, ?>> pool = pools.computeIfAbsent(connectionType, poolType -> {
return ConnectionPoolSupport.createGenericObjectPool(() -> connectionProvider.getConnection(connectionType),
poolConfig, false);
GenericObjectPool<StatefulConnection<?, ?>> newPool = ConnectionPoolSupport
.createGenericObjectPool(() -> connectionProvider.getConnection(connectionType), poolConfig, false);
try {
newPool.preparePool();
} catch (Exception ex) {
throw new PoolException("Could not prepare the pool", ex);
}
return newPool;
});
try {

View File

@@ -20,6 +20,7 @@ import static org.mockito.Mockito.*;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.async.RedisAsyncCommands;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@@ -27,11 +28,13 @@ import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration.LettucePoolingClientConfigurationBuilder;
/**
* Unit tests for {@link LettucePoolingConnectionProvider}.
*
* @author Mark Paluch
* @author Asmir Mustafic
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
@@ -70,4 +73,21 @@ class LettucePoolingConnectionProviderUnitTests {
verify(commandsMock).discard();
}
@Test
void shouldPrepareThePool() {
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMinIdle(5);
poolConfig.setMaxIdle(8);
poolConfig.setMaxTotal(10);
LettucePoolingClientConfiguration config = new LettucePoolingClientConfigurationBuilder().poolConfig(poolConfig)
.build();
LettucePoolingConnectionProvider provider = new LettucePoolingConnectionProvider(connectionProviderMock, config);
provider.getConnection(StatefulRedisConnection.class);
verify(connectionProviderMock, times(5)).getConnection(any());
}
}