50

我们正在开发一个 Play 2.4 应用程序(Java API)。

出于开发目的,我们希望使用具有相对于项目根目录的 DB 文件路径的持久 H2 数据库。

How to use an persistent H2 database in the Play Framework 而不是内存中,有 Play 2.0 的解决方案:

db.default.url="jdbc:h2:file:data/db"

但是,使用 Play 2.4 这似乎不起作用,但我收到错误消息,底部有以下异常:

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:data/db". Use an absolute path, ~/name, ./name, or the 
baseDir setting instead. [90011-187]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
    at org.h2.message.DbException.get(DbException.java:179)
    ...

我可以使用绝对路径和相对于主目录的路径获得连接,如下所示:

db.default.url="jdbc:h2:file:/Users/foo/data/db"

或者

db.default.url="jdbc:h2:~/data/db"

但是,有没有办法引用项目根文件夹?

4

2 回答 2

76

好的,我做了一些研究,并在更改日志(http://www.h2database.com/html/changelog.html)中找到了这个:

隐式相对路径被禁用(系统属性“h2.implicitRelativePath”),因此数据库 URL jdbc:h2:test 现在需要写为 jdbc:h2:./test。

从 1.4.177 Beta 版开始的 H2 中,不再允许隐式相对路径。因此,在您的情况下,应该使用明确的相对路径编写 url db.default.url="jdbc:h2:./data/db":.

于 2015-06-03T06:41:47.310 回答
5

可以使用固定或相对路径。当使用 URL jdbc:h2:file:./data/sample http://www.h2database.com/html/faq.html

现在可以使用相对路径。

例如, jdbc:h2:file:./../../h2db;

于 2018-12-27T00:55:28.247 回答