Skip to content

Commit a704358

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 104dc8c commit a704358

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

+16-10
Original file line numberDiff line numberDiff line change
@@ -291,16 +291,8 @@ public void destroy() {
291291

292292
resetConnection();
293293

294-
if (connectionProvider instanceof DisposableBean) {
295-
try {
296-
((DisposableBean) connectionProvider).destroy();
297-
} catch (Exception e) {
298-
299-
if (log.isWarnEnabled()) {
300-
log.warn(connectionProvider + " did not shut down gracefully.", e);
301-
}
302-
}
303-
}
294+
dispose(connectionProvider);
295+
dispose(reactiveConnectionProvider);
304296

305297
try {
306298
Duration timeout = clientConfiguration.getShutdownTimeout();
@@ -323,6 +315,20 @@ public void destroy() {
323315
}
324316
}
325317

318+
private void dispose(LettuceConnectionProvider connectionProvider) {
319+
320+
if (connectionProvider instanceof DisposableBean) {
321+
try {
322+
((DisposableBean) connectionProvider).destroy();
323+
} catch (Exception e) {
324+
325+
if (log.isWarnEnabled()) {
326+
log.warn(connectionProvider + " did not shut down gracefully.", e);
327+
}
328+
}
329+
}
330+
}
331+
326332
/*
327333
* (non-Javadoc)
328334
* @see org.springframework.data.redis.connection.RedisConnectionFactory#getConnection()

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

+20
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.junit.Test;
4646
import org.mockito.ArgumentMatchers;
4747
import org.springframework.beans.DirectFieldAccessor;
48+
import org.springframework.beans.factory.DisposableBean;
4849
import org.springframework.data.redis.ConnectionFactoryTracker;
4950
import org.springframework.data.redis.connection.RedisClusterConfiguration;
5051
import org.springframework.data.redis.connection.RedisClusterConnection;
@@ -712,6 +713,25 @@ protected AbstractRedisClient createClient() {
712713
verify(connectionMock).close();
713714
}
714715

716+
@Test // DATAREDIS-1027
717+
public void shouldDisposeConnectionProviders() throws Exception {
718+
719+
LettuceConnectionProvider connectionProviderMock = mock(LettuceConnectionProvider.class,
720+
withSettings().extraInterfaces(DisposableBean.class));
721+
LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory() {
722+
@Override
723+
protected LettuceConnectionProvider doCreateConnectionProvider(AbstractRedisClient client,
724+
RedisCodec<?, ?> codec) {
725+
return connectionProviderMock;
726+
}
727+
};
728+
729+
connectionFactory.afterPropertiesSet();
730+
connectionFactory.destroy();
731+
732+
verify((DisposableBean) connectionProviderMock, times(2)).destroy();
733+
}
734+
715735
@Test // DATAREDIS-842
716736
public void databaseShouldBeSetCorrectlyOnSentinelClient() {
717737

0 commit comments

Comments
 (0)