Mcm
Mcm

Reputation: 119

spring-boot Persistent h2database in filesystem

How can I configure spring-boot with h2database so as it reuses database each time I restart.

This is the only line I have in my application.properties file

spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

Upvotes: 6

Views: 7232

Answers (1)

davidxxx
davidxxx

Reputation: 131326

You have to specify for spring.datasource.url a value that specifies a filesystem DB. You can do it by using the jdbc:h2:file: prefix.
For example, you could use this configuration to store the DB in a mydb.mv.db file in the db folder of your home directory:

spring.datasource.url = jdbc:h2:file:~/db/mydb

Note that spring.jpa.database-platform=org.hibernate.dialect.H2Dialect is not required. The url and the H2 JDBC driver located in the classpath at runtime are enough.

Note also that by default, the database will be automatically created at startup if you use an embedded database (H2, HSQL or Derby).

It is the case even if you specify a file as database in the JDBC URL.

So to avoid recreating the db at each Spring Boot startup, you should also add :

spring.jpa.hibernate.ddl-auto = update

or

spring.jpa.hibernate.ddl-auto = validate

Upvotes: 17

Related Questions