@@ -141,7 +141,7 @@ public Connection connect(
141
141
if (!pluginService .acceptsStrategy (hostSpec .getRole (), this .readerSelectorStrategy )) {
142
142
throw new UnsupportedOperationException (
143
143
Messages .get ("ReadWriteSplittingPlugin.unsupportedHostSpecSelectorStrategy" ,
144
- new Object [] { this .readerSelectorStrategy }));
144
+ new Object [] {this .readerSelectorStrategy }));
145
145
}
146
146
147
147
final Connection currentConnection = connectFunc .call ();
@@ -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 .get ())) {
206
+ if (this . readerConnection != null && isConnectionUsable (this .readerConnection .get ())) {
207
207
this .readerConnection .get ().clearWarnings ();
208
208
}
209
209
} catch (final SQLException e ) {
@@ -313,7 +313,7 @@ void switchConnectionIfRequired(final boolean readOnly) throws SQLException {
313
313
} catch (final SQLException e ) {
314
314
if (!isConnectionUsable (currentConnection )) {
315
315
logAndThrowException (
316
- Messages .get ("ReadWriteSplittingPlugin.errorSwitchingToReader" , new Object [] { e .getMessage () }),
316
+ Messages .get ("ReadWriteSplittingPlugin.errorSwitchingToReader" , new Object [] {e .getMessage ()}),
317
317
SqlState .CONNECTION_UNABLE_TO_CONNECT ,
318
318
e );
319
319
return ;
@@ -380,7 +380,7 @@ private void switchToWriterConnection(
380
380
switchCurrentConnectionTo (this .writerConnection , writerHost );
381
381
}
382
382
383
- if (this .isReaderConnFromInternalPool ) {
383
+ if (this .isReaderConnFromInternalPool && this . readerConnection != null ) {
384
384
this .closeConnectionIfIdle (this .readerConnection .get ());
385
385
}
386
386
@@ -412,13 +412,13 @@ private void switchToReaderConnection(final List<HostSpec> hosts)
412
412
return ;
413
413
}
414
414
415
- if (this .readerHostSpec != null && !hosts .contains (this .readerHostSpec )) {
415
+ if (this .readerConnection != null && this . readerHostSpec != null && !hosts .contains (this .readerHostSpec )) {
416
416
// The old reader cannot be used anymore because it is no longer in the list of allowed hosts.
417
417
closeConnectionIfIdle (this .readerConnection .get ());
418
418
}
419
419
420
420
this .inReadWriteSplit = true ;
421
- if (!isConnectionUsable (this .readerConnection .get ())) {
421
+ if (this . readerConnection == null || !isConnectionUsable (this .readerConnection .get ())) {
422
422
initializeReaderConnection (hosts );
423
423
} else {
424
424
try {
@@ -540,7 +540,9 @@ public void releaseResources() {
540
540
541
541
private void closeIdleConnections () {
542
542
LOGGER .finest (() -> Messages .get ("ReadWriteSplittingPlugin.closingInternalConnections" ));
543
- closeConnectionIfIdle (this .readerConnection .get ());
543
+ if (this .readerConnection != null ) {
544
+ closeConnectionIfIdle (this .readerConnection .get ());
545
+ }
544
546
closeConnectionIfIdle (this .writerConnection );
545
547
}
546
548
@@ -572,6 +574,6 @@ Connection getWriterConnection() {
572
574
}
573
575
574
576
Connection getReaderConnection () {
575
- return this .readerConnection .get ();
577
+ return this .readerConnection == null ? null : this . readerConnection .get ();
576
578
}
577
579
}
0 commit comments