The DataSourceAutoConfiguration
does not offer a way to disable the implicit creation of a DataSource
if a suitable database is found. This can be undesired if for example HSQLDB is on the class path for other purposes but the primary data source should always be configured explicitly and yield an error if missing.
A simple property should allow to disable the creation of the implicit embedded data source.
Comment From: snicoll
This can be undesired if for example HSQLDB is on the class path for other purposes but the primary data source should always be configured explicitly and yield an error if missing.
Thanks for the report but I am not sure I understood that. What primary data source? Can we take a step back and can you please describe the use case that led you to report this?
Comment From: raphw
Sure, we have an application with Hyper SQL on the dependency path. In production, we are using Postgres which is injected via OpenShift properties.
In one environment, we forgot to set these (vault) properties and Spring Boot fell back to configuring a DataSource using Hyper SQL. This caused the component to start and (partially) function without persisting any data when the pod was killed.
We cannot remove Hyper SQL due to internal use outside of Spring but we'd like to make sure Spring never creates a datasource of it but fails to start.
Therefore I'd like to disable the implicit data source creation using a property.
Comment From: wilkinsona
This for the additional details, @raphw. It's not pretty, but here's a workaround that I think does what you want:
@Bean
public static BeanPostProcessor disableEmbeddedDatabaseSupport() {
return new BeanPostProcessor() {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof DataSourceProperties) {
Field embeddedDatabaseConnection = ReflectionUtils.findField(DataSourceProperties.class,
"embeddedDatabaseConnection");
ReflectionUtils.makeAccessible(embeddedDatabaseConnection);
ReflectionUtils.setField(embeddedDatabaseConnection, bean, EmbeddedDatabaseConnection.NONE);
}
return bean;
}
};
}
Comment From: wilkinsona
A property that controls the embedded database resolution may make sense here. It can default to CLASSPATH
or similar (the current behaviour) while also having an option of NONE
that would turn it off. Alternatively, we could allow the embeddedDatabaseConnection
itself to be set via properties. This would also allow a specific embedded database to be used when there's more than one on the classpath.
Comment From: raphw
Thanks, I already implemented a workaround that disabled the auto configuration altogether in certain scenarios, but as you say: it's not pretty and I'd love to have a simple way to configure this. I think your suggestion makes sense, this way I can just set this property to NONE and it's clear to any reader of the configuration what it does compared to my current post processor approach. Thanks for considering it as a feature!
Comment From: snicoll
+1 I like the property to control the fallback behaviour if no JDBC information is provided by the user.
Comment From: wilkinsona
Setting spring.datasource.embedded-database-connection: none
will now disable auto-configuration of an embedded database. Setting it to any of the other values of the EmbeddedDatabaseConnection
enum will control which embedded database is used rather than it being determined by the classpath.