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:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user