Skip to content

Commit ca73d2c

Browse files
committed
chore: reduce overloaded methods
1 parent 63e0d3c commit ca73d2c

File tree

2 files changed

+17
-26
lines changed

2 files changed

+17
-26
lines changed

wrapper/src/main/java/software/amazon/jdbc/plugin/readwritesplitting/ReadWriteSplittingPlugin.java

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public <T, E extends Exception> T execute(
203203
if (this.writerConnection != null && !this.writerConnection.isClosed()) {
204204
this.writerConnection.clearWarnings();
205205
}
206-
if (isConnectionUsable(this.readerConnection)) {
206+
if (isConnectionUsable(this.readerConnection.get())) {
207207
this.readerConnection.get().clearWarnings();
208208
}
209209
} catch (final SQLException e) {
@@ -274,7 +274,7 @@ private void setWriterConnection(final Connection writerConnection,
274274
new Object[] {
275275
writerHostSpec.getUrl()}));
276276
}
277-
277+
278278
private void setReaderConnection(final Connection conn, final HostSpec host) {
279279
this.readerConnection = new CacheItem<>(conn, this.getKeepAliveTimeout(host));
280280
this.readerHostSpec = host;
@@ -381,19 +381,13 @@ private void switchToWriterConnection(
381381
}
382382

383383
if (this.isReaderConnFromInternalPool) {
384-
this.closeConnectionIfIdle(this.readerConnection);
384+
this.closeConnectionIfIdle(this.readerConnection.get());
385385
}
386386

387387
LOGGER.finer(() -> Messages.get("ReadWriteSplittingPlugin.switchedFromReaderToWriter",
388388
new Object[] {writerHost.getUrl()}));
389389
}
390390

391-
private void switchCurrentConnectionTo(
392-
final CacheItem<Connection> cachedReaderConnection,
393-
final HostSpec newConnectionHost) throws SQLException {
394-
this.switchCurrentConnectionTo(cachedReaderConnection.get(), newConnectionHost);
395-
}
396-
397391
private void switchCurrentConnectionTo(
398392
final Connection newConnection,
399393
final HostSpec newConnectionHost)
@@ -420,15 +414,15 @@ private void switchToReaderConnection(final List<HostSpec> hosts)
420414

421415
if (this.readerHostSpec != null && !hosts.contains(this.readerHostSpec)) {
422416
// 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());
424418
}
425419

426420
this.inReadWriteSplit = true;
427-
if (!isConnectionUsable(this.readerConnection)) {
421+
if (!isConnectionUsable(this.readerConnection.get())) {
428422
initializeReaderConnection(hosts);
429423
} else {
430424
try {
431-
switchCurrentConnectionTo(this.readerConnection, this.readerHostSpec);
425+
switchCurrentConnectionTo(this.readerConnection.get(), this.readerHostSpec);
432426
LOGGER.finer(() -> Messages.get("ReadWriteSplittingPlugin.switchedFromWriterToReader",
433427
new Object[] {this.readerHostSpec.getUrl()}));
434428
} catch (SQLException e) {
@@ -442,8 +436,12 @@ private void switchToReaderConnection(final List<HostSpec> hosts)
442436
}
443437

444438
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+
}
447445
}
448446
this.readerConnection = null;
449447
this.readerHostSpec = null;
@@ -519,11 +517,7 @@ private void getNewReaderConnection() throws SQLException {
519517
() -> Messages.get("ReadWriteSplittingPlugin.successfullyConnectedToReader",
520518
new Object[] {finalReaderHost.getUrl()}));
521519
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);
527521
}
528522

529523
private boolean isConnectionUsable(final Connection connection) throws SQLException {
@@ -535,7 +529,8 @@ private long getKeepAliveTimeout(final HostSpec host) {
535529
// Let the connection pool handle the lifetime of the reader connection.
536530
return 0;
537531
}
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;
539534
}
540535

541536
@Override
@@ -545,14 +540,10 @@ public void releaseResources() {
545540

546541
private void closeIdleConnections() {
547542
LOGGER.finest(() -> Messages.get("ReadWriteSplittingPlugin.closingInternalConnections"));
548-
closeConnectionIfIdle(this.readerConnection);
543+
closeConnectionIfIdle(this.readerConnection.get());
549544
closeConnectionIfIdle(this.writerConnection);
550545
}
551546

552-
void closeConnectionIfIdle(final CacheItem<Connection> cachedConnection) {
553-
closeConnectionIfIdle(cachedConnection.get());
554-
}
555-
556547
void closeConnectionIfIdle(final Connection internalConnection) {
557548
final Connection currentConnection = this.pluginService.getCurrentConnection();
558549
try {

wrapper/src/main/java/software/amazon/jdbc/util/CacheItem.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public V get() {
4141
}
4242

4343
public V get(final boolean returnExpired) {
44-
return this.isExpired() && !returnExpired ? null : item;
44+
return (this.isExpired() && !returnExpired) ? null : item;
4545
}
4646

4747
@Override

0 commit comments

Comments
 (0)