Package org.springframework.jdbc.support

# Class AbstractFallbackSQLExceptionTranslator

java.lang.Object
org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator

All Implemented Interfaces:
  `SQLExceptionTranslator`

Direct Known Subclasses:
  `SQLErrorCodeSQLExceptionTranslator`, `SQLExceptionSubclassTranslator`, `SQLStateSQLExceptionTranslator`

public abstract class AbstractFallbackSQLExceptionTranslator extends Object implements SQLExceptionTranslator
Base class for `SQLExceptionTranslator` implementations that allow for a fallback to some other `SQLExceptionTranslator`, as well as for custom overrides.

Since:
  2.5.6
Author:
  Juergen Hoeller
See Also:
  - `doTranslate(java.lang.String, java.lang.String, java.sql.SQLException)`
  - `setFallbackTranslator(org.springframework.jdbc.support.SQLExceptionTranslator)`
  - `setCustomTranslator(org.springframework.jdbc.support.SQLExceptionTranslator)`

## Field Summary

Fields
Modifier and Type
Field
Description
`protected final org.apache.commons.logging.Log`
`logger`
Logger available to subclasses.

## Constructor Summary

Constructors
Constructor
Description
`AbstractFallbackSQLExceptionTranslator()`

## Method Summary

Modifier and Type
Method
Description
`protected String`
`buildMessage(String task, String sql, SQLException ex)`
Build a message `String` for the given `SQLException`.
`protected abstract org.springframework.dao.DataAccessException`
`doTranslate(String task, String sql, SQLException ex)`
Template method for actually translating the given exception.
`SQLExceptionTranslator`
`getCustomTranslator()`
Return a custom exception translator, if any.
`SQLExceptionTranslator`
`getFallbackTranslator()`
Return the fallback exception translator, if any.
`void`
`setCustomTranslator(SQLExceptionTranslator customTranslator)`
Set a custom exception translator to override any match that this translator would find.
`void`
`setFallbackTranslator(SQLExceptionTranslator fallback)`
Set the fallback translator to use when this translator cannot find a specific match itself.
`org.springframework.dao.DataAccessException`
`translate(String task, String sql, SQLException ex)`
Pre-checks the arguments, calls `doTranslate(java.lang.String, java.lang.String, java.sql.SQLException)`, and invokes the `fallback translator` if necessary.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Field Details

### logger

protected final org.apache.commons.logging.Log logger
Logger available to subclasses.

## Constructor Details

### AbstractFallbackSQLExceptionTranslator

public AbstractFallbackSQLExceptionTranslator()

## Method Details

### setFallbackTranslator

public void setFallbackTranslator(@Nullable SQLExceptionTranslator fallback)
Set the fallback translator to use when this translator cannot find a specific match itself.

### getFallbackTranslator

@Nullable public SQLExceptionTranslator getFallbackTranslator()
Return the fallback exception translator, if any.

See Also:
  - `setFallbackTranslator(org.springframework.jdbc.support.SQLExceptionTranslator)`

### setCustomTranslator

public void setCustomTranslator(@Nullable SQLExceptionTranslator customTranslator)
Set a custom exception translator to override any match that this translator would find. Note that such a custom `SQLExceptionTranslator` delegate is meant to return `null` if it does not have an override itself.

Since:
  6.1

### getCustomTranslator

@Nullable public SQLExceptionTranslator getCustomTranslator()
Return a custom exception translator, if any.

Since:
  6.1
See Also:
  - `setCustomTranslator(org.springframework.jdbc.support.SQLExceptionTranslator)`

### translate

@Nullable public org.springframework.dao.DataAccessException translate(String task, @Nullable String sql, SQLException ex)
Pre-checks the arguments, calls `doTranslate(java.lang.String, java.lang.String, java.sql.SQLException)`, and invokes the `fallback translator` if necessary.

Specified by:
  `translate` in interface `SQLExceptionTranslator`
Parameters:
  `task` - readable text describing the task being attempted
  `sql` - the SQL query or update that caused the problem (if known)
  `ex` - the offending `SQLException`
Returns:
  the DataAccessException wrapping the `SQLException`, or `null` if no specific translation could be applied
See Also:
  - `NestedRuntimeException.getRootCause()`

### doTranslate

@Nullable protected abstract org.springframework.dao.DataAccessException doTranslate(String task, @Nullable String sql, SQLException ex)
Template method for actually translating the given exception.

The passed-in arguments will have been pre-checked. Furthermore, this method is allowed to return `null` to indicate that no exception match has been found and that fallback translation should kick in.

Parameters:
  `task` - readable text describing the task being attempted
  `sql` - the SQL query or update that caused the problem (if known)
  `ex` - the offending `SQLException`
Returns:
  the DataAccessException, wrapping the `SQLException`; or `null` if no exception match found

### buildMessage

protected String buildMessage(String task, @Nullable String sql, SQLException ex)
Build a message `String` for the given `SQLException`.

To be called by translator subclasses when creating an instance of a generic `DataAccessException` class.

Parameters:
  `task` - readable text describing the task being attempted
  `sql` - the SQL statement that caused the problem
  `ex` - the offending `SQLException`
Returns:
  the message `String` to use