springspring-bootspring-data

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


Solution

  • You have couple of options:

    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.