From f34a7c2af4b3bee26fa7423e0bb3b7b6d3a8596a Mon Sep 17 00:00:00 2001 From: sebastianh6r <1493402+sebastianh6r@users.noreply.github.com> Date: Mon, 27 May 2024 10:20:19 +0200 Subject: [PATCH] Optimize settings for Hibernate ORM * Optimize settings for Hibernate ORM * Teach exception handler about the new BatchUpdateException exceptions Closes #26162 Signed-off-by: Sebastian Hoeninger Signed-off-by: Alexander Schwartz Co-authored-by: Sebastian Hoeninger Co-authored-by: Alexander Schwartz --- .../jpa/PersistenceExceptionConverter.java | 9 ++++-- .../main/resources/default-persistence.xml | 28 +++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/model/jpa/src/main/java/org/keycloak/connections/jpa/PersistenceExceptionConverter.java b/model/jpa/src/main/java/org/keycloak/connections/jpa/PersistenceExceptionConverter.java index ffca7f90fa..a6cca6225e 100644 --- a/model/jpa/src/main/java/org/keycloak/connections/jpa/PersistenceExceptionConverter.java +++ b/model/jpa/src/main/java/org/keycloak/connections/jpa/PersistenceExceptionConverter.java @@ -32,6 +32,7 @@ import java.lang.reflect.InvocationHandler; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Proxy; +import java.sql.SQLException; import java.sql.SQLIntegrityConstraintViolationException; import java.util.function.Predicate; import java.util.regex.Pattern; @@ -89,8 +90,12 @@ public class PersistenceExceptionConverter implements InvocationHandler { Predicate throwModelDuplicateEx = throwable -> throwable instanceof EntityExistsException - || throwable instanceof ConstraintViolationException - || throwable instanceof SQLIntegrityConstraintViolationException; + || throwable instanceof ConstraintViolationException + // SQL state class 23 captures errors like 23505 = UNIQUE VIOLATION et al. + // This captures, for example, a BatchUpdateException which is not mapped to the other exception types + // https://en.wikipedia.org/wiki/SQLSTATE + || (throwable instanceof SQLException bue && bue.getSQLState().startsWith("23")) + || throwable instanceof SQLIntegrityConstraintViolationException; throwModelDuplicateEx = throwModelDuplicateEx.or(checkDuplicationMessage); diff --git a/model/jpa/src/main/resources/default-persistence.xml b/model/jpa/src/main/resources/default-persistence.xml index 50777c8940..8cc13a02dc 100644 --- a/model/jpa/src/main/resources/default-persistence.xml +++ b/model/jpa/src/main/resources/default-persistence.xml @@ -92,6 +92,34 @@ + + + + + + + + + + + + + + + +