org.springframework.jdbc.support
Class SQLErrorCodeSQLExceptionTranslator

java.lang.Object
  extended byorg.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator
All Implemented Interfaces:
SQLExceptionTranslator

public class SQLErrorCodeSQLExceptionTranslator
extends Object
implements SQLExceptionTranslator

Implementation of SQLExceptionTranslator that uses specific vendor codes. More precise than SQLState implementation, but vendor-specific.

This class applies the following matching rules:

Author:
Rod Johnson, Thomas Risberg
See Also:
SQLErrorCodesFactory

Field Summary
protected  Log logger
           
protected  SQLErrorCodes sqlErrorCodes
          Error codes available to subclasses
 
Constructor Summary
SQLErrorCodeSQLExceptionTranslator()
          Constructor for use as a JavaBean.
SQLErrorCodeSQLExceptionTranslator(DataSource ds)
          Create a SQLErrorCode translator for the given DataSource.
SQLErrorCodeSQLExceptionTranslator(SQLErrorCodes sec)
          Create a SQLErrorCode translator given these error codes.
 
Method Summary
protected  DataAccessException customTranslate(String task, String sql, SQLException sqlex)
          Subclasses can override this method to attempt a custom mapping from SQLException to DataAccessException.
 void setDataSource(DataSource ds)
          Set the DataSource.
 void setFallbackTranslator(SQLExceptionTranslator fallback)
          Override the default SQLState fallback translator
 void setSqlErrorCodes(SQLErrorCodes sec)
          Set custom error codes to be used for translation
 DataAccessException translate(String task, String sql, SQLException sqlex)
          Translate the given SQL exception into a generic data access exception.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected final Log logger

sqlErrorCodes

protected SQLErrorCodes sqlErrorCodes
Error codes available to subclasses

Constructor Detail

SQLErrorCodeSQLExceptionTranslator

public SQLErrorCodeSQLExceptionTranslator()
Constructor for use as a JavaBean. The SqlErrorCodes or DataSource property must be set.


SQLErrorCodeSQLExceptionTranslator

public SQLErrorCodeSQLExceptionTranslator(SQLErrorCodes sec)
Create a SQLErrorCode translator given these error codes. Does not require a database metadata lookup to be performed using a connection.

Parameters:
sec - error codes

SQLErrorCodeSQLExceptionTranslator

public SQLErrorCodeSQLExceptionTranslator(DataSource ds)
Create a SQLErrorCode translator for the given DataSource. Invoking this constructor will cause a connection to be obtained from the DataSource to get the metadata

Parameters:
ds - DataSource to use to find metadata and establish which error codes are usable
Method Detail

setSqlErrorCodes

public void setSqlErrorCodes(SQLErrorCodes sec)
Set custom error codes to be used for translation

Parameters:
sec - custom error codes to use

setDataSource

public void setDataSource(DataSource ds)
Set the DataSource.

Setting this property will cause a connection to be obtained from the DataSource to get the metadata.

Parameters:
ds - DataSource to use to find metadata and establish which error codes are usable

setFallbackTranslator

public void setFallbackTranslator(SQLExceptionTranslator fallback)
Override the default SQLState fallback translator

Parameters:
fallback - custom fallback exception translator to use if error code translation fails

translate

public DataAccessException translate(String task,
                                     String sql,
                                     SQLException sqlex)
Description copied from interface: SQLExceptionTranslator
Translate the given SQL exception into a generic data access exception.

Specified by:
translate in interface SQLExceptionTranslator
Parameters:
task - readable text describing the task being attempted
sql - SQL query or update that caused the problem. May be null.
sqlex - SQLException encountered by JDBC implementation

customTranslate

protected DataAccessException customTranslate(String task,
                                              String sql,
                                              SQLException sqlex)
Subclasses can override this method to attempt a custom mapping from SQLException to DataAccessException.

Parameters:
task - task being attempted
sql - SQL that caused the problem
sqlex - offending SQLException
Returns:
null if no custom translation was possible, otherwise a DataAccessException resulting from custom translation. This exception should include the sqlex parameter as a nested root cause. This implementation always returns null, meaning that the translator always falls back to the default error codes.


Copyright (C) 2003-2004 The Spring Framework Project.