New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add property to control the auto-configuration of a DataSource for an embedded database #23412
Comments
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? |
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. |
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;
}
};
} |
A property that controls the embedded database resolution may make sense here. It can default to |
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! |
+1 I like the property to control the fallback behaviour if no JDBC information is provided by the user. |
Setting |
The
DataSourceAutoConfiguration
does not offer a way to disable the implicit creation of aDataSource
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.
The text was updated successfully, but these errors were encountered: