简体   繁体   English

Spring Boot data.sql避免每次启动时多次执行

[英]Spring boot data.sql avoid multiple execution on every startup

Spring boot offers the great option to load initial data into the database on startup using data.sql. Spring boot提供了一个很棒的选项,可以在启动时使用data.sql将初始数据加载到数据库中。 The problem is, I have a table where the column is unique and spring tries to executes the sqls on every startup and fails if the data already present. 问题是,我有一个表,其中的列是唯一的,并且spring尝试在每次启动时执行sql,并且如果已经存在数据则失败。 Now the question is, what is the proper way to handle such cases, is there any way to tell spring when to execute and when not to execute the data.sql file. 现在的问题是,处理这种情况的正确方法是什么,有什么方法可以告诉spring什么时候执行和什么时候不执行data.sql文件。 Thanks 谢谢

You have couple of options: 您有几种选择:

  • Disable the database initialization at start up using spring.datasource.initialize=false 使用spring.datasource.initialize=false禁用启动时的数据库初始化
  • Turning off Spring Boot's fail fast behavior using spring.datasource.continue-on-error=true 使用spring.datasource.continue-on-error=true关闭Spring Boot的快速失败行为

Second option would let your Spring Boot app to start normally by ignoring any exceptions arising from script execution. 第二种选择是让您的Spring Boot应用程序正常运行,方法是忽略脚本执行引起的任何异常。

Spring Boot Documentation Spring Boot文档

If both of the above options doesn't suit your requirement, as Darren Forsythe mentioned in comments, Consider data migration tools like Flyway or Liquibase. 如果以上两个选项都不符合您的要求,如Darren Forsythe在评论中所述,请考虑使用数据迁移工具,例如Flyway或Liquibase。 Spring boot provides great support for both tools. Spring Boot为这两种工具提供了强大的支持。 Check this out. 检查这个出来。

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

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