Skip to content

Commit c3aa953

Browse files
MelleDphilwebb
authored andcommitted
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
1 parent 83fd4fb commit c3aa953

File tree

4 files changed

+88
-5
lines changed

4 files changed

+88
-5
lines changed

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,14 @@ public SpringTransactionProvider transactionProvider(PlatformTransactionManager
7070

7171
@Bean
7272
@Order(0)
73-
public DefaultExecuteListenerProvider jooqExceptionTranslatorExecuteListenerProvider() {
74-
return new DefaultExecuteListenerProvider(new JooqExceptionTranslator());
73+
public DefaultExecuteListenerProvider jooqExceptionTranslatorExecuteListenerProvider(JooqExceptionTranslatorListener jooqExceptionTranslator) {
74+
return new DefaultExecuteListenerProvider(jooqExceptionTranslator);
75+
}
76+
77+
@Bean
78+
@ConditionalOnMissingBean(JooqExceptionTranslatorListener.class)
79+
public JooqExceptionTranslatorListener jooqExceptionTranslator() {
80+
return new JooqExceptionTranslator();
7581
}
7682

7783
@Configuration(proxyBeanMethods = false)

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.apache.commons.logging.Log;
2222
import org.apache.commons.logging.LogFactory;
2323
import org.jooq.ExecuteContext;
24-
import org.jooq.ExecuteListener;
2524
import org.jooq.SQLDialect;
2625

2726
import org.springframework.dao.DataAccessException;
@@ -30,7 +29,7 @@
3029
import org.springframework.jdbc.support.SQLStateSQLExceptionTranslator;
3130

3231
/**
33-
* Transforms {@link java.sql.SQLException} into a Spring-specific
32+
* Transforms {@link SQLException} into a Spring-specific
3433
* {@link DataAccessException}.
3534
*
3635
* @author Lukas Eder
@@ -39,7 +38,7 @@
3938
* @author Stephane Nicoll
4039
* @since 1.5.10
4140
*/
42-
public class JooqExceptionTranslator implements ExecuteListener {
41+
public class JooqExceptionTranslator implements JooqExceptionTranslatorListener {
4342

4443
// Based on the jOOQ-spring-example from https://github.com/jOOQ/jOOQ
4544

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2012-2022 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 org.jooq.ExecuteContext;
20+
import org.jooq.ExecuteListener;
21+
22+
import org.springframework.dao.DataAccessException;
23+
24+
/**
25+
* A {@link ExecuteListener} which can transforms or translate {@link Exception} into a Spring-specific
26+
* {@link DataAccessException}.
27+
*
28+
* @author Dennis Melzer
29+
* @since 3.2.1
30+
*/
31+
public interface JooqExceptionTranslatorListener extends ExecuteListener {
32+
33+
/**
34+
* Override the given {@link Exception} from {@link ExecuteContext} into a generic {@link DataAccessException}.
35+
* @param context The context containing information about the execution.
36+
*/
37+
void exception(ExecuteContext context);
38+
}

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

+40
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.jooq.ConnectionProvider;
2323
import org.jooq.ConverterProvider;
2424
import org.jooq.DSLContext;
25+
import org.jooq.ExecuteContext;
2526
import org.jooq.ExecuteListener;
2627
import org.jooq.ExecuteListenerProvider;
2728
import org.jooq.SQLDialect;
@@ -55,6 +56,7 @@
5556
* @author Andy Wilkinson
5657
* @author Stephane Nicoll
5758
* @author Dmytro Nosan
59+
* @author Dennis Melzer
5860
*/
5961
class JooqAutoConfigurationTests {
6062

@@ -180,6 +182,25 @@ void transactionProviderBacksOffOnExistingTransactionProvider() {
180182
});
181183
}
182184

185+
@Test
186+
void jooqExceptionTranslatorProviderFromConfigurationCustomizerOverridesJooqExceptionTranslatorBean() {
187+
this.contextRunner
188+
.withUserConfiguration(JooqDataSourceConfiguration.class, CustomJooqExceptionTranslatorConfiguration.class)
189+
.run((context) -> {
190+
JooqExceptionTranslatorListener translator = context.getBean(JooqExceptionTranslatorListener.class);
191+
assertThat(translator).isInstanceOf(CustomJooqExceptionTranslator.class);
192+
});
193+
}
194+
195+
@Test
196+
void jooqWithDefaultJooqExceptionTranslator() {
197+
this.contextRunner.withUserConfiguration(JooqDataSourceConfiguration.class).run((context) -> {
198+
JooqExceptionTranslatorListener translator = context.getBean(JooqExceptionTranslatorListener.class);
199+
assertThat(translator).isInstanceOf(JooqExceptionTranslator.class);
200+
});
201+
}
202+
203+
183204
@Test
184205
void transactionProviderFromConfigurationCustomizerOverridesTransactionProviderBean() {
185206
this.contextRunner
@@ -254,6 +275,16 @@ TransactionProvider transactionProvider() {
254275

255276
}
256277

278+
@Configuration(proxyBeanMethods = false)
279+
static class CustomJooqExceptionTranslatorConfiguration {
280+
281+
@Bean
282+
JooqExceptionTranslatorListener jooqExceptionTranslator() {
283+
return new CustomJooqExceptionTranslator();
284+
}
285+
286+
}
287+
257288
@Configuration(proxyBeanMethods = false)
258289
static class CustomTransactionProviderFromCustomizerConfiguration {
259290

@@ -303,4 +334,13 @@ public void rollback(TransactionContext ctx) {
303334

304335
}
305336

337+
static class CustomJooqExceptionTranslator implements JooqExceptionTranslatorListener {
338+
339+
340+
@Override
341+
public void exception(ExecuteContext context) {
342+
343+
}
344+
}
345+
306346
}

0 commit comments

Comments
 (0)