How to use a persistent H2 database in the Play Framework instead of in-memory
Asked Answered
T

1

14

The H2 database used in the Java Todo List tutorial is the following:

db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:play"

How do I modify the configuration file to use a persistent database as opposed to an in-memory version. Do I need to setup an entirely separate DB or can I modify the db.default.url property?

I'm using Play! 2.0.3.

Tarsometatarsus answered 6/9, 2012 at 2:6 Comment(0)
T
29

I found the solution.

To create a file database, modify the following:

From

db.default.url="jdbc:h2:mem:play"

To

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

Where data/db is broken down into:

data/ The folder location of the database files relative to your project root.

db The name of your database files.

Tarsometatarsus answered 6/9, 2012 at 2:36 Comment(1)
The data/db is in fact relative to the directory where the process is started. This is a problem if the process is not always started from the same directory. Instead I suggest to use jdbc:h2:file:~/data/db. This will create the database file relative to the home directory of the current user.Fleshly

© 2022 - 2024 — McMap. All rights reserved.