001/*
002 * Copyright 2002-2014 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.SQLDataException;
020import java.sql.SQLException;
021import java.sql.SQLFeatureNotSupportedException;
022import java.sql.SQLIntegrityConstraintViolationException;
023import java.sql.SQLInvalidAuthorizationSpecException;
024import java.sql.SQLNonTransientConnectionException;
025import java.sql.SQLNonTransientException;
026import java.sql.SQLRecoverableException;
027import java.sql.SQLSyntaxErrorException;
028import java.sql.SQLTimeoutException;
029import java.sql.SQLTransactionRollbackException;
030import java.sql.SQLTransientConnectionException;
031import java.sql.SQLTransientException;
032
033import org.springframework.dao.ConcurrencyFailureException;
034import org.springframework.dao.DataAccessException;
035import org.springframework.dao.DataAccessResourceFailureException;
036import org.springframework.dao.DataIntegrityViolationException;
037import org.springframework.dao.InvalidDataAccessApiUsageException;
038import org.springframework.dao.PermissionDeniedDataAccessException;
039import org.springframework.dao.QueryTimeoutException;
040import org.springframework.dao.RecoverableDataAccessException;
041import org.springframework.dao.TransientDataAccessResourceException;
042import org.springframework.jdbc.BadSqlGrammarException;
043
044/**
045 * {@link SQLExceptionTranslator} implementation which analyzes the specific
046 * {@link java.sql.SQLException} subclass thrown by the JDBC driver.
047 *
048 * <p>Falls back to a standard {@link SQLStateSQLExceptionTranslator} if the JDBC
049 * driver does not actually expose JDBC 4 compliant {@code SQLException} subclasses.
050 *
051 * @author Thomas Risberg
052 * @author Juergen Hoeller
053 * @since 2.5
054 * @see java.sql.SQLTransientException
055 * @see java.sql.SQLTransientException
056 * @see java.sql.SQLRecoverableException
057 */
058public class SQLExceptionSubclassTranslator extends AbstractFallbackSQLExceptionTranslator {
059
060        public SQLExceptionSubclassTranslator() {
061                setFallbackTranslator(new SQLStateSQLExceptionTranslator());
062        }
063
064        @Override
065        protected DataAccessException doTranslate(String task, String sql, SQLException ex) {
066                if (ex instanceof SQLTransientException) {
067                        if (ex instanceof SQLTransientConnectionException) {
068                                return new TransientDataAccessResourceException(buildMessage(task, sql, ex), ex);
069                        }
070                        else if (ex instanceof SQLTransactionRollbackException) {
071                                return new ConcurrencyFailureException(buildMessage(task, sql, ex), ex);
072                        }
073                        else if (ex instanceof SQLTimeoutException) {
074                                return new QueryTimeoutException(buildMessage(task, sql, ex), ex);
075                        }
076                }
077                else if (ex instanceof SQLNonTransientException) {
078                        if (ex instanceof SQLNonTransientConnectionException) {
079                                return new DataAccessResourceFailureException(buildMessage(task, sql, ex), ex);
080                        }
081                        else if (ex instanceof SQLDataException) {
082                                return new DataIntegrityViolationException(buildMessage(task, sql, ex), ex);
083                        }
084                        else if (ex instanceof SQLIntegrityConstraintViolationException) {
085                                return new DataIntegrityViolationException(buildMessage(task, sql, ex), ex);
086                        }
087                        else if (ex instanceof SQLInvalidAuthorizationSpecException) {
088                                return new PermissionDeniedDataAccessException(buildMessage(task, sql, ex), ex);
089                        }
090                        else if (ex instanceof SQLSyntaxErrorException) {
091                                return new BadSqlGrammarException(task, sql, ex);
092                        }
093                        else if (ex instanceof SQLFeatureNotSupportedException) {
094                                return new InvalidDataAccessApiUsageException(buildMessage(task, sql, ex), ex);
095                        }
096                }
097                else if (ex instanceof SQLRecoverableException) {
098                        return new RecoverableDataAccessException(buildMessage(task, sql, ex), ex);
099                }
100
101                // Fallback to Spring's own SQL state translation...
102                return null;
103        }
104
105}