Skip to content

Only apply autoconfiguration that injects DataSource by type, if there is a single or primary DataSource #12914

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -49,6 +50,7 @@
*/
@Configuration
@ConditionalOnClass({ DataSource.class, EmbeddedDatabaseType.class })
@ConditionalOnSingleCandidate(DataSource.class)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure this makes sense as a few lines later there is @ConditionalOnMissingBean({ DataSource.class, XADataSource.class }) which will now never happen.

@EnableConfigurationProperties(DataSourceProperties.class)
@Import({ DataSourcePoolMetadataProvidersConfiguration.class,
DataSourceInitializationConfiguration.class })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
*/
@Configuration
@ConditionalOnClass({ JdbcTemplate.class, PlatformTransactionManager.class })
@ConditionalOnSingleCandidate(DataSource.class)
@AutoConfigureOrder(Ordered.LOWEST_PRECEDENCE)
@EnableConfigurationProperties(DataSourceProperties.class)
public class DataSourceTransactionManagerAutoConfiguration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1701,21 +1701,23 @@ 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"]
----
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
Expand Down