Spring的DataAccessException略记

阅读更多
Spring的DAO框架没有抛出与特定技术相关的异常,例如SQLException或HibernateException,抛出的异常都是与特定技术无关的org.springframework.dao.DataAccessException类的子类,避免系统与某种特殊的持久层实现耦合在一起。DataAccessException是RuntimeException,是一个无须检测的异常,不要求代码去处理这类异常,遵循了Spring的一般理念:异常检测会使代码到处是不相关的catch或throws语句,使代码杂乱无章;并且NestedRuntimeException的子类,是可以通过NestedRuntimeException的getCause()方法获得导致该异常的另一个异常。Spring的异常分类有
Spring的DAO异常层次
异常
何时抛出
CleanupFailureDataAccessException 一项操作成功地执行,但在释放数据库资源时发生异常(例如,关闭一个Connection) DataAccessResourceFailureException 数据访问资源彻底失败,例如不能连接数据库 DataIntegrityViolationException Insert或Update数据时违反了完整性,例如违反了惟一性限制 DataRetrievalFailureException 某些数据不能被检测到,例如不能通过关键字找到一条记录 DeadlockLoserDataAccessException 当前的操作因为死锁而失败 IncorrectUpdateSemanticsDataAccessException Update时发生某些没有预料到的情况,例如更改超过预期的记录数。当这个异常被抛出时,执行着的事务不会被回滚 InvalidDataAccessApiusageException 一个数据访问的JAVA API没有正确使用,例如必须在执行前编译好的查询编译失败了 invalidDataAccessResourceUsageException 错误使用数据访问资源,例如用错误的SQL语法访问关系型数据库 OptimisticLockingFailureException 乐观锁的失败。这将由ORM工具或用户的DAO实现抛出 TypemismatchDataAccessException Java类型和数据类型不匹配,例如试图把String类型插入到数据库的数值型字段中 UncategorizedDataAccessException 有错误发生,但无法归类到某一更为具体的异常中 Spring的DAO异常层次是如此的细致缜密,服务对象能够精确地选择需要捕获哪些异常,捕获的异常对用户更有用的信息,哪些异常可以让她继续在调用堆栈中向上传递。
DataAccessException,它是由Spring framework定义的运行时异常。关于DataAccessException,有两件事需要注意。首先,它是运行时异常,所以使用数据访问对象的应用程序代码不需要像在JDBC和EJB 2.x实体bean的情况下那样使用try-catch块包装每次调用。第二,DataAccessException是有用的,因为它包装底层持久化技术所使用的特定异常类,从而使应用程序的其他部分独立于持久化层。

Spring’s DAO frameworks do not throw technology-specific exceptions, such
as SQLException or HibernateException. Instead, all exceptions thrown are
subclasses of the technology-agnostic org.springframework.dao.DataAccess-
Exception. This enables your data access interfaces to throw Spring’s general
DataAccessException instead of implementation-specific exceptions that would
force other application layers to catch them and thus become coupled to a particular
persistence implementation. In fact, you can intermingle multiple persistence
technologies within the same application without your service objects even
knowing it.
Since DataAccessException is the root of all Spring DAO exceptions, there are
a couple of important things to know.

你可能感兴趣的:(Spring的DataAccessException略记)