@@ -203,7 +203,7 @@ public <T, E extends Exception> T execute(
203
203
if (this .writerConnection != null && !this .writerConnection .isClosed ()) {
204
204
this .writerConnection .clearWarnings ();
205
205
}
206
- if (isConnectionUsable (this .readerConnection )) {
206
+ if (isConnectionUsable (this .readerConnection . get () )) {
207
207
this .readerConnection .get ().clearWarnings ();
208
208
}
209
209
} catch (final SQLException e ) {
@@ -274,7 +274,7 @@ private void setWriterConnection(final Connection writerConnection,
274
274
new Object [] {
275
275
writerHostSpec .getUrl ()}));
276
276
}
277
-
277
+
278
278
private void setReaderConnection (final Connection conn , final HostSpec host ) {
279
279
this .readerConnection = new CacheItem <>(conn , this .getKeepAliveTimeout (host ));
280
280
this .readerHostSpec = host ;
@@ -381,19 +381,13 @@ private void switchToWriterConnection(
381
381
}
382
382
383
383
if (this .isReaderConnFromInternalPool ) {
384
- this .closeConnectionIfIdle (this .readerConnection );
384
+ this .closeConnectionIfIdle (this .readerConnection . get () );
385
385
}
386
386
387
387
LOGGER .finer (() -> Messages .get ("ReadWriteSplittingPlugin.switchedFromReaderToWriter" ,
388
388
new Object [] {writerHost .getUrl ()}));
389
389
}
390
390
391
- private void switchCurrentConnectionTo (
392
- final CacheItem <Connection > cachedReaderConnection ,
393
- final HostSpec newConnectionHost ) throws SQLException {
394
- this .switchCurrentConnectionTo (cachedReaderConnection .get (), newConnectionHost );
395
- }
396
-
397
391
private void switchCurrentConnectionTo (
398
392
final Connection newConnection ,
399
393
final HostSpec newConnectionHost )
@@ -420,15 +414,15 @@ private void switchToReaderConnection(final List<HostSpec> hosts)
420
414
421
415
if (this .readerHostSpec != null && !hosts .contains (this .readerHostSpec )) {
422
416
// The old reader cannot be used anymore because it is no longer in the list of allowed hosts.
423
- closeConnectionIfIdle (this .readerConnection );
417
+ closeConnectionIfIdle (this .readerConnection . get () );
424
418
}
425
419
426
420
this .inReadWriteSplit = true ;
427
- if (!isConnectionUsable (this .readerConnection )) {
421
+ if (!isConnectionUsable (this .readerConnection . get () )) {
428
422
initializeReaderConnection (hosts );
429
423
} else {
430
424
try {
431
- switchCurrentConnectionTo (this .readerConnection , this .readerHostSpec );
425
+ switchCurrentConnectionTo (this .readerConnection . get () , this .readerHostSpec );
432
426
LOGGER .finer (() -> Messages .get ("ReadWriteSplittingPlugin.switchedFromWriterToReader" ,
433
427
new Object [] {this .readerHostSpec .getUrl ()}));
434
428
} catch (SQLException e ) {
@@ -442,8 +436,12 @@ private void switchToReaderConnection(final List<HostSpec> hosts)
442
436
}
443
437
444
438
Connection conn = this .readerConnection .get (true );
445
- if (conn != null ) {
446
- conn .close ();
439
+ if (isConnectionUsable (conn )) {
440
+ try {
441
+ conn .close ();
442
+ } catch (SQLException ex ) {
443
+ // Do nothing
444
+ }
447
445
}
448
446
this .readerConnection = null ;
449
447
this .readerHostSpec = null ;
@@ -519,11 +517,7 @@ private void getNewReaderConnection() throws SQLException {
519
517
() -> Messages .get ("ReadWriteSplittingPlugin.successfullyConnectedToReader" ,
520
518
new Object [] {finalReaderHost .getUrl ()}));
521
519
setReaderConnection (conn , readerHost );
522
- switchCurrentConnectionTo (this .readerConnection , this .readerHostSpec );
523
- }
524
-
525
- private boolean isConnectionUsable (final CacheItem <Connection > cachedConnection ) throws SQLException {
526
- return cachedConnection != null && !cachedConnection .isExpired () && isConnectionUsable (cachedConnection .get ());
520
+ switchCurrentConnectionTo (this .readerConnection .get (), this .readerHostSpec );
527
521
}
528
522
529
523
private boolean isConnectionUsable (final Connection connection ) throws SQLException {
@@ -535,7 +529,8 @@ private long getKeepAliveTimeout(final HostSpec host) {
535
529
// Let the connection pool handle the lifetime of the reader connection.
536
530
return 0 ;
537
531
}
538
- return System .nanoTime () + TimeUnit .MILLISECONDS .toNanos (CACHED_READER_KEEP_ALIVE_TIMEOUT .getLong (properties ));
532
+ final long keepAliveMs = CACHED_READER_KEEP_ALIVE_TIMEOUT .getLong (properties );
533
+ return keepAliveMs > 0 ? System .nanoTime () + TimeUnit .MILLISECONDS .toNanos (keepAliveMs ) : 0 ;
539
534
}
540
535
541
536
@ Override
@@ -545,14 +540,10 @@ public void releaseResources() {
545
540
546
541
private void closeIdleConnections () {
547
542
LOGGER .finest (() -> Messages .get ("ReadWriteSplittingPlugin.closingInternalConnections" ));
548
- closeConnectionIfIdle (this .readerConnection );
543
+ closeConnectionIfIdle (this .readerConnection . get () );
549
544
closeConnectionIfIdle (this .writerConnection );
550
545
}
551
546
552
- void closeConnectionIfIdle (final CacheItem <Connection > cachedConnection ) {
553
- closeConnectionIfIdle (cachedConnection .get ());
554
- }
555
-
556
547
void closeConnectionIfIdle (final Connection internalConnection ) {
557
548
final Connection currentConnection = this .pluginService .getCurrentConnection ();
558
549
try {
0 commit comments