首页 > 解决方案 > Spring Boot 数据源和 h2 数据库路径

问题描述

在我的 Spring Boot 应用程序中,我正在尝试配置 H2 数据库文件夹的路径。我想通过以下路径放置它:

/home/public/h2

像这样的配置:

# Datasource
spring.datasource.url=jdbc:h2:file:/home/public/h2
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver

导致以下错误:

Caused by: org.h2.jdbc.JdbcSQLException: A file path that is implicitly relative to the current working directory is not allowed in the database URL "jdbc:h2:file:/home/public/h2". Use an absolute path, ~/name, ./name, or the baseDir setting instead. [90011-197]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:357) ~[h2-1.4.197.jar:1.4.197]

我也试过spring.datasource.url=jdbc:h2:file:~/home/public/h2 ,但它不起作用。

我在做什么错以及如何正确配置路径?

标签: spring-bootspring-data-jpaspring-datadatasourceh2

解决方案


请尝试使用jdbc:h2:./name(显式相对路径),或

将系统属性设置h2.implicitRelativePathtrue(以防止此检查)。

对于 Windows,绝对路径还需要包含驱动器("C:/...")

h2.implicitRelativePath=true
spring.datasource.url=jdbc:h2:~/home/public/h2

或者

h2.implicitRelativePath=true
spring.datasource.url=jdbc:h2:file:~/home/public/h2

有关更多详细信息,请查看此处...


推荐阅读