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; }