Skip to content

Commit 201c52c

Browse files
committed
fix: add null checks to avoid NPE
1 parent ca73d2c commit 201c52c

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public Connection connect(
141141
if (!pluginService.acceptsStrategy(hostSpec.getRole(), this.readerSelectorStrategy)) {
142142
throw new UnsupportedOperationException(
143143
Messages.get("ReadWriteSplittingPlugin.unsupportedHostSpecSelectorStrategy",
144-
new Object[] { this.readerSelectorStrategy }));
144+
new Object[] {this.readerSelectorStrategy}));
145145
}
146146

147147
final Connection currentConnection = connectFunc.call();
@@ -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.get())) {
206+
if (this.readerConnection != null && isConnectionUsable(this.readerConnection.get())) {
207207
this.readerConnection.get().clearWarnings();
208208
}
209209
} catch (final SQLException e) {
@@ -313,7 +313,7 @@ void switchConnectionIfRequired(final boolean readOnly) throws SQLException {
313313
} catch (final SQLException e) {
314314
if (!isConnectionUsable(currentConnection)) {
315315
logAndThrowException(
316-
Messages.get("ReadWriteSplittingPlugin.errorSwitchingToReader", new Object[] { e.getMessage() }),
316+
Messages.get("ReadWriteSplittingPlugin.errorSwitchingToReader", new Object[] {e.getMessage()}),
317317
SqlState.CONNECTION_UNABLE_TO_CONNECT,
318318
e);
319319
return;
@@ -380,7 +380,7 @@ private void switchToWriterConnection(
380380
switchCurrentConnectionTo(this.writerConnection, writerHost);
381381
}
382382

383-
if (this.isReaderConnFromInternalPool) {
383+
if (this.isReaderConnFromInternalPool && this.readerConnection != null) {
384384
this.closeConnectionIfIdle(this.readerConnection.get());
385385
}
386386

@@ -412,13 +412,13 @@ private void switchToReaderConnection(final List<HostSpec> hosts)
412412
return;
413413
}
414414

415-
if (this.readerHostSpec != null && !hosts.contains(this.readerHostSpec)) {
415+
if (this.readerConnection != null && this.readerHostSpec != null && !hosts.contains(this.readerHostSpec)) {
416416
// The old reader cannot be used anymore because it is no longer in the list of allowed hosts.
417417
closeConnectionIfIdle(this.readerConnection.get());
418418
}
419419

420420
this.inReadWriteSplit = true;
421-
if (!isConnectionUsable(this.readerConnection.get())) {
421+
if (this.readerConnection == null || !isConnectionUsable(this.readerConnection.get())) {
422422
initializeReaderConnection(hosts);
423423
} else {
424424
try {
@@ -540,7 +540,9 @@ public void releaseResources() {
540540

541541
private void closeIdleConnections() {
542542
LOGGER.finest(() -> Messages.get("ReadWriteSplittingPlugin.closingInternalConnections"));
543-
closeConnectionIfIdle(this.readerConnection.get());
543+
if (this.readerConnection != null) {
544+
closeConnectionIfIdle(this.readerConnection.get());
545+
}
544546
closeConnectionIfIdle(this.writerConnection);
545547
}
546548

@@ -572,6 +574,6 @@ Connection getWriterConnection() {
572574
}
573575

574576
Connection getReaderConnection() {
575-
return this.readerConnection.get();
577+
return this.readerConnection == null ? null : this.readerConnection.get();
576578
}
577579
}

0 commit comments

Comments
 (0)