Skip to content

Commit 3fad6e5

Browse files
mp911dechristophstrobl
authored andcommitted
DATAREDIS-1027 - Dispose reactive LettuceConnectionProvider on connection factory shutdown.
LettuceConnectionFactory.destroy() now disposes also the reactive LettuceConnectionProvider to free resources of a connection pool. Original Pull Request: #470
1 parent 595c079 commit 3fad6e5

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -299,16 +299,8 @@ public void destroy() {
299299

300300
resetConnection();
301301

302-
if (connectionProvider instanceof DisposableBean) {
303-
try {
304-
((DisposableBean) connectionProvider).destroy();
305-
} catch (Exception e) {
306-
307-
if (log.isWarnEnabled()) {
308-
log.warn(connectionProvider + " did not shut down gracefully.", e);
309-
}
310-
}
311-
}
302+
dispose(connectionProvider);
303+
dispose(reactiveConnectionProvider);
312304

313305
try {
314306
Duration quietPeriod = clientConfiguration.getShutdownQuietPeriod();
@@ -332,6 +324,20 @@ public void destroy() {
332324
}
333325
}
334326

327+
private void dispose(LettuceConnectionProvider connectionProvider) {
328+
329+
if (connectionProvider instanceof DisposableBean) {
330+
try {
331+
((DisposableBean) connectionProvider).destroy();
332+
} catch (Exception e) {
333+
334+
if (log.isWarnEnabled()) {
335+
log.warn(connectionProvider + " did not shut down gracefully.", e);
336+
}
337+
}
338+
}
339+
}
340+
335341
/*
336342
* (non-Javadoc)
337343
* @see org.springframework.data.redis.connection.RedisConnectionFactory#getConnection()

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.mockito.ArgumentMatchers;
4949

5050
import org.springframework.beans.DirectFieldAccessor;
51+
import org.springframework.beans.factory.DisposableBean;
5152
import org.springframework.data.redis.ConnectionFactoryTracker;
5253
import org.springframework.data.redis.connection.RedisClusterConfiguration;
5354
import org.springframework.data.redis.connection.RedisClusterConnection;
@@ -805,6 +806,25 @@ protected LettuceConnectionProvider doCreateConnectionProvider(AbstractRedisClie
805806
verify(connectionProviderMock, times(2)).getConnection(StatefulConnection.class);
806807
}
807808

809+
@Test // DATAREDIS-1027
810+
public void shouldDisposeConnectionProviders() throws Exception {
811+
812+
LettuceConnectionProvider connectionProviderMock = mock(LettuceConnectionProvider.class,
813+
withSettings().extraInterfaces(DisposableBean.class));
814+
LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory() {
815+
@Override
816+
protected LettuceConnectionProvider doCreateConnectionProvider(AbstractRedisClient client,
817+
RedisCodec<?, ?> codec) {
818+
return connectionProviderMock;
819+
}
820+
};
821+
822+
connectionFactory.afterPropertiesSet();
823+
connectionFactory.destroy();
824+
825+
verify((DisposableBean) connectionProviderMock, times(2)).destroy();
826+
}
827+
808828
@Test // DATAREDIS-842
809829
public void databaseShouldBeSetCorrectlyOnSentinelClient() {
810830

0 commit comments

Comments
 (0)