【问题标题】:H2 in memory database not working after update to SpringBoot 2.4.0更新到 Spring Boot 2.4.0 后,内存数据库中的 H2 无法正常工作
【发布时间】:2021-03-03 15:46:26
【问题描述】:

我有一个 SpringBoot 应用程序,它使用 H2 内存数据库进行集成测试。如果我在 versino 2.3.4.RELEASE 中使用 SpringBoot,则测试有效。如果我升级到 2.4.0 并出现以下错误,它们会失败:

java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'liquibase' defined in class path resource [org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration$LiquibaseConfiguration.class]: Invocation of init method failed; nested exception is java.lang.RuntimeException: Driver com.microsoft.sqlserver.jdbc.SQLServerDriver claims to not accept jdbcUrl, jdbc:h2:mem:integrationTestDB;DB_CLOSE_DELAY=-1;MODE=MSSQLServer;INIT=CREATE SCHEMA IF NOT EXISTS dbo\;SET SCHEMA dbo
Caused by: java.lang.RuntimeException: Driver com.microsoft.sqlserver.jdbc.SQLServerDriver claims to not accept jdbcUrl, jdbc:h2:mem:integrationTestDB;DB_CLOSE_DELAY=-1;MODE=MSSQLServer;INIT=CREATE SCHEMA IF NOT EXISTS dbo\;SET SCHEMA dbo

这是我的 integration-test.properties,供测试使用:

spring.datasource.url=jdbc:h2:mem:integrationTestDB;\
  DB_CLOSE_DELAY=-1;\
  MODE=MSSQLServer;\
  INIT=CREATE SCHEMA IF NOT EXISTS dbo\\;SET SCHEMA dbo
spring.datasource.driver=org.h2.Driver
spring.datasource.hikari.driver-class-name=org.h2.Driver

hibernate.dialect=org.hibernate.dialect.SQLServerDialect
hibernate.hbm2ddl.auto=none

spring.datasource.username=sa
spring.datasource.password=

spring.liquibase.user=sa
spring.liquibase.password=

H2 的版本是 1.4.200。 成功与失败的区别在于pom的父元素中的SpringBoot版本:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.0</version>
    </parent>

liquibase 版本从 3.8.9 更改为 3.10.3。我将其配置为保持在 3.8.9,但这没有帮助。

我阅读了发行说明,找到了关于嵌入式数据库检测的部分:https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.4-Release-Notes#embedded-database-detection

但是添加

spring.datasource.initialization-mode=always

属性也没有帮助。

我记得上次找了合适的datasource url花了一些时间,但是用google找不到任何新的线索。

你能给我一个提示,是什么导致了这个问题?

亲切的问候, 乌尔里希

【问题讨论】:

  • 这一定是 liquibase 或你的其他配置的问题,因为我可以用你的配置做一个最小的例子。你可以发布你的pom吗?
  • 我将发布我的 pom 的精简版。
  • 我创建了一个存储库,其中包含我项目的一个非常简化的版本。我离开了基本的库和配置。问题是可以重现的:github.com/UlrichViefhaus/spring-boot-h2-liquibase-example

标签: java sql-server spring-boot h2 liquibase


【解决方案1】:

您的属性包含 H2 和 SQL Server 配置的混合。例如,您为 DataSource 配置了 H2 JDBC URL,但将 Hibernate 配置为使用 SQLServerDialect。该异常表明在尝试初始化 Liquibase 时正在使用 SQL Server 的 JDBC 驱动程序。在我看来,您正在尝试在集成测试中使用 H2,替换部署应用时使用的 SQL Server。

有一个new spring.liquibase.driver-class-name property in 2.4.0。未设置时,它会回退到使用spring.datasource.driver-class-name。没有spring.datasource.driver 属性,所以尝试替换以下两行:

spring.datasource.driver=org.h2.Driver
spring.datasource.hikari.driver-class-name=org.h2.Driver

下面一行:

spring.datasource.driver-class-name=org.h2.Driver

【讨论】:

  • 你是对的。那行得通。我没有尝试,因为 IntelliJ IDEA 将其标记为错误并告诉我:“无法解析类‘驱动程序’”。但它有效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多