From a3a1fe97748649b3ce5f78a8d02a1b9d89ea904e Mon Sep 17 00:00:00 2001 From: Rupert Madden-Abbott Date: Wed, 18 Apr 2018 19:22:44 +0100 Subject: [PATCH 1/2] Switch off auto-configuration with multiple DataSource beans. --- .../boot/autoconfigure/jdbc/DataSourceAutoConfiguration.java | 2 ++ .../jdbc/DataSourceTransactionManagerAutoConfiguration.java | 1 + 2 files changed, 3 insertions(+) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration.java index 3f0ba38045d6..1856223adf03 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration.java @@ -26,6 +26,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate; import org.springframework.boot.autoconfigure.condition.SpringBootCondition; import org.springframework.boot.autoconfigure.jdbc.metadata.DataSourcePoolMetadataProvidersConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; @@ -49,6 +50,7 @@ */ @Configuration @ConditionalOnClass({ DataSource.class, EmbeddedDatabaseType.class }) +@ConditionalOnSingleCandidate(DataSource.class) @EnableConfigurationProperties(DataSourceProperties.class) @Import({ DataSourcePoolMetadataProvidersConfiguration.class, DataSourceInitializationConfiguration.class }) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceTransactionManagerAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceTransactionManagerAutoConfiguration.java index e37281232614..8d0302027aea 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceTransactionManagerAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceTransactionManagerAutoConfiguration.java @@ -44,6 +44,7 @@ */ @Configuration @ConditionalOnClass({ JdbcTemplate.class, PlatformTransactionManager.class }) +@ConditionalOnSingleCandidate(DataSource.class) @AutoConfigureOrder(Ordered.LOWEST_PRECEDENCE) @EnableConfigurationProperties(DataSourceProperties.class) public class DataSourceTransactionManagerAutoConfiguration { From 9bbadcd59e874179be5929801d8ce3b8139c7e61 Mon Sep 17 00:00:00 2001 From: Rupert Madden-Abbott Date: Wed, 18 Apr 2018 19:22:57 +0100 Subject: [PATCH 2/2] Update documentation. --- .../src/main/asciidoc/howto.adoc | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc index 32bfcb7edcca..373795f0bbbc 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc @@ -1701,13 +1701,15 @@ class for more details. [[howto-two-datasources]] === Configure Two DataSources -If you need to configure multiple data sources, you can apply the same tricks that are -described in the previous section. You must, however, mark one of the `DataSource` -instances as `@Primary`, because various auto-configurations down the road expect to be -able to get one by type. +`DataSourceAutoConfiguration` and `DataSourceTransactionManagerAutoConfiguration` and both expect +to be able to locate a `DataSource` by type and will be unable to do so if you configure multiple +data sources. In this case these pieces of auto-configuration will back off completely. -If you create your own `DataSource`, the auto-configuration backs off. In the following -example, we provide the _exact_ same feature set as the auto-configuration provides +You may mark one of the `DataSource` instances as `@Primary` to allow this auto-configuration to +apply for this instance only. You then need to apply the same configuration manually to your other +data sources if desired. + +In the following example, we provide the _exact_ same feature set as the auto-configuration provides on the primary data source: [source,java,indent=0,subs="verbatim,quotes,attributes"] @@ -1715,7 +1717,7 @@ on the primary data source: include::{code-examples}/jdbc/SimpleTwoDataSourcesExample.java[tag=configuration] ---- -TIP: `firstDataSourceProperties` has to be flagged as `@Primary` so that the database +TIP: `firstDataSourceProperties` has been marked as `@Primary` so that the database initializer feature uses your copy (if you use the initializer). Both data sources are also bound for advanced customizations. For instance, you could