56

We're developing a Play 2.4 application (Java API).

For dev purposes, we'd like to use a persistent H2 database with DB file path relative to the project root directory.

In How to use a persistent H2 database in the Play Framework instead of in-memory there was solution for Play 2.0:

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

However, with Play 2.4 this doesn't seem to work but I get error message with the following exception at the bottom:

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)
    ...

I could get connection to work with an absolute path and with a path relative to the home directory, like the following:

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

or

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

However, is there some way to refer to the project root folder?

Community
  • 1
  • 1
Touko
  • 11,359
  • 16
  • 75
  • 105
  • 4
    how about `db.default.url="jdbc:h2:./data/db"`? – Roman Jun 02 '15 at 14:24
  • @Roman Seems to work, thanks. Don't know whether that could be fragile if process would be started from different directory in some way. But for our development usage this is not a problem. Would you like to add that as an answer? – Touko Jun 03 '15 at 05:19

2 Answers2

86

Ok, I did a little research and found this in the changelog (http://www.h2database.com/html/changelog.html):

Implicit relative paths are disabled (system property "h2.implicitRelativePath"), so that the database URL jdbc:h2:test now needs to be written as jdbc:h2:./test.

In H2 starting from version 1.4.177 Beta, implicit relative paths are not allowed anymore. Therefore, in your case the url should be written with a explicit relative path: db.default.url="jdbc:h2:./data/db".

Roman
  • 5,651
  • 1
  • 30
  • 41
  • Have you ever tried to access h2 db from jar file itself if yes then please respond me on this thread : https://stackoverflow.com/questions/49352706/create-read-only-h2-database-connection – Gopal00005 Mar 18 '18 at 22:16
6

A fixed or relative path can be used. When using the URL jdbc:h2:file:./data/sample http://www.h2database.com/html/faq.html

now a relative path can be used.

for example, jdbc:h2:file:./../../h2db;

Sijo Song
  • 131
  • 1
  • 2