6

I'm aware that H2 has a boolean property/setting called DATABASE_TO_UPPER, which you can set at least in the connection URL, as in: ;DATABASE_TO_UPPER=false

I’d like to set this to false, but in my Spring Boot app, I don’t explicitly have a H2 connection URL anywhere. Implicitly there sure is a connection URL though, as I can see in the logs:

o.s.j.d.e.EmbeddedDatabaseFactory: Shutting down embedded database: 
url='jdbc:h2:mem:2fb4805b-f927-49b3-a786-2a2cac440f44;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false'

So the question is, what's the easiest way to tell H2 to disable DATABASE_TO_UPPER in this scenario? Can I do it in code when creating the H2 datasource with EmbeddedDatabaseBuilder (see below)? Or in application properties maybe?

This is how the H2 database is explicitly initialised in code:

@Configuration
@EnableTransactionManagement
public class DataSourceConfig {

    @Bean
    public DataSource devDataSource() {
        return new EmbeddedDatabaseBuilder()
                .generateUniqueName(true)
                .setType(EmbeddedDatabaseType.H2)
                .setScriptEncoding("UTF-8")
                .ignoreFailedDrops(true)
                .addScripts("db/init.sql", "db/schema.sql", "db/test_data.sql")
                .build();
    }

}

Also, I'm telling JPA/Hibernate not to auto-generate embedded database (without this there was an issue that two in-memory databases were launched):

spring.jpa.generate-ddl=false
spring.jpa.hibernate.ddl-auto=none
Community
  • 1
  • 1
Jonik
  • 80,077
  • 70
  • 264
  • 372

2 Answers2

7

You can't w\ the generateUniqueName, but if you call setName("testdb;DATABASE_TO_UPPER=false") you can add parameters. I doubt this is officially supported, but it worked for me.

The spring code that generates the connection url is like this: String.format("jdbc:h2:mem:%s;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false", databaseName)

TJ Singleton
  • 750
  • 6
  • 12
1

You may want abandon using explicit creation via EmbeddedDatabaseBuilder. Spring Boot creates H2 instance automatically based on configuration. So I would try this in application.properties:

spring.datasource.url=jdbc:h2:file:~/testdb;DATABASE_TO_UPPER=false
luboskrnac
  • 23,973
  • 10
  • 81
  • 92