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

Conversation

rupert-madden-abbott
Copy link
Contributor

Currently, an error is thrown if I add multiple DataSources to my context.

There are two workarounds for this issue:

  1. Mark one of the DataSources as @Primary
  2. Exclude all autoconfiguration that wants to inject DataSource by type

It is easy to do 1. but this can be arbitrary decision unless the user knows exactly what auto-configuration is being applied. It can lead to subtle differences in behaviour which can trip you up, especially if you don't understand what you are doing by choosing this option.

It is hard to do 2. without a full knowledge of which auto-configurations. A piece of auto-configuration knows if it wants to inject by type and can foresee that injecting a DataSource by type is likely to trip people up. Therefore, they should make use of ConditionalOnSingleCandidate so they don't trip anybody up. Afterall, if somebody has already added multiple DataSources without into their application, then they probably know what they want to do with them.

This was proposed in #5541 but the commit that closed that ticket did not actually add this annotation to either DataSourceAutoConfiguration nor DataSourceTransactionManagerAutoConfiguration so this PR fixes that and also updates the documentation to reflect this behaviour.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 18, 2018
@@ -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.

@snicoll
Copy link
Member

snicoll commented Apr 19, 2018

@rupert654 I disagree with the analysis and we can't accept such change without additional tests that demonstrate and exercise the conditions you've added.

Please open a separate issue with a sample that we can run that demonstrates the problem you're experiencing.

@snicoll snicoll closed this Apr 19, 2018
@snicoll snicoll added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged labels Apr 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants