From 2147976c178a4eab7ca02ba99cfb53d767e3d8f1 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 18 Feb 2020 16:27:31 +0100 Subject: [PATCH] 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 --- .../jdbc/DataSourceAutoConfiguration.java | 7 ++++++- .../DataSourceAutoConfigurationTests.java | 20 ++++++++++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) 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 7cf6a066957..8ccaa5c2707 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,6 +38,7 @@ import org.springframework.context.annotation.Configuration; 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}. @@ -133,6 +134,10 @@ public class DataSourceAutoConfiguration { @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)); + } if (anyMatches(context, metadata, this.pooledCondition)) { return ConditionOutcome.noMatch(message.foundExactly("supported pooled data source")); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfigurationTests.java index 6eb3a5a8a2c..a7815302b9e 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,6 +27,7 @@ import java.util.List; import java.util.Properties; import java.util.Random; import java.util.function.Consumer; +import java.util.function.Function; import java.util.logging.Logger; import javax.sql.DataSource; @@ -143,15 +144,19 @@ public class DataSourceAutoConfigurationTests { }); } + @Test + public void dataSourceWhenNoConnectionPoolsAreAvailableWithUrlDoesNotCreateDataSource() { + this.contextRunner.with(hideConnectionPools()) + .run((context) -> assertThat(context).doesNotHaveBean(DataSource.class)); + } + /** * This test makes sure that if no supported data source is present, a datasource is * still created if "spring.datasource.type" is present. */ @Test - public void explicitTypeNoSupportedDataSource() { - this.contextRunner - .withClassLoader(new FilteredClassLoader("org.apache.tomcat", "com.zaxxer.hikari", - "org.apache.commons.dbcp", "org.apache.commons.dbcp2")) + public void dataSourceWhenNoConnectionPoolsAreAvailableWithUrlAndTypeCreatesDataSource() { + this.contextRunner.with(hideConnectionPools()) .withPropertyValues("spring.datasource.driverClassName:org.hsqldb.jdbcDriver", "spring.datasource.url:jdbc:hsqldb:mem:testdb", "spring.datasource.type:" + SimpleDriverDataSource.class.getName()) @@ -197,6 +202,11 @@ public class DataSourceAutoConfigurationTests { .isTrue()); } + private static Function hideConnectionPools() { + return (runner) -> runner.withClassLoader(new FilteredClassLoader("org.apache.tomcat", "com.zaxxer.hikari", + "org.apache.commons.dbcp", "org.apache.commons.dbcp2")); + } + private void assertDataSource(Class expectedType, List hiddenPackages, Consumer consumer) { FilteredClassLoader classLoader = new FilteredClassLoader(StringUtils.toStringArray(hiddenPackages));