Browse Source

Add conditional bean for jOOQ translator

Introduce an jOOQ `ExecuteListener` sub-interface  specifically
for exception translation with the auto-configured
`DefaultExecuteListenerProvider` instance.

Users can now define a bean that implements the interface or
omit it and continue to use the existing exception translation
logic.

See gh-38762
pull/39276/head
Dennis Melzer 2 years ago committed by Phillip Webb
parent
commit
c3aa95335a
  1. 10
      spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jooq/JooqAutoConfiguration.java
  2. 5
      spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jooq/JooqExceptionTranslator.java
  3. 38
      spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jooq/JooqExceptionTranslatorListener.java
  4. 40
      spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jooq/JooqAutoConfigurationTests.java

10
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jooq/JooqAutoConfiguration.java

@ -70,8 +70,14 @@ public class JooqAutoConfiguration { @@ -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)

5
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jooq/JooqExceptionTranslator.java

@ -21,7 +21,6 @@ import java.sql.SQLException; @@ -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; @@ -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; @@ -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

38
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jooq/JooqExceptionTranslatorListener.java

@ -0,0 +1,38 @@ @@ -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);
}

40
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jooq/JooqAutoConfigurationTests.java

@ -22,6 +22,7 @@ import org.jooq.CharsetProvider; @@ -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; @@ -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 { @@ -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 { @@ -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 { @@ -303,4 +334,13 @@ class JooqAutoConfigurationTests {
}
static class CustomJooqExceptionTranslator implements JooqExceptionTranslatorListener {
@Override
public void exception(ExecuteContext context) {
}
}
}

Loading…
Cancel
Save