Skip to content

Commit

Permalink
Respect spring.dao.exceptiontranslation setting
Browse files Browse the repository at this point in the history
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
  • Loading branch information
philwebb committed Jan 11, 2021
1 parent cff3e4c commit 53a6fa2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
@@ -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.
Expand Down Expand Up @@ -29,7 +29,9 @@
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;

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

@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);
}

}

}
@@ -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.
Expand All @@ -25,6 +25,7 @@
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;

Expand Down Expand Up @@ -83,6 +84,29 @@ void transactionManagerWithExistingTransactionManagerIsNotOverridden() {
.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)
Expand Down

0 comments on commit 53a6fa2

Please sign in to comment.