【问题标题】:Spring Boot: H2 database not saved to fileSpring Boot:H2 数据库未保存到文件
【发布时间】:2015-05-09 10:07:57
【问题描述】:

我正在尝试使用 Spring Boot 将 H2 内存数据库持久化到一个文件中,以重用其中的数据。

不幸的是,指定数据源url的方式就像

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

(complete application.properties)

对我不起作用。我在硬盘上的任何地方都找不到 H2 生成的文件(另外,重新启动服务器后保存的数据不可用)。

为了可视化这一点,我创建了一个示例项目,可以在 Bitbucket 上找到。

这样,如果应用程序是从带有gradle run 的 IDE 中运行或在从 jar 中打包后运行的,似乎并没有什么不同。

需要什么配置选项来持久化和重用 H2 DB?

更新:

我意识到http://localhost:8080/configprops 有一个配置选项的执行器端点,显示

"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"
    }
},

但我只是在我的硬盘上找不到文件WeatherDB,重启服务器后也没有任何数据可用。

非常欢迎任何建议;-)

【问题讨论】:

    标签: spring spring-boot spring-data h2


    【解决方案1】:

    表因架构迁移而被删除,为我解决了更新问题。

    application.properties
    spring.jpa.hibernate.ddl-auto=update
    

    【讨论】:

    • 它有效,谢谢!但是我在哪里可以看到存储的这些数据?在文件中?或者我可以通过 SQL Developer 连接到创建的数据库来观看它?
    【解决方案2】:

    您的application.properties 文件未被拾取。替换

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

    compile "org.springframework.boot:spring-boot-starter-data-jpa"
    

    【讨论】:

    • 您好,非常感谢您的回答。恕我直言,它应该有所作为,因为文档说如果在“类路径/配置包”中没有找到 application.config,它将在类路径根目录中查找。 (docs.spring.io/spring-boot/docs/current/reference/html/…) 无论如何,我试了一下,但没有成功:(
    • 你是对的。我将您的应用程序与运行良好的应用程序进行了比较,发现您需要直接使用 Spring Boot JPA 依赖项而不是 Spring Data JPA。我已经更新了我的答案以反映这一点。我已经用你的代码检查了这个更改,现在可以正常工作了。
    猜你喜欢
    • 2020-08-11
    • 1970-01-01
    • 2016-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-05
    • 1970-01-01
    • 2021-10-27
    相关资源
    最近更新 更多