0

I am working on my Spring-Boot-Application that is using a database. I want to give the user the option to use an embedded database (for quick testing) or a hard-drive-based database (for long term usage). My way is to add a bool flag to the application.properties, read this in the config and create the wanted DataSource.

@Bean
public DataSource dataSource() {    
    if (embedded) {
        EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
        EmbeddedDatabase db = builder
                .setType(EmbeddedDatabaseType.H2)
                .addScript("db.sql")
                .build();
        return db;
    } else {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(databaseURL);
        dataSource.setUsername(databaseUser);
        dataSource.setPassword(databasePassword);
        return dataSource;
    }
}   

This is working just fine. My problem right now is that I want to persist the embedded-database between sessions, and load its content again if the application is starting again (if the user used embedded database before, and is using it again so he isnt loosing his data)

What do I need to add to my code to make the database create snapshots and use them again if starting up?

Greetz and thanks for your help, Patrick

1 Answer 1

1

You configure it though connection URL.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for you answer! Sadly I dont see a way to configure the connection url in the EmbeddedDataBaseBuilder

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.