java Spring:H2 数据库持久化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42857731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring: H2 Database persistence
提问by SteveOhio
My application.properties:
我的 application.properties:
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.url=jdbc:h2:./src/main/resources/asnDB;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.user=sa
spring.datasource.password=
spring.h2.console.enabled=true
spring.jpa.hibernate.ddl-auto=create
I have a data.sql which is loaded when I start the spring-project.
我有一个 data.sql,它在我启动 spring 项目时加载。
How do I alter the application.properties to make the database persistent?
如何更改 application.properties 以使数据库持久化?
For now it always makes a new one. It doesn't work neither if I change the ddl.auto=create
to ddl.auto=update
. I know that ddl.auto=create
overwrites my DB, but I have no idea how to make it persistent.
现在它总是制造一个新的。如果我更改为ddl.auto=create
,它也不起作用ddl.auto=update
。我知道这会ddl.auto=create
覆盖我的数据库,但我不知道如何使其持久化。
In the data.sql there are 3 Insert-Statements and when I run the project I already have 3 inserts in my DB. Then I insert a new one via my UI and quit the project. When i re-run the project there are just the initial 3 inserts. But there should be 4 inserts.
在 data.sql 中有 3 个插入语句,当我运行该项目时,我的数据库中已经有 3 个插入。然后我通过我的 UI 插入一个新的并退出项目。当我重新运行项目时,只有最初的 3 个插入。但是应该有4个插入。
回答by PowerFlower
You miss the auto-reconnect feature
您错过了自动重新连接功能
spring.datasource.url=jdbc:h2:file:~/test2;DB_CLOSE_ON_EXIT=FALSE;AUTO_RECONNECT=TRUE
spring.datasource.url=jdbc:h2:file:~/test2;DB_CLOSE_ON_EXIT=FALSE;AUTO_RECONNECT=TRUE
So that for example works:
因此,例如工作:
spring.datasource.url=jdbc:h2:file:~/test2;DB_CLOSE_ON_EXIT=FALSE;AUTO_RECONNECT=TRUE
spring.datasource.username=admin
spring.datasource.password=password
spring.datasource.driver-class-name=org.h2.Driver
#spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
回答by Raj
persistence comes from property spring.jpa.hibernate.ddl-autobeing updateinstead of being create
持久性来自属性spring.jpa.hibernate.ddl-auto被更新而不是被创建