Skip to content

Commit 48847dd

Browse files
fix: handle nested auth exceptions in MySQLExceptionHandler (#1601)
1 parent 8996829 commit 48847dd

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public boolean isLoginException(final Throwable throwable, @Nullable TargetDrive
8787
if (exception instanceof SQLException) {
8888
sqlState = ((SQLException) exception).getSQLState();
8989
} else if (targetDriverDialect != null) {
90-
sqlState = targetDriverDialect.getSQLState(throwable);
90+
sqlState = targetDriverDialect.getSQLState(exception);
9191
}
9292

9393
if (isLoginException(sqlState)) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public boolean isLoginException(final Throwable throwable, @Nullable TargetDrive
9393
if (exception instanceof SQLException) {
9494
sqlState = ((SQLException) exception).getSQLState();
9595
} else if (targetDriverDialect != null) {
96-
sqlState = targetDriverDialect.getSQLState(throwable);
96+
sqlState = targetDriverDialect.getSQLState(exception);
9797
}
9898

9999
if (isLoginException(sqlState)) {
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package software.amazon.jdbc.exceptions;
18+
19+
import static org.junit.jupiter.api.Assertions.assertTrue;
20+
21+
import com.mysql.cj.exceptions.CJException;
22+
import java.sql.SQLNonTransientConnectionException;
23+
import org.junit.jupiter.api.Test;
24+
import software.amazon.jdbc.targetdriverdialect.MysqlConnectorJTargetDriverDialect;
25+
26+
public class MySQLExceptionHandlerTest {
27+
28+
@Test
29+
public void testIsLoginException_NestedAuthenticationException() {
30+
31+
MySQLExceptionHandler handler = new MySQLExceptionHandler();
32+
33+
// Create the nested exception structure as described in requirements
34+
// Inner cause: Authentication exception with SQL state "28000"
35+
CJException authException = new CJException(
36+
"Access denied for user 'db_user'@'172.18.0.1' (using password: YES)"
37+
);
38+
authException.setSQLState("28000");
39+
40+
// Outer exception: Connection exception with SQL state "08001"
41+
SQLNonTransientConnectionException connectionException = new SQLNonTransientConnectionException(
42+
"Could not create connection to database server. Attempted reconnect 3 times. Giving up.",
43+
"08001",
44+
authException
45+
);
46+
47+
assertTrue(handler.isLoginException(connectionException, new MysqlConnectorJTargetDriverDialect()),
48+
"Should detect nested authentication exception with dialect");
49+
}
50+
}

0 commit comments

Comments
 (0)