【问题标题】:Spring Boot 2.0.4 - H2 Database - @SpringBootTest - Failing on Caused by: org.h2.jdbc.JdbcSQLException: Schema "classpath:db/schema.sql" not foundSpring Boot 2.0.4 - H2 数据库 - @SpringBootTest - 失败原因:org.h2.jdbc.JdbcSQLException:找不到架构“classpath:db/schema.sql”
【发布时间】:2019-01-25 04:17:28
【问题描述】:

我已经使用 Spring Boot 2.0.4 构建了一个应用程序并与 JPA 集成。

现在我想用 h2 数据库运行一些测试,但是我无法用 h2 执行 Spring Boot 测试。

例外情况如下所示:

Caused by: org.springframework.jdbc.datasource.init.UncategorizedScriptException: Failed to execute database script; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is org.h2.jdbc.JdbcSQLException: Schema "classpath:db/schema.sql" not found [90079-197]
    at org.springframework.jdbc.datasource.init.DatabasePopulatorUtils.execute(DatabasePopulatorUtils.java:58)
    at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.runScripts(DataSourceInitializer.java:210)
    at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.createSchema(DataSourceInitializer.java:104)
    at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker.afterPropertiesSet(DataSourceInitializerInvoker.java:64)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1758)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1695)
    ... 74 more
Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is org.h2.jdbc.JdbcSQLException: Schema "classpath:db/schema.sql" not found [90079-197]
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:81)
    at org.springframework.jdbc.datasource.init.DatabasePopulatorUtils.execute(DatabasePopulatorUtils.java:46)
    ... 79 more
Caused by: org.h2.jdbc.JdbcSQLException: Schema "classpath:db/schema.sql" not found [90079-197]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:357)
    at org.h2.message.DbException.get(DbException.java:179)
    at org.h2.message.DbException.get(DbException.java:155)
    at org.h2.engine.Database.getSchema(Database.java:1808)
    at org.h2.engine.Session.setCurrentSchemaName(Session.java:1317)
    at org.h2.jdbc.JdbcConnection.setSchema(JdbcConnection.java:1989)
    at com.zaxxer.hikari.pool.PoolBase.setupConnection(PoolBase.java:423)
    at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:370)
    at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:194)
    at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:460)
    at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:534)
    at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115)
    at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112)
    at org.springframework.jdbc.datasource.DataSourceUtils.fetchConnection(DataSourceUtils.java:151)
    at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:115)
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:78)
    ... 80 more

而我的 application-test.properties 是:

spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.jdbc-url=jdbc:h2:mem:test
spring.datasource.schema=classpath:db/schema.sql
spring.datasource.data=classpath:db/data.sql
spring.datasource.initialization-mode=always


spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none
logging.level.org.hibernate.SQL=debug
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

这些异常发生在我执行 schema.sql 时,但 h2 想在 spring.datasource.schema 中指定的名为“classpath:schema.sql”的模式中执行此脚本。

【问题讨论】:

  • 欢迎来到 Stack Overflow!请使用tour 并通读help center,尤其是[how-to-ask] 您最好的选择是进行研究,搜索有关 SO 的相关主题,然后试一试。如果您在进行更多研究和搜索后卡住并且无法摆脱卡住,请发布您的尝试示例 minimal reproducible example 并具体说明您卡在哪里。人们会很乐意提供帮助。

标签: spring-boot spring-data-jpa h2


【解决方案1】:

通过 Not Found 异常很清楚 Spring boot 无法找到 schema.sql 文件。

只需将src/main/resources/db/schema.sql 复制到src/test/resources/db/schema.sql

【讨论】:

  • schema.sql的路径为src/test/resources/db/schema.sql,如果路径错误,则异常为Caused by: org.springframework.boot.context.properties.source.InvalidConfigurationPropertyValueException: Property spring.datasource.schema with value 'class path resource [db/schema.sql]' is invalid: The specified resource does not exist.
【解决方案2】:

您的属性应如下所示:

spring.datasource.schema=db/schema.sql

在 DataSourceInitializer 类中你可以看到它是如何使用的

public boolean createSchema() {
    List<Resource> scripts = getScripts("spring.datasource.schema",
            this.properties.getSchema(), "schema");
    if (!scripts.isEmpty()) {
        if (!isEnabled()) {
            logger.debug("Initialization disabled (not running DDL scripts)");
            return false;
        }
        String username = this.properties.getSchemaUsername();
        String password = this.properties.getSchemaPassword();
        runScripts(scripts, username, password);
    }
    return !scripts.isEmpty();
}

getScripts 方法添加类路径字符串:

private List<Resource> getScripts(String propertyName, List<String> resources,
        String fallback) {
    if (resources != null) {
        return getResources(propertyName, resources, true);
    }
    String platform = this.properties.getPlatform();
    List<String> fallbackResources = new ArrayList<>();
    fallbackResources.add("classpath*:" + fallback + "-" + platform + ".sql");
    fallbackResources.add("classpath*:" + fallback + ".sql");
    return getResources(propertyName, fallbackResources, false);
}

所以你不必设置它。

【讨论】:

    【解决方案3】:

    在您的 application.properties 文件中设置 spring.jpa.defer-datasource-initialization = true

    【讨论】:

      猜你喜欢
      • 2018-06-02
      • 2017-08-17
      • 2015-10-19
      • 1970-01-01
      • 1970-01-01
      • 2019-01-27
      • 2021-12-01
      • 2013-07-26
      • 2022-06-22
      相关资源
      最近更新 更多