简体   繁体   English

Spring Boot schema.sql - 重新启动时删除db模式

[英]Spring Boot schema.sql - drop db schema on restart

Hi I'm using Spring Boot version 1.5.9. 嗨,我使用的是Spring Boot 1.5.9版。

When using Spring Boot to initialize schema.sql for mysql database, it works all fine and the database schema is getting created successfully. 当使用Spring Boot为mysql数据库初始化schema.sql ,它工作正常,并且数据库模式已成功创建。 But on restart of the application this schema.sql script is executing again and the application fails to start because the tables already exist. 但是在重新启动应用程序时,此schema.sql脚本再次执行,并且应用程序无法启动,因为表已经存在。

I tried spring.jpa.hibernate.ddl-auto=create-drop option in application.properties but it does not have any effect (probably because it only works for Hibernate entities which I'm not using) 我在application.properties尝试了spring.jpa.hibernate.ddl-auto=create-drop选项,但它没有任何效果(可能因为它只适用于我没有使用的Hibernate实体)

Is there a way to have Spring Boot to re-create schema from schema.sql every time on restart if the database is not in-memory one? 如果数据库不在内存中,有没有办法让Spring Boot每次重启时都从schema.sql重新创建模式?

GitHub: https://github.com/itisha/spring-batch-demo/tree/database-input GitHub: https//github.com/itisha/spring-batch-demo/tree/database-input

According to the documentation you can simply ignore exceptions by setting spring.datasource.continue-on-error property to true 根据文档,您可以通过将spring.datasource.continue-on-error属性设置为true来忽略异常

Spring Boot enables the fail-fast feature of the Spring JDBC initializer by default, so if the scripts cause exceptions the application will fail to start. Spring Boot默认启用Spring JDBC初始化程序的快速失败功能,因此如果脚本导致异常,则应用程序将无法启动。 You can tune that using spring.datasource.continue-on-error. 您可以使用spring.datasource.continue-on-error来调整它。

or even turn it off with spring.datasource.initialize set to false 甚至将spring.datasource.initialize设置为false将其关闭

You can also disable initialization by setting spring.datasource.initialize to false. 您还可以通过将spring.datasource.initialize设置为false来禁用初始化。

A workaround could be, to change the create statements in your schema.sql from 解决方法可能是,更改schema.sql中的create语句

 CREATE TABLE test .....

to

 CREATE TABLE IF NOT EXISTS test ...

use the IF NOT EXISTS statements 使用IF NOT EXISTS语句

关闭自动模式创建以避免冲突:在application.properties中添加此行

spring.jpa.hibernate.ddl-auto=none

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM