How to configure spring-boot to use file based H2 database

后端 未结 5 618
闹比i
闹比i 2020-12-23 10:51

I have successfully created a spring boot application that uses the H2 embedded database in-memory. I would now like to change this to a file based version that will persist

相关标签:
5条回答
  • 2020-12-23 11:33

    I am adding this answer to avoid confusion and further research.

    Actually I have the same problem and none of the answer worked for me completely rather than the mix for some answers worked.

    Here is the minimal configuration which is required to persist H2 db in spring boot.

    application.properties

    # H2
    spring.h2.console.enabled=true
    spring.h2.console.path=/h2
    # Datasource
    spring.datasource.url=jdbc:h2:file:~/spring-boot-h2-db
    spring.datasource.username=sa
    spring.datasource.password=
    spring.datasource.driver-class-name=org.h2.Driver
    spring.jpa.hibernate.ddl-auto=update
    

    Here spring.jpa.hibernate.ddl-auto=update does the trick. Nothing else is required.

    No need to add spring-boot-starter-jdbc in pom.xml

    No need to add any parameter in jdbc url.

    0 讨论(0)
  • 2020-12-23 11:34

    Create a file .h2.server.properties in your class path and put below things and try again. You can create this file in resources folder.

    #H2 Server Properties
    0=H2 File|org.h2.Driver|jdbc\:h2\:file\:~/test;DB_CLOSE_ON_EXIT=FALSE
    
    # Enable if you want other applications to connect
    #webAllowOthers=true
    #webPort=8082
    #webSSL=false
    
    0 讨论(0)
  • 2020-12-23 11:47

    Just generated a brand new Spring Boot project with start.spring.io with a few dependencies h2, JPA, web, devtools, actuator. After adding a simple Entity and Spring Data repository, the database is indeed created in memory by default.

    Adding the following to my application.properties definitely creates the database file in the right place:

    spring.datasource.url=jdbc:h2:file:~/test;DB_CLOSE_ON_EXIT=FALSE
    spring.datasource.username=test
    spring.datasource.password=test
    spring.datasource.driverClassName=org.h2.Driver
    

    I can even connect to it with the H2 console when devtools is enabled http://localhost:8080/h2-console/.

    The next logical step is to visit the http://localhost:8080/autoconfig endpoint and check the auto-configuration status.

    In my case, the following is positiveMatches:

    DataSourceAutoConfiguration.NonEmbeddedConfiguration: [
    {
      condition: "DataSourceAutoConfiguration.NonEmbeddedDataSourceCondition",
      message: "supported DataSource class found"
    },
    {
      condition: "OnBeanCondition",
      message: "@ConditionalOnMissingBean (types: javax.sql.DataSource,javax.sql.XADataSource; SearchStrategy: all) found no beans"
    }
    ],
    

    and the following in negativeMatches:

    DataSourceAutoConfiguration.EmbeddedConfiguration: [
    {
      condition: "DataSourceAutoConfiguration.EmbeddedDataSourceCondition",
      message: "existing non-embedded database detected"
    }
    ],
    

    Could you try the following and check the auto-configuration report for those?

    0 讨论(0)
  • 2020-12-23 11:48

    Using the following setting on application.properties, I manage to keep the data persisted even after shutting down and restarting SpringBoot, and even after restarting the computer.

    spring.datasource.name=japodb
    spring.datasource.initialize=false
    spring.datasource.driverClassName=org.h2.Driver
    
    spring.datasource.url=jdbc:h2:file:~/japodb;DB_CLOSE_ON_EXIT=FALSE;IFEXISTS=TRUE;DB_CLOSE_DELAY=-1;
    

    Don't Close a Database when the VM Exits, yes, but also don’t make a new database if it’s already there.

    jdbc:h2:<url>;IFEXISTS=TRUE
    
    spring.jpa.hibernate.ddl-auto = update
    
    0 讨论(0)
  • 2020-12-23 11:53

    Refer to http://www.h2database.com/html/cheatSheet.html

    I guess it might be problem with the jdbc.url, change it like this:

    # from:
    spring.datasource.url=jdbc:h2:file:~/test;DB_CLOSE_ON_EXIT=FALSE
    
    # to:
    spring.datasource.url=jdbc:h2:~/test;DB_CLOSE_ON_EXIT=FALSE
    
    0 讨论(0)
提交回复
热议问题