001/* 002 * Copyright 2002-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.jdbc.support; 018 019import java.sql.SQLException; 020 021import org.springframework.dao.DataAccessException; 022import org.springframework.lang.Nullable; 023 024/** 025 * Strategy interface for translating between {@link SQLException SQLExceptions} 026 * and Spring's data access strategy-agnostic {@link DataAccessException} 027 * hierarchy. 028 * 029 * <p>Implementations can be generic (for example, using 030 * {@link java.sql.SQLException#getSQLState() SQLState} codes for JDBC) or wholly 031 * proprietary (for example, using Oracle error codes) for greater precision. 032 * 033 * @author Rod Johnson 034 * @author Juergen Hoeller 035 * @see org.springframework.dao.DataAccessException 036 */ 037@FunctionalInterface 038public interface SQLExceptionTranslator { 039 040 /** 041 * Translate the given {@link SQLException} into a generic {@link DataAccessException}. 042 * <p>The returned DataAccessException is supposed to contain the original 043 * {@code SQLException} as root cause. However, client code may not generally 044 * rely on this due to DataAccessExceptions possibly being caused by other resource 045 * APIs as well. That said, a {@code getRootCause() instanceof SQLException} 046 * check (and subsequent cast) is considered reliable when expecting JDBC-based 047 * access to have happened. 048 * @param task readable text describing the task being attempted 049 * @param sql the SQL query or update that caused the problem (if known) 050 * @param ex the offending {@code SQLException} 051 * @return the DataAccessException wrapping the {@code SQLException}, 052 * or {@code null} if no translation could be applied 053 * (in a custom translator; the default translators always throw an 054 * {@link org.springframework.jdbc.UncategorizedSQLException} in such a case) 055 * @see org.springframework.dao.DataAccessException#getRootCause() 056 */ 057 @Nullable 058 DataAccessException translate(String task, @Nullable String sql, SQLException ex); 059 060}