我正在尝试使用 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
重新启动服务器后,我的硬盘上也没有任何数据可用。非常欢迎任何建议;-)
最佳答案
由于架构迁移将表设置为更新而被删除,这为我解决了。
application.properties
spring.jpa.hibernate.ddl-auto=update
关于 Spring Boot : H2 database not saved to file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28921896/