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
This commit is contained in:
Sébastien Deleuze 2022-10-10 11:50:50 +02:00
parent 42c3ac64ff
commit 8ed1906f43
2 changed files with 11 additions and 3 deletions

View File

@ -77,6 +77,9 @@ public class SQLErrorCodeSQLExceptionTranslator extends AbstractFallbackSQLExcep
private static final int MESSAGE_SQL_THROWABLE_CONSTRUCTOR = 4; private static final int MESSAGE_SQL_THROWABLE_CONSTRUCTOR = 4;
private static final int MESSAGE_SQL_SQLEX_CONSTRUCTOR = 5; 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. */ /** Error codes used by this translator. */
@Nullable @Nullable
@ -424,8 +427,7 @@ public class SQLErrorCodeSQLExceptionTranslator extends AbstractFallbackSQLExcep
* in the root of the classpath. * in the root of the classpath.
*/ */
static boolean hasUserProvidedErrorCodesFile() { static boolean hasUserProvidedErrorCodesFile() {
return new ClassPathResource(SQLErrorCodesFactory.SQL_ERROR_CODE_OVERRIDE_PATH, return USER_PROVIDED_ERROR_CODES_FILE_PRESENT;
SQLErrorCodesFactory.class.getClassLoader()).exists();
} }
} }

View File

@ -68,14 +68,20 @@ public class SQLErrorCodesFactory {
/** /**
* Keep track of a single instance so we can return it to classes that request it. * 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. * Return the singleton instance.
*/ */
public static SQLErrorCodesFactory getInstance() { public static SQLErrorCodesFactory getInstance() {
if (instance == null) {
instance = new SQLErrorCodesFactory();
}
return instance; return instance;
} }