7

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

2 Answers 2

12

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

application.properties
spring.jpa.hibernate.ddl-auto=update
Sign up to request clarification or add additional context in comments.

1 Comment

It works, thanks ! but where i can see this data stored ? in file ? or I can connect by SQL Developer to created database to watch it ?
3

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"

2 Comments

Hi, thanks a lot for the answer. IMHO it should make a difference as the documentation says it would look in the classpath root if there is no application.config found in "a classpath /config package". (docs.spring.io/spring-boot/docs/current/reference/html/…) Anyway, I gave it a try and it didn't work :(
You are right. I compared your app with mine that works fine and found that you need to use Spring Boot JPA dependency instead of Spring Data JPA directly. I have updated my answer to reflect this. I have checked this change with your code and it works fine now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.