简体   繁体   English

Spring 引导 - 摆脱 Hikari 数据源并使用 H2

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

I would like to use an H2 database in a Spring Boot application.我想在 Spring Boot 应用程序中使用 H2 数据库。

This is what I have in application.properties:这是我在 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:这是 build.gradle 文件中的相关行:

    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.当我检查 H2 数据库的连接时,一切都很好。

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.但是尽管如此,当我将新实体发布到应用程序中时,我看不到 H2 数据库中没有创建任何内容,而是在后台使用了 Hikari 数据源,正如我从日志中看到的那样。

Why is Spring ignoring the application.properties file and not using the H2 data source?为什么Spring忽略application.properties文件,不使用H2数据源? How can I force it to use the H2 data source?如何强制它使用 H2 数据源?

This is Spring Boot 2.3.4.RELEASE.这是 Spring Boot 2.3.4.RELEASE。

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.我猜你有多个数据源和一个除 H2 之外的数据源被默认自动连接。因为 Hirakicp 是一个连接池而不是数据库。 U need not get rid of Hirakicp rather set h2 datasources as primary你不需要摆脱 Hirakicp 而是将 h2 数据源设置为主

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.如果您配置了多个数据源 bean,那么 spring 会自动装配其他数据源以用作默认源。

using @Primary annotation while declaring H2 Datasource bean should solve this.在声明 H2 数据源 bean 时使用 @Primary 注释应该可以解决这个问题。 Using this annotation will force spring to autowire the datult to h2 datasource.使用此注释将强制 spring 将数据自动连接到 h2 数据源。

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.如果您没有为 H2 声明数据源 bean 但有其他数据源 bean,则需要声明 h2 bean 并使用 @primary 注释将其设置为主。

note - Hirakicp is a db connection object not a db.注意 - Hirakicp 是数据库连接 object 而不是数据库。

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.我认为这是因为 SpringBoot 应用程序创建了自己的内存嵌入式数据库,而您的数据库客户端创建了自己的内存嵌入式数据库。

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.您只需要更改数据库 url 使其成为基于文件的数据库。

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.如果您需要继续使用内存方法,您可以通过 tcp 连接到它,以便您的应用程序和客户端可以使用相同的数据库。

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? Why is Spring ignoring the application.properties file and not using the H2 data source 是什么意思? Hikari is connection pool, not a datasource. Hikari是连接池,不是数据源。 If you would like to provide your own datasource, you have to inject a bean with the configuration of your interest.如果您想提供自己的数据源,则必须注入一个具有您感兴趣的配置的 bean。

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在此处查看有关连接池的更多信息: 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但是,如果它是您不喜欢的日志记录,您可以使用org.zaxxer.hikari=error将记录器设置为错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM