@@ -276,6 +276,7 @@ private void setWriterConnection(final Connection writerConnection,
276276 }
277277
278278 private void setReaderConnection (final Connection conn , final HostSpec host ) {
279+ closeReaderConnectionIfIdle (this .readerConnection );
279280 this .readerConnection = new CacheItem <>(conn , this .getKeepAliveTimeout (host ));
280281 this .readerHostSpec = host ;
281282 LOGGER .finest (
@@ -380,8 +381,8 @@ private void switchToWriterConnection(
380381 switchCurrentConnectionTo (this .writerConnection , writerHost );
381382 }
382383
383- if (this .isReaderConnFromInternalPool && this . readerConnection != null ) {
384- this .closeConnectionIfIdle (this .readerConnection . get () );
384+ if (this .isReaderConnFromInternalPool ) {
385+ this .closeReaderConnectionIfIdle (this .readerConnection );
385386 }
386387
387388 LOGGER .finer (() -> Messages .get ("ReadWriteSplittingPlugin.switchedFromReaderToWriter" ,
@@ -412,9 +413,9 @@ private void switchToReaderConnection(final List<HostSpec> hosts)
412413 return ;
413414 }
414415
415- if (this .readerConnection != null && this . readerHostSpec != null && !hosts .contains (this .readerHostSpec )) {
416+ if (this .readerHostSpec != null && !hosts .contains (this .readerHostSpec )) {
416417 // The old reader cannot be used anymore because it is no longer in the list of allowed hosts.
417- closeConnectionIfIdle (this .readerConnection . get () );
418+ closeReaderConnectionIfIdle (this .readerConnection );
418419 }
419420
420421 this .inReadWriteSplit = true ;
@@ -435,22 +436,13 @@ private void switchToReaderConnection(final List<HostSpec> hosts)
435436 new Object [] {this .readerHostSpec .getUrl ()}));
436437 }
437438
438- Connection conn = this .readerConnection .get (true );
439- if (isConnectionUsable (conn )) {
440- try {
441- conn .close ();
442- } catch (SQLException ex ) {
443- // Do nothing
444- }
445- }
446- this .readerConnection = null ;
447- this .readerHostSpec = null ;
439+ closeReaderConnectionIfIdle (this .readerConnection );
448440 initializeReaderConnection (hosts );
449441 }
450442 }
451443
452444 if (this .isWriterConnFromInternalPool ) {
453- this .closeConnectionIfIdle (this .writerConnection );
445+ this .closeWriterConnectionIfIdle (this .writerConnection );
454446 }
455447 }
456448
@@ -540,26 +532,37 @@ public void releaseResources() {
540532
541533 private void closeIdleConnections () {
542534 LOGGER .finest (() -> Messages .get ("ReadWriteSplittingPlugin.closingInternalConnections" ));
543- if (this .readerConnection != null ) {
544- closeConnectionIfIdle (this .readerConnection .get ());
535+ closeReaderConnectionIfIdle (this .readerConnection );
536+ closeWriterConnectionIfIdle (this .writerConnection );
537+ }
538+
539+ void closeReaderConnectionIfIdle (CacheItem <Connection > readerConnection ) {
540+ if (readerConnection == null ) {
541+ return ;
545542 }
546- closeConnectionIfIdle (this .writerConnection );
543+
544+ final Connection currentConnection = this .pluginService .getCurrentConnection ();
545+ final Connection readerConnectionCache = readerConnection .get (true );
546+
547+ try {
548+ if (isConnectionUsable (readerConnectionCache ) && readerConnectionCache != currentConnection ) {
549+ readerConnectionCache .close ();
550+ }
551+ } catch (SQLException e ) {
552+ // Do nothing.
553+ }
554+
555+ this .readerConnection = null ;
556+ this .readerHostSpec = null ;
547557 }
548558
549- void closeConnectionIfIdle (final Connection internalConnection ) {
559+ void closeWriterConnectionIfIdle (final Connection internalConnection ) {
550560 final Connection currentConnection = this .pluginService .getCurrentConnection ();
551561 try {
552562 if (isConnectionUsable (internalConnection )
553563 && internalConnection != currentConnection ) {
554564 internalConnection .close ();
555- if (internalConnection == writerConnection ) {
556- writerConnection = null ;
557- }
558-
559- if (internalConnection == readerConnection .get ()) {
560- readerConnection = null ;
561- readerHostSpec = null ;
562- }
565+ writerConnection = null ;
563566 }
564567 } catch (final SQLException e ) {
565568 // ignore
0 commit comments