Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flyway Core with Spring Boot gives error Circular depends-on relationship between 'delayedFlywayInitializer' and 'entityManagerFactory'

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?

like image 603
xarqt Avatar asked Jan 21 '26 01:01

xarqt


1 Answers

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

  • Removing it, or setting to false could fix your issue

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

like image 193
ameen Avatar answered Jan 23 '26 17:01

ameen