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
This commit is contained in:
Phillip Webb 2021-01-11 13:55:27 -08:00
parent cff3e4c831
commit 53a6fa2fed
2 changed files with 35 additions and 4 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
@ -29,7 +29,9 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.support.JdbcTransactionManager;
import org.springframework.transaction.TransactionManager;
@ -54,13 +56,18 @@ public class DataSourceTransactionManagerAutoConfiguration {
@Bean
@ConditionalOnMissingBean(TransactionManager.class)
JdbcTransactionManager transactionManager(DataSource dataSource,
DataSourceTransactionManager transactionManager(Environment environment, DataSource dataSource,
ObjectProvider<TransactionManagerCustomizers> transactionManagerCustomizers) {
JdbcTransactionManager transactionManager = new JdbcTransactionManager(dataSource);
DataSourceTransactionManager transactionManager = createTransactionManager(environment, dataSource);
transactionManagerCustomizers.ifAvailable((customizers) -> customizers.customize(transactionManager));
return transactionManager;
}
private DataSourceTransactionManager createTransactionManager(Environment environment, DataSource dataSource) {
return environment.getProperty("spring.dao.exceptiontranslation.enable", Boolean.class, Boolean.TRUE)
? new JdbcTransactionManager(dataSource) : new DataSourceTransactionManager(dataSource);
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
@ -25,6 +25,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.support.JdbcTransactionManager;
import org.springframework.transaction.TransactionManager;
@ -83,6 +84,29 @@ class DataSourceTransactionManagerAutoConfigurationTests {
.hasBean("myTransactionManager"));
}
@Test // gh-24321
void transactionManagerWithDaoExceptionTranslationDisabled() {
this.contextRunner.withConfiguration(AutoConfigurations.of(DataSourceAutoConfiguration.class))
.withPropertyValues("spring.dao.exceptiontranslation.enable=false")
.run((context) -> assertThat(context.getBean(TransactionManager.class))
.isExactlyInstanceOf(DataSourceTransactionManager.class));
}
@Test // gh-24321
void transactionManagerWithDaoExceptionTranslationEnabled() {
this.contextRunner.withConfiguration(AutoConfigurations.of(DataSourceAutoConfiguration.class))
.withPropertyValues("spring.dao.exceptiontranslation.enable=true")
.run((context) -> assertThat(context.getBean(TransactionManager.class))
.isExactlyInstanceOf(JdbcTransactionManager.class));
}
@Test // gh-24321
void transactionManagerWithDaoExceptionTranslationDefault() {
this.contextRunner.withConfiguration(AutoConfigurations.of(DataSourceAutoConfiguration.class))
.run((context) -> assertThat(context.getBean(TransactionManager.class))
.isExactlyInstanceOf(JdbcTransactionManager.class));
}
@Test
void transactionWithMultipleDataSourcesIsNotConfigured() {
this.contextRunner.withUserConfiguration(MultiDataSourceConfiguration.class)