Spring Boot – 摆脱 Hikari 数据源,使用 H2

huangapple go评论102阅读模式
英文:

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

问题

以下是翻译好的内容:

我想在一个Spring Boot应用程序中使用H2数据库。

这是我在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

这是build.gradle文件中相关的行:

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

当我检查H2数据库的连接时,一切都正常。

但尽管如此,当我将新实体POST到应用程序时,我在H2数据库中看不到任何创建,而从日志中可以看出,后台实际上使用了Hikari数据源。

为什么Spring忽略application.properties文件并不使用H2数据源?如何强制它使用H2数据源?

这是Spring Boot 2.3.4.RELEASE版本。

英文:

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.

答案1

得分: 1

我猜你有多个数据源,并且除了 H2 之外还有另一个数据源被自动装配为默认数据源。因为 Hirakicp 是一个连接池而不是数据库,所以你不需要摆脱 Hirakicp,而是将 H2 数据源设置为主要数据源。

如果你有多个数据源配置,以下解决方案应该适用:

如果你配置了多个数据源 bean,只是 Spring 自动装配了其他数据源作为默认源。

在声明 H2 数据源 bean 时使用 @Primary 注解应该可以解决这个问题。使用这个注解会强制 Spring 自动装配默认到 H2 数据源。

如果你没有为 H2 声明数据源 bean,但有其他数据源 bean,你需要声明 H2 数据源 bean 并使用 @Primary 注解将其设置为主要数据源。

注意 - Hirakicp 是一个数据库连接对象,而不是数据库本身。

示例代码:

@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 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;

    }

答案2

得分: 0

我认为这是因为SpringBoot应用程序创建了自己的内存嵌入式数据库,而您的数据库客户端也创建了自己的内存嵌入式数据库。

我建议您将数据库从内存更改为文件系统,并查看应用程序和数据库客户端是否显示相同的数据。

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

您只需要更改数据库的URL,使其成为基于文件的数据库。

如果您希望继续使用内存方法,您可以通过TCP连接到它,以便应用程序和客户端都可以使用同一个数据库。

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

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

答案3

得分: 0

什么是“为什么Spring忽略application.properties文件,而不使用H2数据源”的意思?Hikari 是连接池,不是数据源。如果您想要提供自己的数据源,您必须注入一个带有您感兴趣的配置的Bean。

连接池 连接池是维护的数据库连接缓存,以便在将来需要向数据库发出请求时可以重用这些连接。

数据源 是运行报表或获取信息所使用的数据的来源。

在这里查看有关连接池的更多信息:https://www.baeldung.com/java-connection-pooling

但是,如果您不喜欢日志记录,您可以使用 org.zaxxer.hikari=error 将日志记录器设置为错误。

英文:

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

huangapple
  • 本文由 发表于 2020年10月27日 14:44:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/64549392.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定