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;
022
023/**
024 * Strategy interface for translating between {@link SQLException SQLExceptions}
025 * and Spring's data access strategy-agnostic {@link DataAccessException}
026 * hierarchy.
027 *
028 * <p>Implementations can be generic (for example, using
029 * {@link java.sql.SQLException#getSQLState() SQLState} codes for JDBC) or wholly
030 * proprietary (for example, using Oracle error codes) for greater precision.
031 *
032 * @author Rod Johnson
033 * @author Juergen Hoeller
034 * @see org.springframework.dao.DataAccessException
035 */
036public interface SQLExceptionTranslator {
037
038        /**
039         * Translate the given {@link SQLException} into a generic {@link DataAccessException}.
040         * <p>The returned DataAccessException is supposed to contain the original
041         * {@code SQLException} as root cause. However, client code may not generally
042         * rely on this due to DataAccessExceptions possibly being caused by other resource
043         * APIs as well. That said, a {@code getRootCause() instanceof SQLException}
044         * check (and subsequent cast) is considered reliable when expecting JDBC-based
045         * access to have happened.
046         * @param task readable text describing the task being attempted
047         * @param sql the SQL query or update that caused the problem (if known)
048         * @param ex the offending {@code SQLException}
049         * @return the DataAccessException wrapping the {@code SQLException},
050         * or {@code null} if no translation could be applied
051         * (in a custom translator; the default translators always throw an
052         * {@link org.springframework.jdbc.UncategorizedSQLException} in such a case)
053         * @see org.springframework.dao.DataAccessException#getRootCause()
054         */
055        DataAccessException translate(String task, String sql, SQLException ex);
056
057}