一般在dao层将全部异常转嫁到Spring的RuntimeException体系(org.springframework.dao.DataAccessException)中来。 如:spring
try { vehicleDAO.insert(vehicle); } catch (DataAccessException e) { SQLException sqle = (SQLException) e.getCause(); System.out.println("Error code: " + sqle.getErrorCode()); System.out.println("SQL state: " + sqle.getSQLState()); }
咱们能够获取状态码和SQL状态。 这样就知道了这个错误的具体含义,好比104:惟一约束验证失败。这就是咱们故意设置的重复主键问题。sql
Spring的JDBC模块为咱们预约义了一些错误代码,它存储在org.springframework.jdbc.support包下的sql-error-codes.xml文件中,其中描述HSQL的内容为:数据库
<bean id="HSQL" class="org.springframework.jdbc.support.SQLErrorCodes"> <property name="databaseProductName"> <value>HSQL Database Engine</value> </property> <property name="badSqlGrammarCodes"> <value>-22,-28</value> </property> <property name="duplicateKeyCodes"> <value>-104</value> </property> <property name="dataIntegrityViolationCodes"> <value>-9</value> </property> <property name="dataAccessResourceFailureCodes"> <value>-80</value> </property> </bean>
上面咱们已经知道在org.springframework.jdbc.support包下有sql-error-codes.xml文件,在Spring启动时会自动读取这个文件中的错误码,它为咱们预分类了一些错误码,而咱们能够增强它,来使用咱们自定义的异常。工具
首先,定义一个异常类,咱们就来自定义一下前面的-104错误,就是HSQL的重复键的问题:code
package org.ourpioneer.vehicle.exception; import org.springframework.dao.DataIntegrityViolationException; public class VehicleDuplicateKeyException extends DataIntegrityViolationException { public VehicleDuplicateKeyException(String msg) { super(msg); } public VehicleDuplicateKeyException(String msg, Throwable cause) { super(msg, cause); } }
以后咱们从新新建一个sql-error-codes.xml代码,并将它放到类路径的根目录下,这样Spring会发现它并使用咱们自定义的文件,在配置中定义以下:xml
<bean id="HSQL" class="org.springframework.jdbc.support.SQLErrorCodes"> <property name="databaseProductName" value="HSQL Database Engine" /> <property name="useSqlStateForTranslation" value="false" /> <property name="customTranslations"> <list> <ref local="vehicleDuplicateKeyTranslation" /> </list> </property> </bean> <bean id="vehicleDuplicateKeyTranslation" class="org.springframework.jdbc.support.CustomSQLErrorCodesTranslation"> <property name="errorCodes" value="-104" /> <property name="exceptionClass" value="org.ourpioneer.vehicle.exception.VehicleDuplicateKeyException" /> </bean>
以后就可使用咱们本身定义的异常类了。出现异常后,VehicleDuplicateKeyException就抛出了。 DataAccessException是RuntimeException,是无需检查的异常,不要求进行代码处理。事务
这样服务层能够精确的捕获异常,或者向上继续抛出异常。资源
更多内容关注: get