【问题标题】:How to use a path relative to project root to H2 db-file configuration with Play Framework 2.4?如何使用相对于项目根目录的路径到带有 Play Framework 2.4 的 H2 db 文件配置?
【发布时间】:2015-08-16 05:59:33
【问题描述】:

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

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

How to use a persistent H2 database in the Play Framework instead of in-memory 中有 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"

但是,有什么方法可以引用项目根文件夹吗?

【问题讨论】:

  • db.default.url="jdbc:h2:./data/db" 怎么样?
  • @Roman 似乎有效,谢谢。如果进程以某种方式从不同的目录启动,不知道这是否会很脆弱。但是对于我们的开发使用来说,这不是问题。您想添加它作为答案吗?

标签: jdbc h2 relative-path persistent playframework-2.4


【解决方案1】:

好的,我做了一些研究,并在更改日志 (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"

【讨论】:

【解决方案2】:

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

现在可以使用相对路径。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-09
    • 2017-03-24
    • 2011-05-27
    • 1970-01-01
    • 2018-01-15
    相关资源
    最近更新 更多