62
62
import org .springframework .dao .QueryTimeoutException ;
63
63
import org .springframework .data .redis .ExceptionTranslationStrategy ;
64
64
import org .springframework .data .redis .FallbackExceptionTranslationStrategy ;
65
+ import org .springframework .data .redis .RedisSystemException ;
65
66
import org .springframework .data .redis .connection .*;
66
67
import org .springframework .data .redis .connection .convert .TransactionResultConverter ;
67
68
import org .springframework .data .redis .connection .lettuce .LettuceConnectionProvider .TargetAware ;
@@ -351,22 +352,29 @@ public void close() throws DataAccessException {
351
352
352
353
isClosed = true ;
353
354
355
+ reset ();
356
+ }
357
+
358
+ private void reset () {
359
+
354
360
if (asyncDedicatedConn != null ) {
355
361
try {
356
362
if (customizedDatabaseIndex ()) {
357
363
potentiallySelectDatabase (defaultDbIndex );
358
364
}
359
365
connectionProvider .release (asyncDedicatedConn );
366
+ asyncDedicatedConn = null ;
360
367
} catch (RuntimeException ex ) {
361
368
throw convertLettuceAccessException (ex );
362
369
}
363
370
}
364
371
372
+ LettuceSubscription subscription = this .subscription ;
365
373
if (subscription != null ) {
366
374
if (subscription .isAlive ()) {
367
375
subscription .doClose ();
368
376
}
369
- subscription = null ;
377
+ this . subscription = null ;
370
378
}
371
379
372
380
this .dbIndex = defaultDbIndex ;
@@ -381,7 +389,8 @@ public boolean isClosed() {
381
389
public RedisClusterAsyncCommands <byte [], byte []> getNativeConnection () {
382
390
383
391
LettuceSubscription subscription = this .subscription ;
384
- return (subscription != null ? subscription .getNativeConnection ().async () : getAsyncConnection ());
392
+ return (subscription != null && subscription .isAlive () ? subscription .getNativeConnection ().async ()
393
+ : getAsyncConnection ());
385
394
}
386
395
387
396
@ Override
@@ -509,8 +518,8 @@ public List<Object> exec() {
509
518
LettuceTransactionResultConverter resultConverter = new LettuceTransactionResultConverter (
510
519
new LinkedList <>(txResults ), exceptionConverter );
511
520
512
- pipeline (newLettuceResult (exec , source -> resultConverter
513
- .convert (LettuceConverters .transactionResultUnwrapper ().convert (source ))));
521
+ pipeline (newLettuceResult (exec ,
522
+ source -> resultConverter .convert (LettuceConverters .transactionResultUnwrapper ().convert (source ))));
514
523
return null ;
515
524
}
516
525
@@ -695,7 +704,8 @@ public void setPipeliningFlushPolicy(PipeliningFlushPolicy pipeliningFlushPolicy
695
704
@ SuppressWarnings ("unchecked" )
696
705
protected StatefulRedisPubSubConnection <byte [], byte []> switchToPubSub () {
697
706
698
- close ();
707
+ checkSubscription ();
708
+ reset ();
699
709
return connectionProvider .getConnection (StatefulRedisPubSubConnection .class );
700
710
}
701
711
@@ -870,6 +880,10 @@ RedisClusterCommands<byte[], byte[]> getDedicatedConnection() {
870
880
871
881
protected RedisClusterAsyncCommands <byte [], byte []> getAsyncDedicatedConnection () {
872
882
883
+ if (isClosed ()) {
884
+ throw new RedisSystemException ("Connection is closed" , null );
885
+ }
886
+
873
887
StatefulConnection <byte [], byte []> connection = getOrCreateDedicatedConnection ();
874
888
875
889
if (connection instanceof StatefulRedisConnection ) {
0 commit comments