From 04e441f468ae41e54e8f8a0c3ecc2483a4cdcb72 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 13 Apr 2021 12:48:46 +0100 Subject: [PATCH] Add a config prop for the embedded database connection Previously, the embedded database connection that would be used could only be controlled via the classpath. If multiple embedded database dependencies were present, it wasn't possible to control the one that the auto-configured would use. It also wasn't possible to disable auto-configuration of an embedded database. This commit introduces a new configuration property, spring.datasource.embedded-database-connection. It can be set to one of the values of the EmbeddedDatabaseConnection enum to control the auto-configuration of an embedded database. Setting it to none will disable the auto-configuration and ensure that an external database is used instead. Closes gh-23412 --- .../jdbc/DataSourceProperties.java | 18 ++++++++++++++++-- .../jdbc/DataSourcePropertiesTests.java | 18 ++++++++++++++++++ .../docs/asciidoc/spring-boot-features.adoc | 2 ++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java index 3c86b3830b3..956e605d61a 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java @@ -162,7 +162,11 @@ public class DataSourceProperties implements BeanClassLoaderAware, InitializingB @Deprecated private Charset sqlScriptEncoding; - private EmbeddedDatabaseConnection embeddedDatabaseConnection = EmbeddedDatabaseConnection.NONE; + /** + * Connection details for an embedded database. Defaults to the most suitable embedded + * database that is available on the classpath. + */ + private EmbeddedDatabaseConnection embeddedDatabaseConnection; private Xa xa = new Xa(); @@ -175,7 +179,9 @@ public class DataSourceProperties implements BeanClassLoaderAware, InitializingB @Override public void afterPropertiesSet() throws Exception { - this.embeddedDatabaseConnection = EmbeddedDatabaseConnection.get(this.classLoader); + if (this.embeddedDatabaseConnection == null) { + this.embeddedDatabaseConnection = EmbeddedDatabaseConnection.get(this.classLoader); + } } /** @@ -506,6 +512,14 @@ public class DataSourceProperties implements BeanClassLoaderAware, InitializingB this.sqlScriptEncoding = sqlScriptEncoding; } + public EmbeddedDatabaseConnection getEmbeddedDatabaseConnection() { + return this.embeddedDatabaseConnection; + } + + public void setEmbeddedDatabaseConnection(EmbeddedDatabaseConnection embeddedDatabaseConnection) { + this.embeddedDatabaseConnection = embeddedDatabaseConnection; + } + public ClassLoader getClassLoader() { return this.classLoader; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourcePropertiesTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourcePropertiesTests.java index 623891402c5..b82438dde9f 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourcePropertiesTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourcePropertiesTests.java @@ -69,6 +69,24 @@ class DataSourcePropertiesTests { .isThrownBy(properties::determineUrl).withMessageContaining("Failed to determine suitable jdbc url"); } + @Test + void determineUrlWithSpecificEmbeddedConnection() throws Exception { + DataSourceProperties properties = new DataSourceProperties(); + properties.setGenerateUniqueName(false); + properties.setEmbeddedDatabaseConnection(EmbeddedDatabaseConnection.HSQLDB); + properties.afterPropertiesSet(); + assertThat(properties.determineUrl()).isEqualTo(EmbeddedDatabaseConnection.HSQLDB.getUrl("testdb")); + } + + @Test + void whenEmbeddedConnectionIsNoneAndNoUrlIsConfiguredThenDetermineUrlThrows() throws Exception { + DataSourceProperties properties = new DataSourceProperties(); + properties.setGenerateUniqueName(false); + properties.setEmbeddedDatabaseConnection(EmbeddedDatabaseConnection.NONE); + assertThatExceptionOfType(DataSourceProperties.DataSourceBeanCreationException.class) + .isThrownBy(properties::determineUrl).withMessageContaining("Failed to determine suitable jdbc url"); + } + @Test void determineUrlWithExplicitConfig() throws Exception { DataSourceProperties properties = new DataSourceProperties(); diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc index 6a9a8959f06..e01d927024c 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc @@ -3565,6 +3565,8 @@ TIP: The "`How-to`" section includes a <