Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring H2 embedded database [duplicate]

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();
}
like image 245
Nunyet de Can Calçada Avatar asked Nov 29 '22 22:11

Nunyet de Can Calçada


2 Answers

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();
}
like image 148
pezetem Avatar answered Dec 02 '22 12:12

pezetem


You can try using EmbeddedDatabaseBuilder.setName()

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

Note: I have not tried this myself, but found a clue on this answer

like image 29
qtips Avatar answered Dec 02 '22 11:12

qtips