Skip to content

Commit eb990bc

Browse files
authored
PersistenceExceptionConverter NPE
make sure SQLException#getSQLState is not null before attempting to call methods on it. I have observed exceptions during AWS MySQL RDS failovers where sql state is null, causing this method to throw an NPE exception rather than the expected ModelException closes keycloak#38467 Signed-off-by: Robert Hollencamp <[email protected]>
1 parent a7e6383 commit eb990bc

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

model/jpa/src/main/java/org/keycloak/connections/jpa/PersistenceExceptionConverter.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,7 @@ public static ModelException convert(Throwable t) {
9191
Predicate<Throwable> throwModelDuplicateEx = throwable ->
9292
throwable instanceof EntityExistsException
9393
|| throwable instanceof ConstraintViolationException
94-
// SQL state class 23 captures errors like 23505 = UNIQUE VIOLATION et al.
95-
// This captures, for example, a BatchUpdateException which is not mapped to the other exception types
96-
// https://en.wikipedia.org/wiki/SQLSTATE
97-
|| (throwable instanceof SQLException bue && bue.getSQLState().startsWith("23"))
94+
|| isSqlStateClass23(throwable)
9895
|| throwable instanceof SQLIntegrityConstraintViolationException;
9996

10097
throwModelDuplicateEx = throwModelDuplicateEx.or(checkDuplicationMessage);
@@ -110,4 +107,15 @@ public static ModelException convert(Throwable t) {
110107
}
111108
}
112109

110+
/**
111+
* SQL state class 23 captures errors like 23505 = UNIQUE VIOLATION et al.
112+
* This captures, for example, a BatchUpdateException which is not mapped to the other exception types
113+
* https://en.wikipedia.org/wiki/SQLSTATE
114+
*/
115+
private static boolean isSqlStateClass23(Throwable t) {
116+
return t instanceof SQLException bue
117+
&& bue.getSQLState() != null
118+
&& bue.getSQLState().startsWith("23");
119+
}
120+
113121
}

0 commit comments

Comments
 (0)