Java Reference
In-Depth Information
parameter and returns a
DataAccessException
.By default, the data access tem-
plate classes typically use a
SQLErrorCodeSQLExceptionTranslator
. This class
implements the
SQLExceptionTranslator
interface and translates a limited num-
ber of Oracle error codes to data access exceptions. We can add support for addi-
tional error codes by subclassing
SQLErrorCodeSQLExceptionTranslator
and
overriding its
customTranslate()
method:
public class MyOracleSQLExceptionTranslator extends
SQLErrorCodeSQLExceptionTranslator {
protected DataAccessException customTranslate(String task,
String sql, SQLException sqlex) {
switch (sqlex.getErrorCode()) {
case 8177:
return new CannotSerializeTransactionException(
"Can't serialize", sqlex);
case 60:
return new CannotAcquireLockException(
"Deadlock", sqlex);
default:
return null;
}
}
}
This class maps
ORA-00060
to
CannotAcquireLockException
and
ORA-08177
to
CannotSerializeTransactionException
. You would write a similar class for a dif-
ferent database.
Once we have written this class, we must configure the
JdbcTemplate
or
SqlMap-
ClientTemplate
objects, which are used by the application to access the database,
to use it. For example, a
SqlMapClientTemplate
is configured as follows:
<beans>
…
<bean id="SqlMapClientTemplate"
class="org.springframework.orm.iBatis.SqlMapClientTemplate"
autowire="constructor">
<property name="exceptionTranslator"
ref ="ExceptionTranslator"/>
</bean>
<bean id="ExceptionTranslator"
class="net.chrisrichardson.foodToGo.util.spring.
bbbbbbb
bb
➥
MyOracleSQLExceptionTranslator">
<property name="dataSource" ref="DataSource"/>
</bean>
…
</beans>
Search WWH ::
Custom Search