Skip to content
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

Closed
raphw opened this issue Sep 19, 2020 · 7 comments
Assignees
Labels
theme: datasource Issues relating to data sources type: enhancement A general enhancement
Milestone

Comments

@raphw
Copy link

raphw commented Sep 19, 2020

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Sep 19, 2020
@snicoll snicoll added the for: team-attention An issue we'd like other members of the team to review label Sep 22, 2020
@snicoll
Copy link
Member

snicoll commented Sep 22, 2020

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?

@snicoll snicoll added status: waiting-for-feedback We need additional information before we can continue and removed for: team-attention An issue we'd like other members of the team to review labels Sep 22, 2020
@raphw
Copy link
Author

raphw commented Sep 22, 2020

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.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Sep 22, 2020
@wilkinsona
Copy link
Member

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;
        }

    };
}

@wilkinsona
Copy link
Member

wilkinsona commented Nov 11, 2020

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.

@wilkinsona wilkinsona added type: enhancement A general enhancement for: team-attention An issue we'd like other members of the team to review and removed status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged labels Nov 11, 2020
@raphw
Copy link
Author

raphw commented Nov 11, 2020

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!

@snicoll
Copy link
Member

snicoll commented Nov 12, 2020

+1 I like the property to control the fallback behaviour if no JDBC information is provided by the user.

@philwebb philwebb added this to the 2.5.x milestone Nov 18, 2020
@philwebb philwebb added status: pending-design-work Needs design work before any code can be developed and removed for: team-attention An issue we'd like other members of the team to review labels Nov 18, 2020
@wilkinsona wilkinsona added the theme: datasource Issues relating to data sources label Feb 11, 2021
@wilkinsona wilkinsona changed the title Add property to disable auto generation of embeded DataSource Add property to disable auto generation of embedded DataSource Apr 13, 2021
@wilkinsona wilkinsona removed the status: pending-design-work Needs design work before any code can be developed label Apr 13, 2021
@wilkinsona wilkinsona self-assigned this Apr 13, 2021
@wilkinsona wilkinsona changed the title Add property to disable auto generation of embedded DataSource Add property to control the auto-configuration of a DataSource for an embedded database Apr 13, 2021
@wilkinsona wilkinsona modified the milestones: 2.5.x, 2.5.0-RC1 Apr 13, 2021
@wilkinsona
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
theme: datasource Issues relating to data sources type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

5 participants