Skip to content

Commit 53a6fa2

Browse files
committed
Respect spring.dao.exceptiontranslation setting
Update `DataSourceTransactionManagerAutoConfiguration` to respect the `spring.dao.exceptiontranslation` setting. If `exceptiontranslation` is `false` then we create a classic `DataSourceTransactionManager` rather than a `JdbcTransactionManager`. Fixes gh-24321
1 parent cff3e4c commit 53a6fa2

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceTransactionManagerAutoConfiguration.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,7 +29,9 @@
2929
import org.springframework.context.annotation.Bean;
3030
import org.springframework.context.annotation.Configuration;
3131
import org.springframework.core.Ordered;
32+
import org.springframework.core.env.Environment;
3233
import org.springframework.jdbc.core.JdbcTemplate;
34+
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
3335
import org.springframework.jdbc.support.JdbcTransactionManager;
3436
import org.springframework.transaction.TransactionManager;
3537

@@ -54,13 +56,18 @@ static class JdbcTransactionManagerConfiguration {
5456

5557
@Bean
5658
@ConditionalOnMissingBean(TransactionManager.class)
57-
JdbcTransactionManager transactionManager(DataSource dataSource,
59+
DataSourceTransactionManager transactionManager(Environment environment, DataSource dataSource,
5860
ObjectProvider<TransactionManagerCustomizers> transactionManagerCustomizers) {
59-
JdbcTransactionManager transactionManager = new JdbcTransactionManager(dataSource);
61+
DataSourceTransactionManager transactionManager = createTransactionManager(environment, dataSource);
6062
transactionManagerCustomizers.ifAvailable((customizers) -> customizers.customize(transactionManager));
6163
return transactionManager;
6264
}
6365

66+
private DataSourceTransactionManager createTransactionManager(Environment environment, DataSource dataSource) {
67+
return environment.getProperty("spring.dao.exceptiontranslation.enable", Boolean.class, Boolean.TRUE)
68+
? new JdbcTransactionManager(dataSource) : new DataSourceTransactionManager(dataSource);
69+
}
70+
6471
}
6572

6673
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceTransactionManagerAutoConfigurationTests.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@
2525
import org.springframework.boot.autoconfigure.AutoConfigurations;
2626
import org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration;
2727
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
28+
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
2829
import org.springframework.jdbc.support.JdbcTransactionManager;
2930
import org.springframework.transaction.TransactionManager;
3031

@@ -83,6 +84,29 @@ void transactionManagerWithExistingTransactionManagerIsNotOverridden() {
8384
.hasBean("myTransactionManager"));
8485
}
8586

87+
@Test // gh-24321
88+
void transactionManagerWithDaoExceptionTranslationDisabled() {
89+
this.contextRunner.withConfiguration(AutoConfigurations.of(DataSourceAutoConfiguration.class))
90+
.withPropertyValues("spring.dao.exceptiontranslation.enable=false")
91+
.run((context) -> assertThat(context.getBean(TransactionManager.class))
92+
.isExactlyInstanceOf(DataSourceTransactionManager.class));
93+
}
94+
95+
@Test // gh-24321
96+
void transactionManagerWithDaoExceptionTranslationEnabled() {
97+
this.contextRunner.withConfiguration(AutoConfigurations.of(DataSourceAutoConfiguration.class))
98+
.withPropertyValues("spring.dao.exceptiontranslation.enable=true")
99+
.run((context) -> assertThat(context.getBean(TransactionManager.class))
100+
.isExactlyInstanceOf(JdbcTransactionManager.class));
101+
}
102+
103+
@Test // gh-24321
104+
void transactionManagerWithDaoExceptionTranslationDefault() {
105+
this.contextRunner.withConfiguration(AutoConfigurations.of(DataSourceAutoConfiguration.class))
106+
.run((context) -> assertThat(context.getBean(TransactionManager.class))
107+
.isExactlyInstanceOf(JdbcTransactionManager.class));
108+
}
109+
86110
@Test
87111
void transactionWithMultipleDataSourcesIsNotConfigured() {
88112
this.contextRunner.withUserConfiguration(MultiDataSourceConfiguration.class)

0 commit comments

Comments
 (0)