commit
21a08ed37d
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.core.Ordered;
|
import org.springframework.core.Ordered;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||||
import org.springframework.jdbc.support.JdbcTransactionManager;
|
import org.springframework.jdbc.support.JdbcTransactionManager;
|
||||||
import org.springframework.transaction.TransactionManager;
|
import org.springframework.transaction.TransactionManager;
|
||||||
|
|
||||||
|
|
@ -54,13 +56,18 @@ public class DataSourceTransactionManagerAutoConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean(TransactionManager.class)
|
@ConditionalOnMissingBean(TransactionManager.class)
|
||||||
JdbcTransactionManager transactionManager(DataSource dataSource,
|
DataSourceTransactionManager transactionManager(Environment environment, DataSource dataSource,
|
||||||
ObjectProvider<TransactionManagerCustomizers> transactionManagerCustomizers) {
|
ObjectProvider<TransactionManagerCustomizers> transactionManagerCustomizers) {
|
||||||
JdbcTransactionManager transactionManager = new JdbcTransactionManager(dataSource);
|
DataSourceTransactionManager transactionManager = createTransactionManager(environment, dataSource);
|
||||||
transactionManagerCustomizers.ifAvailable((customizers) -> customizers.customize(transactionManager));
|
transactionManagerCustomizers.ifAvailable((customizers) -> customizers.customize(transactionManager));
|
||||||
return 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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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.AutoConfigurations;
|
||||||
import org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration;
|
import org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration;
|
||||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||||
|
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||||
import org.springframework.jdbc.support.JdbcTransactionManager;
|
import org.springframework.jdbc.support.JdbcTransactionManager;
|
||||||
import org.springframework.transaction.TransactionManager;
|
import org.springframework.transaction.TransactionManager;
|
||||||
|
|
||||||
|
|
@ -83,6 +84,29 @@ class DataSourceTransactionManagerAutoConfigurationTests {
|
||||||
.hasBean("myTransactionManager"));
|
.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
|
@Test
|
||||||
void transactionWithMultipleDataSourcesIsNotConfigured() {
|
void transactionWithMultipleDataSourcesIsNotConfigured() {
|
||||||
this.contextRunner.withUserConfiguration(MultiDataSourceConfiguration.class)
|
this.contextRunner.withUserConfiguration(MultiDataSourceConfiguration.class)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue