Reputation: 197
I want to import some data on the SQL server database, I'm using Spring Boot 2.3.4. I use also Hibernate to generate the tables.
I added flyway core in pom:
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
Created the configuration file:
import org.flywaydb.core.Flyway;
import org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer;
import org.springframework.boot.autoconfigure.flyway.FlywayMigrationStrategy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
@Configuration
public class FlyWayConfiguration {
@Bean
FlywayMigrationInitializer flywayInitializer(Flyway flyway) {
return new FlywayMigrationInitializer(flyway, (f) ->{} );
}
@Bean
@DependsOn("entityManagerFactory")
FlywayMigrationInitializer delayedFlywayInitializer(Flyway flyway) {
return new FlywayMigrationInitializer(flyway, new FlywayMigrationStrategy() {
@Override
public void migrate(Flyway flyway) {
flyway.migrate();
}
});
}
}
I created a file on resources/db/migration/V1_0_1__InitialData.sql
Now I'm having this error
Error creating bean with name 'delayedFlywayInitializer' defined in class path resource
[com/ikun/mkj/config/MigrationConfiguration.class]: Circular depends-on relationship between
'delayedFlywayInitializer' and 'entityManagerFactory' at
org.springframework.beans.factory.support.AbstractBeanFactory
I don't know how to fix this, I searched for solution but couldn't make. Can someone help me please?
Upvotes: 14
Views: 11984
Reputation: 51
At first I tried ameen's solution, but it alone didn't help. When I applied it, Flyway then complained there was no table to run the sql on.
I ended up having to add the following to application.properties:
spring.jpa.defer-datasource-initialization = false
spring.main.allow-circular-references = true
spring.flyway.depends-on = entityManagerFactory
The additional two lines change the initialization order and causes Flyway to be initialized before entityManagerFactory.
I guess this is relevant for newer versions.
Spring Boot 3.1.3 here.
p.s. this seem to be a problem for Spring Boot 3.1.x up to 3.1.4 only. Versions 3.1.5 and 3.0.9 work without the need to add those settings. I believe this impacts only some versions of Spring Boot 2 too, so the easy solution is to switch off of the problematic version, if possible.
Upvotes: 1
Reputation: 836
Most likely u are deferring the Datasource initializing by adding :
spring.jpa.defer-datasource-initialization =true #set it to false
in your application.[yml/properties].
as in the reference : https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html
set spring.jpa.defer-datasource-initialization to true. This will defer data source initialization until after any EntityManagerFactory beans have been created and initialized. schema.sql can then be used to make additions to any schema creation performed by Hibernate and data.sql can be used to populate it.
And by default Flyway depends on Datasource , Datasource in defer mode will wait for EntityManagerFactory and Ofc since we use flyway the default is to start Flyway before Jpa to ensure DB consistency
So we have a circular Dependency flyway->DS->EMF->Flyway
Upvotes: 37