如何在Play框架2.4中使用相对于项目根目录的路径配置H2数据库文件?

56

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

出于开发目的,我们想使用一个持久的H2数据库,其DB文件路径相对于项目根目录。

如何在Play Framework中使用持久的H2数据库而不是内存中的中,提供了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
db.default.url="jdbc:h2:./data/db"的翻译是什么? - Roman
@Roman 看起来可以工作,谢谢。不知道如果进程以某种方式从不同的目录启动,是否会变得脆弱。但对于我们的开发使用,这不是问题。您想将其添加为答案吗? - Touko
2个回答

88

好的,我进行了一些调研,并在更改日志中找到了以下内容 (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"


你是否曾经尝试从jar文件本身访问h2数据库?如果是,请在此线程上回复我:https://stackoverflow.com/questions/49352706/create-read-only-h2-database-connection - Gopal00005

6

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

现在可以使用相对路径。

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接