|
| 1 | +/* |
| 2 | + * Copyright 2012-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.autoconfigure.jooq; |
| 18 | + |
| 19 | +import java.sql.SQLException; |
| 20 | +import java.util.function.Function; |
| 21 | + |
| 22 | +import org.apache.commons.logging.Log; |
| 23 | +import org.apache.commons.logging.LogFactory; |
| 24 | +import org.jooq.ExecuteContext; |
| 25 | +import org.jooq.SQLDialect; |
| 26 | + |
| 27 | +import org.springframework.dao.DataAccessException; |
| 28 | +import org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator; |
| 29 | +import org.springframework.jdbc.support.SQLExceptionTranslator; |
| 30 | +import org.springframework.jdbc.support.SQLStateSQLExceptionTranslator; |
| 31 | +import org.springframework.util.Assert; |
| 32 | + |
| 33 | +/** |
| 34 | + * Default implementation of {@link ExceptionTranslatorExecuteListener} that delegates to |
| 35 | + * an {@link SQLExceptionTranslator}. |
| 36 | + * |
| 37 | + * @author Lukas Eder |
| 38 | + * @author Andreas Ahlenstorf |
| 39 | + * @author Phillip Webb |
| 40 | + * @author Stephane Nicoll |
| 41 | + */ |
| 42 | +final class DefaultExceptionTranslatorExecuteListener implements ExceptionTranslatorExecuteListener { |
| 43 | + |
| 44 | + // Based on the jOOQ-spring-example from https://github.com/jOOQ/jOOQ |
| 45 | + |
| 46 | + private static final Log defaultLogger = LogFactory.getLog(ExceptionTranslatorExecuteListener.class); |
| 47 | + |
| 48 | + private final Log logger; |
| 49 | + |
| 50 | + private Function<ExecuteContext, SQLExceptionTranslator> translatorFactory; |
| 51 | + |
| 52 | + DefaultExceptionTranslatorExecuteListener() { |
| 53 | + this(defaultLogger, new DefaultTranslatorFactory()); |
| 54 | + } |
| 55 | + |
| 56 | + DefaultExceptionTranslatorExecuteListener(Function<ExecuteContext, SQLExceptionTranslator> translatorFactory) { |
| 57 | + this(defaultLogger, translatorFactory); |
| 58 | + } |
| 59 | + |
| 60 | + DefaultExceptionTranslatorExecuteListener(Log logger) { |
| 61 | + this(logger, new DefaultTranslatorFactory()); |
| 62 | + } |
| 63 | + |
| 64 | + private DefaultExceptionTranslatorExecuteListener(Log logger, |
| 65 | + Function<ExecuteContext, SQLExceptionTranslator> translatorFactory) { |
| 66 | + Assert.notNull(translatorFactory, "TranslatorFactory must not be null"); |
| 67 | + this.logger = logger; |
| 68 | + this.translatorFactory = translatorFactory; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void exception(ExecuteContext context) { |
| 73 | + SQLExceptionTranslator translator = this.translatorFactory.apply(context); |
| 74 | + // The exception() callback is not only triggered for SQL exceptions but also for |
| 75 | + // "normal" exceptions. In those cases sqlException() returns null. |
| 76 | + SQLException exception = context.sqlException(); |
| 77 | + while (exception != null) { |
| 78 | + handle(context, translator, exception); |
| 79 | + exception = exception.getNextException(); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Handle a single exception in the chain. SQLExceptions might be nested multiple |
| 85 | + * levels deep. The outermost exception is usually the least interesting one ("Call |
| 86 | + * getNextException to see the cause."). Therefore the innermost exception is |
| 87 | + * propagated and all other exceptions are logged. |
| 88 | + * @param context the execute context |
| 89 | + * @param translator the exception translator |
| 90 | + * @param exception the exception |
| 91 | + */ |
| 92 | + private void handle(ExecuteContext context, SQLExceptionTranslator translator, SQLException exception) { |
| 93 | + DataAccessException translated = translator.translate("jOOQ", context.sql(), exception); |
| 94 | + if (exception.getNextException() != null) { |
| 95 | + this.logger.error("Execution of SQL statement failed.", (translated != null) ? translated : exception); |
| 96 | + return; |
| 97 | + } |
| 98 | + if (translated != null) { |
| 99 | + context.exception(translated); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Default {@link SQLExceptionTranslator} factory that creates the translator based on |
| 105 | + * the Spring DB name. |
| 106 | + */ |
| 107 | + private static final class DefaultTranslatorFactory implements Function<ExecuteContext, SQLExceptionTranslator> { |
| 108 | + |
| 109 | + @Override |
| 110 | + public SQLExceptionTranslator apply(ExecuteContext context) { |
| 111 | + return apply(context.configuration().dialect()); |
| 112 | + } |
| 113 | + |
| 114 | + private SQLExceptionTranslator apply(SQLDialect dialect) { |
| 115 | + String dbName = getSpringDbName(dialect); |
| 116 | + return (dbName != null) ? new SQLErrorCodeSQLExceptionTranslator(dbName) |
| 117 | + : new SQLStateSQLExceptionTranslator(); |
| 118 | + } |
| 119 | + |
| 120 | + private String getSpringDbName(SQLDialect dialect) { |
| 121 | + return (dialect != null && dialect.thirdParty() != null) ? dialect.thirdParty().springDbName() : null; |
| 122 | + } |
| 123 | + |
| 124 | + } |
| 125 | + |
| 126 | +} |
0 commit comments