Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot: H2 database not saved to file

I'm trying to persist an H2 in-memory DB to a file with Spring Boot to reuse the data in there.

Unfortunately, the way to specify the datasource url like

spring.datasource.url = jdbc:h2:file:~/WeatherDB;FILE_LOCK=FS

(complete application.properties)

doesn't work for me. I can't find a file generated by H2 anywhere on my hard disk (also, saved data is not available after restarting the server).

To visualize this, I created a sample project that can be found on Bitbucket.

With that, it doesn't seem to make a difference if the application is run from an IDE with gradle run or after packaging it from the jar.

What config option is needed to persist and reuse the H2 DB ?

Update:

I realized there is a actuator endpoint for config options at http://localhost:8080/configprops that shows

"spring.datasource.CONFIGURATION_PROPERTIES": {

    "prefix": "spring.datasource",
    "properties": {
        "schema": null,
        "data": null,
        "xa": {
            "dataSourceClassName": null,
            "properties": { }
        },
        "separator": ";",
        "url": "jdbc:h2:file:~/WeatherDB",
        "platform": "all",
        "continueOnError": false,
        "jndiName": null,
        "sqlScriptEncoding": null,
        "password": "******",
        "driverClassName": "org.h2.Driver",
        "initialize": true,
        "username": "sa"
    }
},

But I just can't find an file WeatherDB on my harddisk nor is any data available after restarting the server.

Any suggestions very much welcome ;-)

like image 870
Markus Avatar asked Mar 08 '15 00:03

Markus


2 Answers

Table get dropped due to schema migration setting it to update solved for me.

application.properties
spring.jpa.hibernate.ddl-auto=update
like image 88
Deepak Kumar Avatar answered Dec 17 '22 00:12

Deepak Kumar


Your application.properties file is not being picked up. Replace

compile "org.springframework.data:spring-data-jpa:1.7.2.RELEASE"
compile "org.hibernate:hibernate-entitymanager:4.3.8.Final"

with

compile "org.springframework.boot:spring-boot-starter-data-jpa"
like image 45
manish Avatar answered Dec 16 '22 23:12

manish