1

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

1
  • 2
    Look into Liquibase or Flyway, both are DB/Data migration tools Commented Feb 12, 2018 at 19:42

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Create a separate profile and set the below property in application.properties :

spring.sql.init.mode=never

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.