为什么 Spring 使用嵌入式 H2 启动会抛出 'org.h2.message.DbException' 错误?

Why does Spring Boot with embedded H2 throw a 'org.h2.message.DbException' error?

我正在使用 Eclipse/STS 在 MacBook 上做一个简单的 Spring 引导和嵌入式 H2 教程。

当我只使用 H2 作为内存时它工作正常,我的 application.properties 文件如下所示:

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

但是我想将数据库持久化到一个文件中(这样应用关闭时数据不会丢失)。

当我进行此更改以将数据库写入磁盘时:

spring.datasource.url=jdbc:h2:file:/data/demo

我在启动时遇到这个错误:

org.h2.message.DbException: Log file error: "/data/demo.trace.db", cause: "org.h2.message.DbException: Error while creating file ""/data"" [90062-200]" [90034-200]

我做错了什么?

来自堆栈跟踪的更多详细信息:

2020-11-09 12:33:24.246  INFO 15139 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
org.h2.message.DbException: Log file error: "/data/demo.trace.db", cause: "org.h2.message.DbException: Error while creating file ""/data"" [90062-200]" [90034-200]
org.h2.message.DbException: Log file error: "/data/demo.trace.db", cause: "org.h2.message.DbException: Error while creating file ""/data"" [90062-200]" [90034-200]
    at org.h2.message.DbException.get(DbException.java:194)
    at org.h2.message.TraceSystem.logWritingError(TraceSystem.java:294)

[...]

Caused by: org.h2.jdbc.JdbcSQLNonTransientException: Log file error: "/data/demo.trace.db", cause: "org.h2.message.DbException: Error while creating file ""/data"" [90062-200]" [90034-200]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:505)
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:429)
    ... 92 more
Caused by: org.h2.message.DbException: Error while creating file "/data" [90062-200]
    at org.h2.message.DbException.get(DbException.java:205)
    at org.h2.message.DbException.get(DbException.java:181)
    at org.h2.store.fs.FilePathDisk.createDirectory(FilePathDisk.java:290)
    at org.h2.store.fs.FileUtils.createDirectory(FileUtils.java:43)
    at org.h2.store.fs.FileUtils.createDirectories(FileUtils.java:315)
    at org.h2.message.TraceSystem.openWriter(TraceSystem.java:305)
    ... 89 more
Caused by: org.h2.jdbc.JdbcSQLNonTransientException: Error while creating file "/data" [90062-200]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:505)
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:429)
    ... 95 more

解决方案是我需要这样的 . ./data/demo:

spring.datasource.url=jdbc:h2:file:./data/demo