Skip to content

Commit

Permalink
Do not fail if "spring.datasource.url" cannot be resolved
Browse files Browse the repository at this point in the history
Previously, a condition checked the value of "spring.datasource.url" to
determine if an embedded database has to be created as a fallback. When
the value is set with an unresolved placeholder, this fails even if
the DataSource is going to created by another mean ultimately.

This commit makes a more conservative check by only checking the
presence of the property rather than its value.

Closes gh-20438
  • Loading branch information
snicoll committed Mar 25, 2020
1 parent 9a5ffb7 commit 1835323
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import org.springframework.context.annotation.Import;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.util.StringUtils;

/**
* {@link EnableAutoConfiguration Auto-configuration} for {@link DataSource}.
Expand Down Expand Up @@ -129,14 +128,16 @@ private ClassLoader getDataSourceClassLoader(ConditionContext context) {
*/
static class EmbeddedDatabaseCondition extends SpringBootCondition {

private static final String DATASOURCE_URL_PROPERTY = "spring.datasource.url";

private final SpringBootCondition pooledCondition = new PooledDataSourceCondition();

@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
ConditionMessage.Builder message = ConditionMessage.forCondition("EmbeddedDataSource");
String url = context.getEnvironment().getProperty("spring.datasource.url");
if (StringUtils.hasText(url)) {
return ConditionOutcome.noMatch(message.found("explicit url").items(url));
boolean hasDatasourceUrl = context.getEnvironment().containsProperty(DATASOURCE_URL_PROPERTY);
if (hasDatasourceUrl) {
return ConditionOutcome.noMatch(message.because(DATASOURCE_URL_PROPERTY + " is set"));
}
if (anyMatches(context, metadata, this.pooledCondition)) {
return ConditionOutcome.noMatch(message.foundExactly("supported pooled data source"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ public void testDefaultDataSourceCanBeOverridden() {
.run((context) -> assertThat(context).getBean(DataSource.class).isInstanceOf(BasicDataSource.class));
}

@Test
public void whenThereIsAUserProvidedDataSourceAnUnresolvablePlaceholderDoesNotCauseAProblem() {
this.contextRunner.withUserConfiguration(TestDataSourceConfiguration.class)
.withPropertyValues("spring.datasource.url:${UNRESOLVABLE_PLACEHOLDER}")
.run((context) -> assertThat(context).getBean(DataSource.class).isInstanceOf(BasicDataSource.class));
}

@Test
public void testDataSourceIsInitializedEarly() {
this.contextRunner.withUserConfiguration(TestInitializedDataSourceConfiguration.class)
Expand Down

0 comments on commit 1835323

Please sign in to comment.