Spring Boot - Get rid of Hikari Data Source and use H2

Viewed 4907

I would like to use an H2 database in a Spring Boot application.

This is what I have in application.properties:

spring.datasource.name=testdb
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=whydoesthishavetobe
spring.datasource.password=sodifficult
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

This are the relevant lines in the build.gradle file:

    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    runtimeOnly 'com.h2database:h2'

When I check the connection for the H2 database, it's all good.

But despite this, when I POST new entities into the application, I see nothing getting created in the H2 database, and Hikari data source is getting used instead in the background, as I see from the logs.

Why is Spring ignoring the application.properties file and not using the H2 data source? How can I force it to use the H2 data source?

This is Spring Boot 2.3.4.RELEASE.

3 Answers

I guess you have multiple datasources and a datasource other tha H2 is being autowired as default.As Hirakicp is a connection pool not a db. U need not get rid of Hirakicp rather set h2 datasources as primary

The below solution should work if you have multiple datasources ,

If you have multiple data source beans configured, it's just that spring is autowiring other data source to be used as a default source.

using @Primary annotation while declaring H2 Datasource bean should solve this. Using this annotation will force spring to autowire the datult to h2 datasource.

in case you have not declared a datasource bean for H2 but have other datasource beans, you will need to declare the h2 bean and set it as primary using @primary annotation.

note - Hirakicp is a db connection object not a db .

Example -

    @Bean("one")
    public BasicDataSource dataSourceOne() {
        BasicDataSource basicDataSource = new BasicDataSource();
        basicDataSource.setUrl(env.getProperty("spring.a.connectionUrl"));
        basicDataSource.setDriverClassName(env.getProperty("spring.a.DriverClass"));
        basicDataSource.setUsername(env.getProperty("spring.a.username"));
        basicDataSource.setPassword(env.getProperty("spring.a.password"));
        //  basicDataSource.setMaxActive(2);
        return basicDataSource;

    }

    @Primary
    @Bean("two")
    public BasicDataSource dataSourceTwo() {
        BasicDataSource basicDataSource = new BasicDataSource();
        basicDataSource.setUrl(env.getProperty("spring.u.connectionUrl"));
        basicDataSource.setDriverClassName(env.getProperty("spring.u.DriverClass"));
        basicDataSource.setUsername(env.getProperty("spring.u.username"));
        basicDataSource.setPassword(env.getProperty("spring.u.password"));
        return basicDataSource;

    }

I think this is happening because SpringBoot application creates its own in-memory embedded database while your database client creates its own in-memory embedded database.

I suggest you to change the database from in-memory to filesystem and see if the application and your database client shows the same data.

spring.datasource.url=jdbc:h2:file:/data/sample/testdb

You only need to change the database url to make it a file based database.

If you need to proceed with in-memory approach, you can connect to that via tcp so that both your application and client can use the same database.

spring.datasource.url=jdbc:h2:tcp://localhost/mem:testdb

What do you mean by Why is Spring ignoring the application.properties file and not using the H2 data source? Hikari is connection pool, not a datasource. If you would like to provide your own datasource, you have to inject a bean with the configuration of your interest.

Connection pool A connection pool is a cache of database connections maintained so that the connections can be reused when future requests to the database are required.

Datasource Is where data that is being used to run a report or gain information is originating from.

Check here for more on connection pools: https://www.baeldung.com/java-connection-pooling

But, if it the logging that you don't like, you can set the logger to error with org.zaxxer.hikari=error

Related