简体   繁体   中英

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. 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. 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. Thanks

You have couple of options:

  • Disable the database initialization at start up using spring.datasource.initialize=false
  • Turning off Spring Boot's fail fast behavior using spring.datasource.continue-on-error=true

Second option would let your Spring Boot app to start normally by ignoring any exceptions arising from script execution.

Spring Boot Documentation

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. Spring boot provides great support for both tools. Check this out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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