Search code examples
javaspringspring-booth2embedded-database

Spring H2 embedded database


I want to create an in-memory database populated with test data for fast testing, so I declare this bean in my configuration file, but I also want so set this properties:

MODE=MySQL
DB_CLOSE_ON_EXIT=FALSE

but I don't know where to do it

@Bean
public DataSource dataSource(){
    return
        (new EmbeddedDatabaseBuilder())
        .setType(EmbeddedDatabaseType.H2) //.H2 
        .addScript("classpath:db/H2.schema.sql")
        .addScript("classpath:db/H2.data.sql")
        .build();
}

Solution

  • try this

    @Bean
    public DataSource dataSource(){
        return
            new EmbeddedDatabaseBuilder()
            .setType(EmbeddedDatabaseType.H2)
            .setName("testDB;DB_CLOSE_ON_EXIT=FALSE;MODE=MySQL") 
            .addScript("classpath:db/H2.schema.sql")
            .addScript("classpath:db/H2.data.sql")
            .build();
    }