h2内存数据库不工作

yqyhoc1h  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(443)

我有一个springboot应用程序,使用内存数据库进行集成测试。如果在版本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-数据库检测
但是添加

spring.datasource.initialization-mode=always

对物业也没什么帮助。
我记得上次找到正确的数据源url花了一些时间,但是我在google上找不到任何新的线索。
你能给我一个提示吗,是什么引起这个问题的?
谨致问候,乌尔里希

yc0p9oo0

yc0p9oo01#

您的属性包含h2和sqlserver配置的混合。例如,您为数据源配置了一个h2jdbc url,但将hibernate配置为使用 SQLServerDialect . 异常显示在尝试初始化liquibase时正在使用sql server的jdbc驱动程序。在我看来,您正在尝试在集成测试中使用h2,以取代部署应用程序时使用的sql server。
有一个新的 spring.liquibase.driver-class-name 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

相关问题