Skip to content

Commit

Permalink
Add support for configurable custom translator
Browse files Browse the repository at this point in the history
Closes gh-24634
  • Loading branch information
jhoeller committed Jul 14, 2023
1 parent 52c1927 commit 5199274
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,11 +26,15 @@
import org.springframework.util.Assert;

/**
* Base class for {@link SQLExceptionTranslator} implementations that allow for
* fallback to some other {@link SQLExceptionTranslator}.
* Base class for {@link SQLExceptionTranslator} implementations that allow for a
* fallback to some other {@link SQLExceptionTranslator}, as well as for custom
* overrides.
*
* @author Juergen Hoeller
* @since 2.5.6
* @see #doTranslate
* @see #setFallbackTranslator
* @see #setCustomTranslator
*/
public abstract class AbstractFallbackSQLExceptionTranslator implements SQLExceptionTranslator {

Expand All @@ -40,23 +44,47 @@ public abstract class AbstractFallbackSQLExceptionTranslator implements SQLExcep
@Nullable
private SQLExceptionTranslator fallbackTranslator;

@Nullable
private SQLExceptionTranslator customTranslator;


/**
* Override the default SQL state fallback translator
* (typically a {@link SQLStateSQLExceptionTranslator}).
* Set the fallback translator to use when this translator cannot find a
* specific match itself.
*/
public void setFallbackTranslator(@Nullable SQLExceptionTranslator fallback) {
this.fallbackTranslator = fallback;
}

/**
* Return the fallback exception translator, if any.
* @see #setFallbackTranslator
*/
@Nullable
public SQLExceptionTranslator getFallbackTranslator() {
return this.fallbackTranslator;
}

/**
* Set a custom exception translator to override any match that this translator
* would find. Note that such a custom {@link SQLExceptionTranslator} delegate
* is meant to return {@code null} if it does not have an override itself.
* @since 6.1
*/
public void setCustomTranslator(@Nullable SQLExceptionTranslator customTranslator) {
this.customTranslator = customTranslator;
}

/**
* Return a custom exception translator, if any.
* @since 6.1
* @see #setCustomTranslator
*/
@Nullable
public SQLExceptionTranslator getCustomTranslator() {
return this.customTranslator;
}


/**
* Pre-checks the arguments, calls {@link #doTranslate}, and invokes the
Expand All @@ -67,6 +95,15 @@ public SQLExceptionTranslator getFallbackTranslator() {
public DataAccessException translate(String task, @Nullable String sql, SQLException ex) {
Assert.notNull(ex, "Cannot translate a null SQLException");

SQLExceptionTranslator custom = getCustomTranslator();
if (custom != null) {
DataAccessException dae = custom.translate(task, sql, ex);
if (dae != null) {
// Custom exception match found.
return dae;
}
}

DataAccessException dae = doTranslate(task, sql, ex);
if (dae != null) {
// Specific exception match found.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@
* of the class path (e.g. in the "/WEB-INF/classes" directory), as long as the
* Spring JDBC package is loaded from the same ClassLoader.
*
* <p>This translator is commonly used by default if a user-provided `sql-error-codes.xml`
* file has been found in the root of the classpath, as a signal to use this strategy.
* Otherwise, {@link SQLExceptionSubclassTranslator} serves as the default translator
* as of 6.0.
*
* @author Rod Johnson
* @author Thomas Risberg
* @author Juergen Hoeller
Expand All @@ -75,11 +80,10 @@ 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();

private static final boolean userProvidedErrorCodesFilePresent =
new ClassPathResource(SQLErrorCodesFactory.SQL_ERROR_CODE_OVERRIDE_PATH,
SQLErrorCodesFactory.class.getClassLoader()).exists();

/** Error codes used by this translator. */
@Nullable
private SingletonSupplier<SQLErrorCodes> sqlErrorCodes;

Expand Down Expand Up @@ -198,9 +202,9 @@ protected DataAccessException doTranslate(String task, @Nullable String sql, SQL
if (sqlErrorCodes != null) {
SQLExceptionTranslator customTranslator = sqlErrorCodes.getCustomSqlExceptionTranslator();
if (customTranslator != null) {
DataAccessException customDex = customTranslator.translate(task, sql, sqlEx);
if (customDex != null) {
return customDex;
dae = customTranslator.translate(task, sql, sqlEx);
if (dae != null) {
return dae;
}
}
}
Expand Down Expand Up @@ -228,11 +232,10 @@ protected DataAccessException doTranslate(String task, @Nullable String sql, SQL
for (CustomSQLErrorCodesTranslation customTranslation : customTranslations) {
if (Arrays.binarySearch(customTranslation.getErrorCodes(), errorCode) >= 0 &&
customTranslation.getExceptionClass() != null) {
DataAccessException customException = createCustomException(
task, sql, sqlEx, customTranslation.getExceptionClass());
if (customException != null) {
dae = createCustomException(task, sql, sqlEx, customTranslation.getExceptionClass());
if (dae != null) {
logTranslation(task, sql, sqlEx, true);
return customException;
return dae;
}
}
}
Expand Down Expand Up @@ -306,7 +309,9 @@ else if (Arrays.binarySearch(sqlErrorCodes.getCannotSerializeTransactionCodes(),
* resulting from custom translation. This exception should include the {@code sqlEx} parameter
* as a nested root cause. This implementation always returns {@code null}, meaning that the
* translator always falls back to the default error codes.
* @deprecated as of 6.1, in favor of {@link #setCustomTranslator}
*/
@Deprecated(since = "6.1")
@Nullable
protected DataAccessException customTranslate(String task, @Nullable String sql, SQLException sqlEx) {
return null;
Expand Down Expand Up @@ -426,7 +431,7 @@ private void logTranslation(String task, @Nullable String sql, SQLException sqlE
* in the root of the classpath.
*/
static boolean hasUserProvidedErrorCodesFile() {
return USER_PROVIDED_ERROR_CODES_FILE_PRESENT;
return userProvidedErrorCodesFilePresent;
}

}

0 comments on commit 5199274

Please sign in to comment.