Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a path relative to project root to H2 db-file configuration with Play Framework 2.4?

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?

like image 719
Touko Avatar asked Jun 02 '15 12:06

Touko


People also ask

How do I access H2 DB files?

Alternatively you can connect using the browser based H2 console. The easiest way to access the console is to double click the H2 database jar file at <installation-directory>\confluence\WEB-INF\lib\h2-x.x.x.jar .

How do I access the H2 console?

Access the H2 Console You can access the console at the following URL: http://localhost:8080/h2-console/. You need to enter the JDBC URL, and credentials. To access the test database that the greeter quickstart uses, enter these details: JDBC URL: jdbc:h2:mem:greeter-quickstart;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1.

Does H2 support PostgreSQL?

SQL Support Compatibility modes for IBM DB2, Apache Derby, HSQLDB, MS SQL Server, MySQL, Oracle, and PostgreSQL.


2 Answers

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

like image 81
Roman Avatar answered Nov 07 '22 14:11

Roman


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;

like image 21
Sijo Song Avatar answered Nov 07 '22 15:11

Sijo Song