From 8ed1906f43cb02ea6a380a50027a47d756f8819b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Mon, 10 Oct 2022 11:50:50 +0200 Subject: [PATCH] Refine SQLErrorCodesFactory reachability on native images SQLErrorCodeSQLExceptionTranslator#USER_PROVIDED_ERROR_CODES_FILE_PRESENT evaluation at build time combined with the lazy SQLErrorCodesFactory#instance initialization allow to avoid making SQLErrorCodesFactory constructor reachable when no custom sql-error-codes.xml is provided. Closes gh-29294 --- .../jdbc/support/SQLErrorCodeSQLExceptionTranslator.java | 6 ++++-- .../jdbc/support/SQLErrorCodesFactory.java | 8 +++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/support/SQLErrorCodeSQLExceptionTranslator.java b/spring-jdbc/src/main/java/org/springframework/jdbc/support/SQLErrorCodeSQLExceptionTranslator.java index 609f16325f..02943bdebf 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/support/SQLErrorCodeSQLExceptionTranslator.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/support/SQLErrorCodeSQLExceptionTranslator.java @@ -77,6 +77,9 @@ public class SQLErrorCodeSQLExceptionTranslator extends AbstractFallbackSQLExcep private static final int MESSAGE_SQL_THROWABLE_CONSTRUCTOR = 4; private static final int MESSAGE_SQL_SQLEX_CONSTRUCTOR = 5; + private static final boolean USER_PROVIDED_ERROR_CODES_FILE_PRESENT = + new ClassPathResource(SQLErrorCodesFactory.SQL_ERROR_CODE_OVERRIDE_PATH, SQLErrorCodesFactory.class.getClassLoader()).exists(); + /** Error codes used by this translator. */ @Nullable @@ -424,8 +427,7 @@ public class SQLErrorCodeSQLExceptionTranslator extends AbstractFallbackSQLExcep * in the root of the classpath. */ static boolean hasUserProvidedErrorCodesFile() { - return new ClassPathResource(SQLErrorCodesFactory.SQL_ERROR_CODE_OVERRIDE_PATH, - SQLErrorCodesFactory.class.getClassLoader()).exists(); + return USER_PROVIDED_ERROR_CODES_FILE_PRESENT; } } diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/support/SQLErrorCodesFactory.java b/spring-jdbc/src/main/java/org/springframework/jdbc/support/SQLErrorCodesFactory.java index 45452f0bcd..12e8ba6cc3 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/support/SQLErrorCodesFactory.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/support/SQLErrorCodesFactory.java @@ -68,14 +68,20 @@ public class SQLErrorCodesFactory { /** * Keep track of a single instance so we can return it to classes that request it. + * Lazily initialized in order to avoid making {@code SQLErrorCodesFactory} constructor + * reachable on native images when not needed. */ - private static final SQLErrorCodesFactory instance = new SQLErrorCodesFactory(); + @Nullable + private static SQLErrorCodesFactory instance; /** * Return the singleton instance. */ public static SQLErrorCodesFactory getInstance() { + if (instance == null) { + instance = new SQLErrorCodesFactory(); + } return instance; }