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
This commit is contained in:
parent
059c4fbd43
commit
04e441f468
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -3565,6 +3565,8 @@ TIP: The "`How-to`" section includes a <<howto.adoc#howto-database-initializatio
|
|||
Spring Boot can auto-configure embedded https://www.h2database.com[H2], http://hsqldb.org/[HSQL], and https://db.apache.org/derby/[Derby] databases.
|
||||
You need not provide any connection URLs.
|
||||
You need only include a build dependency to the embedded database that you want to use.
|
||||
If there are multiple embedded databases on the classpath, set the configprop:spring.datasource.embedded-database-connection[] configuration property to control which one is used.
|
||||
Setting the property to `none` disables auto-configuration of an embedded database.
|
||||
|
||||
[NOTE]
|
||||
====
|
||||
|
|
|
|||
Loading…
Reference in New Issue