Skip to content
Advertisement

Spring boot not executing schema.sql script

I’m developing a Spring Boot web application and want to create a MySql database if it’s not already created. So I’ve done a dump of my current database in order to have an empty schema of it. Put it in /src/main/resources, so maven brings it to /WEB-INF/classes when building the war file. That’s how my application.properties is configured (according to Spring docs, that should create the DB from the script):

JavaScript

And that’s the error I get when I try to run the application (it complains about a non-existing DB):

JavaScript

So it seems that Spring tries to connect to the database even schema.sql, which contains the script to create the DB, not executed. There are some related questions in Stack Overflow about that, but still can’t get it working, even if I try with spring.datasource.initialize=true

Advertisement

Answer

Well, looks like you can’t do that from the common JDBC connect: creating a database in mysql from java

Hence Spring Boot can’t do that for you automatically.

Please, don’t mix the DB creating with its creation for its content: tables, procedures, triggers etc.

UPDATE

Yes, you can do that on the application startup. You just a separate initializer, which has an order before dataSourceInitializer.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement