Skip to content

Commit 2147976

Browse files
committed
Do not fallback to embedded configuration if a datasource url is set
This commit makes sure that a fallback embedded datasource is not created if no suitable connection pool is found and an url has been explicitly registered. This is consistent with EmbeddedDataSourceConfiguration as it is using EmbeddedDatabaseBuilder behind the scenes and the latter does not honour the configured URL anyway. Closes gh-19192
1 parent c8907d4 commit 2147976

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,6 +38,7 @@
3838
import org.springframework.context.annotation.Import;
3939
import org.springframework.core.type.AnnotatedTypeMetadata;
4040
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
41+
import org.springframework.util.StringUtils;
4142

4243
/**
4344
* {@link EnableAutoConfiguration Auto-configuration} for {@link DataSource}.
@@ -133,6 +134,10 @@ static class EmbeddedDatabaseCondition extends SpringBootCondition {
133134
@Override
134135
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
135136
ConditionMessage.Builder message = ConditionMessage.forCondition("EmbeddedDataSource");
137+
String url = context.getEnvironment().getProperty("spring.datasource.url");
138+
if (StringUtils.hasText(url)) {
139+
return ConditionOutcome.noMatch(message.found("explicit url").items(url));
140+
}
136141
if (anyMatches(context, metadata, this.pooledCondition)) {
137142
return ConditionOutcome.noMatch(message.foundExactly("supported pooled data source"));
138143
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfigurationTests.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@
2727
import java.util.Properties;
2828
import java.util.Random;
2929
import java.util.function.Consumer;
30+
import java.util.function.Function;
3031
import java.util.logging.Logger;
3132

3233
import javax.sql.DataSource;
@@ -143,15 +144,19 @@ public void testEmbeddedTypeDefaultsUsername() {
143144
});
144145
}
145146

147+
@Test
148+
public void dataSourceWhenNoConnectionPoolsAreAvailableWithUrlDoesNotCreateDataSource() {
149+
this.contextRunner.with(hideConnectionPools())
150+
.run((context) -> assertThat(context).doesNotHaveBean(DataSource.class));
151+
}
152+
146153
/**
147154
* This test makes sure that if no supported data source is present, a datasource is
148155
* still created if "spring.datasource.type" is present.
149156
*/
150157
@Test
151-
public void explicitTypeNoSupportedDataSource() {
152-
this.contextRunner
153-
.withClassLoader(new FilteredClassLoader("org.apache.tomcat", "com.zaxxer.hikari",
154-
"org.apache.commons.dbcp", "org.apache.commons.dbcp2"))
158+
public void dataSourceWhenNoConnectionPoolsAreAvailableWithUrlAndTypeCreatesDataSource() {
159+
this.contextRunner.with(hideConnectionPools())
155160
.withPropertyValues("spring.datasource.driverClassName:org.hsqldb.jdbcDriver",
156161
"spring.datasource.url:jdbc:hsqldb:mem:testdb",
157162
"spring.datasource.type:" + SimpleDriverDataSource.class.getName())
@@ -197,6 +202,11 @@ public void testDataSourceIsInitializedEarly() {
197202
.isTrue());
198203
}
199204

205+
private static Function<ApplicationContextRunner, ApplicationContextRunner> hideConnectionPools() {
206+
return (runner) -> runner.withClassLoader(new FilteredClassLoader("org.apache.tomcat", "com.zaxxer.hikari",
207+
"org.apache.commons.dbcp", "org.apache.commons.dbcp2"));
208+
}
209+
200210
private <T extends DataSource> void assertDataSource(Class<T> expectedType, List<String> hiddenPackages,
201211
Consumer<T> consumer) {
202212
FilteredClassLoader classLoader = new FilteredClassLoader(StringUtils.toStringArray(hiddenPackages));

0 commit comments

Comments
 (0)