Skip to content

Commit ae00ea3

Browse files
committed
code review
1 parent 1072e8e commit ae00ea3

File tree

5 files changed

+34
-2
lines changed

5 files changed

+34
-2
lines changed

wrapper/src/main/java/software/amazon/jdbc/exceptions/AbstractPgExceptionHandler.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
import software.amazon.jdbc.util.StringUtils;
2424

2525
public abstract class AbstractPgExceptionHandler implements ExceptionHandler {
26+
27+
protected static final String READ_ONLY_CONNECTION_SQLSTATE = "25006";
28+
2629
public abstract List<String> getNetworkErrors();
2730

2831
public abstract List<String> getAccessErrors();
@@ -99,7 +102,7 @@ public boolean isLoginException(final String sqlState) {
99102
@Override
100103
public boolean isReadOnlyConnectionException(
101104
final @Nullable String sqlState, final @Nullable Integer errorCode) {
102-
return "25006".equals(sqlState);
105+
return READ_ONLY_CONNECTION_SQLSTATE.equals(sqlState);
103106
}
104107

105108
@Override

wrapper/src/main/java/software/amazon/jdbc/exceptions/MySQLExceptionHandler.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
package software.amazon.jdbc.exceptions;
1818

1919
import java.sql.SQLException;
20+
import java.util.Arrays;
21+
import java.util.HashSet;
22+
import java.util.Set;
2023
import org.checkerframework.checker.nullness.qual.Nullable;
2124
import software.amazon.jdbc.targetdriverdialect.TargetDriverDialect;
2225
import software.amazon.jdbc.util.StringUtils;
@@ -27,6 +30,11 @@ public class MySQLExceptionHandler implements ExceptionHandler {
2730
public static final String SET_NETWORK_TIMEOUT_ON_CLOSED_CONNECTION =
2831
"setNetworkTimeout cannot be called on a closed connection";
2932

33+
private static final Set<Integer> SQLSTATE_READ_ONLY_CONNECTION = new HashSet<>(Arrays.asList(
34+
1290, // The MySQL server is running with the --read-only option, so it cannot execute this statement
35+
1836 // Running in read-only mode
36+
));
37+
3038
@Override
3139
public boolean isNetworkException(final Throwable throwable, @Nullable TargetDriverDialect targetDriverDialect) {
3240
Throwable exception = throwable;
@@ -106,7 +114,8 @@ public boolean isLoginException(final String sqlState) {
106114
@Override
107115
public boolean isReadOnlyConnectionException(
108116
final @Nullable String sqlState, final @Nullable Integer errorCode) {
109-
return "HY000".equals(sqlState) && errorCode != null && (errorCode == 1290 || errorCode == 1836);
117+
// HY000 - generic SQL state; use error code for more specific information
118+
return "HY000".equals(sqlState) && errorCode != null && (SQLSTATE_READ_ONLY_CONNECTION.contains(errorCode));
110119
}
111120

112121
@Override

wrapper/src/main/java/software/amazon/jdbc/plugin/failover/FailoverConnectionPlugin.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,8 @@ protected boolean shouldExceptionTriggerConnectionSwitch(final Throwable t) {
894894
return true;
895895
}
896896

897+
// For STRICT_WRITER failover mode when connection exception indicate that the connection's in read-only mode,
898+
// initiate a failover by returning true.
897899
return this.failoverMode == FailoverMode.STRICT_WRITER
898900
&& this.pluginService.isReadOnlyConnectionException(t, this.pluginService.getTargetDriverDialect());
899901
}

wrapper/src/main/java/software/amazon/jdbc/plugin/failover2/FailoverConnectionPlugin.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,8 @@ protected boolean shouldExceptionTriggerConnectionSwitch(final Throwable t) {
702702
return true;
703703
}
704704

705+
// For STRICT_WRITER failover mode when connection exception indicate that the connection's in read-only mode,
706+
// initiate a failover by returning true.
705707
return this.failoverMode == FailoverMode.STRICT_WRITER
706708
&& this.pluginService.isReadOnlyConnectionException(t, this.pluginService.getTargetDriverDialect());
707709
}

wrapper/src/test/java/software/amazon/jdbc/plugin/failover/FailoverConnectionPluginTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@
1818

1919
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
2020
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertFalse;
2122
import static org.junit.jupiter.api.Assertions.assertThrows;
23+
import static org.junit.jupiter.api.Assertions.assertTrue;
2224
import static org.mockito.ArgumentMatchers.any;
2325
import static org.mockito.ArgumentMatchers.anyString;
2426
import static org.mockito.ArgumentMatchers.eq;
2527
import static org.mockito.Mockito.atLeastOnce;
2628
import static org.mockito.Mockito.doNothing;
29+
import static org.mockito.Mockito.doReturn;
2730
import static org.mockito.Mockito.doThrow;
2831
import static org.mockito.Mockito.never;
2932
import static org.mockito.Mockito.spy;
@@ -441,4 +444,17 @@ private void initializePlugin() throws SQLException {
441444
spyPlugin.setReaderFailoverHandler(mockReaderFailoverHandler);
442445
// doReturn(mockConnectionService).when(spyPlugin).getConnectionService();
443446
}
447+
448+
@Test
449+
void test_failover_when_read_only_connection() throws SQLException {
450+
initializePlugin();
451+
spyPlugin.failoverMode = FailoverMode.STRICT_WRITER;
452+
453+
when(mockPluginService.isReadOnlyConnectionException(any(), any(TargetDriverDialect.class))).thenReturn(true);
454+
assertTrue(spyPlugin.shouldExceptionTriggerConnectionSwitch(new SQLException("test", "any")));
455+
456+
when(mockPluginService.isReadOnlyConnectionException(any(), any(TargetDriverDialect.class))).thenReturn(false);
457+
assertFalse(spyPlugin.shouldExceptionTriggerConnectionSwitch(new SQLException("test", "any")));
458+
}
459+
444460
}

0 commit comments

Comments
 (0)