Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable H2's DATABASE_TO_UPPER in Spring Boot, without explicit connection URL

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
like image 276
Jonik Avatar asked Nov 09 '15 10:11

Jonik


People also ask

How do I make my H2 database persistent?

Persist the data in H2 Database If we want to persist the data in the H2 database, we should store data in a file. To achieve the same, we need to change the datasource URL property. In the above property, the sampledata is a file name.

How do I run H2 database locally?

Click Windows → type H2 Console → Click H2 console icon. Connect to the URL http://localhost:8082. At the time of connecting, the H2 database will ask for database registration as shown in the following screenshot.

What is JDBC H2 MEM?

This means the database to be opened is private. In this case, the database URL is jdbc:h2:mem: Opening two connections within the same virtual machine means opening two different (private) databases. Sometimes multiple connections to the same in-memory database are required.

Is H2 database case sensitive?

Text comparison in MySQL is case insensitive by default, while in H2 it is case sensitive (as in most other databases). H2 does support case insensitive text comparison, but it needs to be set separately, using SET IGNORECASE TRUE. This affects comparison using =, LIKE, REGEXP.


2 Answers

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)

like image 122
TJ Singleton Avatar answered Oct 18 '22 18:10

TJ Singleton


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
like image 39
luboskrnac Avatar answered Oct 18 '22 19:10

luboskrnac