JDBC的异常转换器
传统的JDBC API在发生几乎所有的数据操作问题都抛出相同的SQLException,它将异常的细节性信息封装在异常属性中,所以如果希望了解异常的具体原因,你必须分析异常对象的信息。
SQLException拥有两个代表异常具体原因的属性:错误码和SQL状态码,前者是数据库相关的,可通过getErrorCode()返回,其值的类型是int;而后者是一个标准的错误代码,可通过getSQLState()返回,是一个String类型的值,由5字符组成。
Spring根据错误码和SQL状态码信息将SQLExeption翻译成Spring DAO的异常体系。在org.springframework.jdbc.support包中定义了SQLExceptionTranslator接口,该接口的两个实现类SQLErrorCodeSQLExceptionTranslator和SQLStateSQLExceptionTranslator分别负责处理SQLException中错误代码和SQL状态码的翻译工作。将SQLException翻译成Spring DAO异常体系的工作是比较艰辛的,但Spring框架替我们完成这项艰巨的工作并保证转换的正确性,我们有充分的理由依赖这个转换的正确性。
spring这样做主要有2个作用,一是统一建立spring框架的异常框架体系,使得不同的异常最后都封装转换成spring异常体系框架中,这个框架体系分类很详细,这样对问题也比较好定位。
二是使得spring对底层异常保持独立,涡轮采用什么样的dao层,都不影响spring的异常体系。
异常的基本概念:
实现代码:
import org.springframework.dao.ConcurrencyFailureException;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.PermissionDeniedDataAccessException;
import org.springframework.dao.RecoverableDataAccessException;
import org.springframework.dao.TransientDataAccessResourceException;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.jdbc.UncategorizedSQLException;
/**
* 统一捕获异常
* @author zhengjunxiang
*
*/
public class CaptureException {
//处理异常方法
private void catchException(){
try{
}catch(BadSqlGrammarException e1){//NonTransient
/**
* Exception thrown when SQL specified is invalid. Such exceptions always have
* a <code>java.sql.SQLException</code> root cause.
*
* <p>It would be possible to have subclasses for no such table, no such column etc.
* A custom SQLExceptionTranslator could create such more specific exceptions,
* without affecting code using this class.
*
* @author Rod Johnson
* @see InvalidResultSetAccessException
*/
}catch(DataIntegrityViolationException e2){
/**
* Exception thrown when an attempt to insert or update data
* results in violation of an integrity constraint. Note that this
* is not purely a relational concept; unique primary keys are
* required by most database types.
*
* @author Rod Johnson
*/
}catch(DataAccessResourceFailureException e3){
/**
* Data access exception thrown when a resource fails completely:
* for example, if we can't connect to a database using JDBC.
*
* @author Rod Johnson
* @author Thomas Risberg
*/
}catch(PermissionDeniedDataAccessException e5){
/**
* Exception thrown when the underlying resource denied a permission
* to access a specific element, such as a specific database table.
*
* @author Juergen Hoeller
* @since 2.0
*/
}catch(ConcurrencyFailureException e6){//Transient
/**
* Exception thrown on concurrency failure.
*
* <p>This exception should be subclassed to indicate the type of failure:
* optimistic locking, failure to acquire lock, etc.
*
* @author Thomas Risberg
* @since 1.1
* @see OptimisticLockingFailureException
* @see PessimisticLockingFailureException
* @see CannotAcquireLockException
* @see DeadlockLoserDataAccessException
*/
}catch(TransientDataAccessResourceException e7){
/**
* Data access exception thrown when a resource fails temporarily
* and the operation can be retried.
*
* @author Thomas Risberg
* @since 2.5
* @see java.sql.SQLTransientConnectionException
*/
}catch(RecoverableDataAccessException e8){//Recoverable
/**
* Data access exception thrown when a previously failed operation might be able
* to succeed if the application performs some recovery steps and retries the entire
* transaction or in the case of a distributed transaction, the transaction branch.
* At a minimum, the recovery operation must include closing the current connection
* and getting a new connection.
*
* @author Thomas Risberg
* @since 2.5
* @see java.sql.SQLRecoverableException
*/
}catch(UncategorizedSQLException e9){
/**
* Exception thrown when we can't classify a SQLException into
* one of our generic data access exceptions.
*
* @author Rod Johnson
* @author Juergen Hoeller
*/
}catch(DataAccessException e10){ //所以异常的基类,下包括事务异常和非事务异常,以上是spring抛出的SqlException异常
/**
* Root of the hierarchy of data access exceptions discussed in
* <a href="http://www.amazon.com/exec/obidos/tg/detail/-/0764543857/">Expert One-On-One J2EE Design and Development</a>.
* Please see Chapter 9 of this book for detailed discussion of the
* motivation for this package.
*
* <p>This exception hierarchy aims to let user code find and handle the
* kind of error encountered without knowing the details of the particular
* data access API in use (e.g. JDBC). Thus it is possible to react to an
* optimistic locking failure without knowing that JDBC is being used.
*
* <p>As this class is a runtime exception, there is no need for user code
* to catch it or subclasses if any error is to be considered fatal
* (the usual case).
*
* @author Rod Johnson
*/
}catch(NumberFormatException nfe){
/**
* 数字转换异常
*/
}catch(Throwable e){
/**
* 异常的最基类
*/
}
}
}