Java Reference
In-Depth Information
<property name="duplicateKeyCodes">
<property name="dataIntegrityViolationCodes">
<property name="dataAccessResourceFailureCodes">
<property name="cannotAcquireLockCodes">
<property name="deadlockLoserCodes">
Note that the databaseProductName property is used to match the database product name returned
by Connection.getMetaData().getDatabaseProductName() . This enables Spring to know which type of
database is currently connecting. The useSqlStateForTranslation property means that the SQLState
property, rather than the errorCode property, should be used to match the error code. Finally, the
SQLErrorCodes class defines several categories for you to map database error codes. The code 23505 lies
in the dataIntegrityViolationCodes category.
Customizing Data Access Exception Handling
The Spring JDBC framework only maps well-known error codes. Sometimes you may wish to customize
the mapping yourself. For example, you might decide to add more codes to an existing category or
define a custom exception for particular error codes.
In Table 3-2, the error code 23505 indicates a duplicate key error in Apache Derby. It is mapped
by default to DataIntegrityViolationException . Suppose that you want to create a custom exception
type, MyDuplicateKeyException , for this kind of error. It should extend DataIntegrityViolationException
because it is also a kind of data integrity violation error. Remember that for an exception to be thrown by
the Spring JDBC framework, it must be compatible with the root exception class DataAccessException .
package com.apress.springenterpriserecipes.vehicle;
import org.springframework.dao.DataIntegrityViolationException;
public class MyDuplicateKeyException extends DataIntegrityViolationException {
public MyDuplicateKeyException(String msg) {
public MyDuplicateKeyException(String msg, Throwable cause) {
super(msg, cause);
Search WWH ::

Custom Search