diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jooq/JooqAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jooq/JooqAutoConfiguration.java index 72944ddf3f..26b882e811 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jooq/JooqAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jooq/JooqAutoConfiguration.java @@ -70,8 +70,14 @@ public class JooqAutoConfiguration { @Bean @Order(0) - public DefaultExecuteListenerProvider jooqExceptionTranslatorExecuteListenerProvider() { - return new DefaultExecuteListenerProvider(new JooqExceptionTranslator()); + public DefaultExecuteListenerProvider jooqExceptionTranslatorExecuteListenerProvider(JooqExceptionTranslatorListener jooqExceptionTranslator) { + return new DefaultExecuteListenerProvider(jooqExceptionTranslator); + } + + @Bean + @ConditionalOnMissingBean(JooqExceptionTranslatorListener.class) + public JooqExceptionTranslatorListener jooqExceptionTranslator() { + return new JooqExceptionTranslator(); } @Configuration(proxyBeanMethods = false) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jooq/JooqExceptionTranslator.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jooq/JooqExceptionTranslator.java index 383da48c97..b27d99213c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jooq/JooqExceptionTranslator.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jooq/JooqExceptionTranslator.java @@ -21,7 +21,6 @@ import java.sql.SQLException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.jooq.ExecuteContext; -import org.jooq.ExecuteListener; import org.jooq.SQLDialect; import org.springframework.dao.DataAccessException; @@ -30,7 +29,7 @@ import org.springframework.jdbc.support.SQLExceptionTranslator; import org.springframework.jdbc.support.SQLStateSQLExceptionTranslator; /** - * Transforms {@link java.sql.SQLException} into a Spring-specific + * Transforms {@link SQLException} into a Spring-specific * {@link DataAccessException}. * * @author Lukas Eder @@ -39,7 +38,7 @@ import org.springframework.jdbc.support.SQLStateSQLExceptionTranslator; * @author Stephane Nicoll * @since 1.5.10 */ -public class JooqExceptionTranslator implements ExecuteListener { +public class JooqExceptionTranslator implements JooqExceptionTranslatorListener { // Based on the jOOQ-spring-example from https://github.com/jOOQ/jOOQ diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jooq/JooqExceptionTranslatorListener.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jooq/JooqExceptionTranslatorListener.java new file mode 100644 index 0000000000..a02b8508f2 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jooq/JooqExceptionTranslatorListener.java @@ -0,0 +1,38 @@ +/* + * Copyright 2012-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.jooq; + +import org.jooq.ExecuteContext; +import org.jooq.ExecuteListener; + +import org.springframework.dao.DataAccessException; + +/** + * A {@link ExecuteListener} which can transforms or translate {@link Exception} into a Spring-specific + * {@link DataAccessException}. + * + * @author Dennis Melzer + * @since 3.2.1 + */ +public interface JooqExceptionTranslatorListener extends ExecuteListener { + + /** + * Override the given {@link Exception} from {@link ExecuteContext} into a generic {@link DataAccessException}. + * @param context The context containing information about the execution. + */ + void exception(ExecuteContext context); +} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jooq/JooqAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jooq/JooqAutoConfigurationTests.java index 1e78fee1d9..d2c41e3196 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jooq/JooqAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jooq/JooqAutoConfigurationTests.java @@ -22,6 +22,7 @@ import org.jooq.CharsetProvider; import org.jooq.ConnectionProvider; import org.jooq.ConverterProvider; import org.jooq.DSLContext; +import org.jooq.ExecuteContext; import org.jooq.ExecuteListener; import org.jooq.ExecuteListenerProvider; import org.jooq.SQLDialect; @@ -55,6 +56,7 @@ import static org.mockito.Mockito.mock; * @author Andy Wilkinson * @author Stephane Nicoll * @author Dmytro Nosan + * @author Dennis Melzer */ class JooqAutoConfigurationTests { @@ -180,6 +182,25 @@ class JooqAutoConfigurationTests { }); } + @Test + void jooqExceptionTranslatorProviderFromConfigurationCustomizerOverridesJooqExceptionTranslatorBean() { + this.contextRunner + .withUserConfiguration(JooqDataSourceConfiguration.class, CustomJooqExceptionTranslatorConfiguration.class) + .run((context) -> { + JooqExceptionTranslatorListener translator = context.getBean(JooqExceptionTranslatorListener.class); + assertThat(translator).isInstanceOf(CustomJooqExceptionTranslator.class); + }); + } + + @Test + void jooqWithDefaultJooqExceptionTranslator() { + this.contextRunner.withUserConfiguration(JooqDataSourceConfiguration.class).run((context) -> { + JooqExceptionTranslatorListener translator = context.getBean(JooqExceptionTranslatorListener.class); + assertThat(translator).isInstanceOf(JooqExceptionTranslator.class); + }); + } + + @Test void transactionProviderFromConfigurationCustomizerOverridesTransactionProviderBean() { this.contextRunner @@ -254,6 +275,16 @@ class JooqAutoConfigurationTests { } + @Configuration(proxyBeanMethods = false) + static class CustomJooqExceptionTranslatorConfiguration { + + @Bean + JooqExceptionTranslatorListener jooqExceptionTranslator() { + return new CustomJooqExceptionTranslator(); + } + + } + @Configuration(proxyBeanMethods = false) static class CustomTransactionProviderFromCustomizerConfiguration { @@ -303,4 +334,13 @@ class JooqAutoConfigurationTests { } + static class CustomJooqExceptionTranslator implements JooqExceptionTranslatorListener { + + + @Override + public void exception(ExecuteContext context) { + + } + } + }