11

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?

xarqt
  • 137
  • 1
  • 2
  • 12
  • Hi, do you need this `@DependsOn("entityManagerFactory")`? If you remove that the problem might be solved – Marcos Barbero Nov 24 '20 at 16:04
  • If I remove it the flyway is initialized before hibernate creates the tables so the script migration fails cause the tables are not created yet, so this is the problem? @MarcosBarbero – xarqt Nov 24 '20 at 16:08
  • I see, but you don't need Hibernate to create your schemas you can do that using Flyway. Mixing the two will only make it more difficult for you – Marcos Barbero Nov 24 '20 at 16:12
  • I need to use Hibernate to create the tables and everything else. Regarding flyway what else can I use for migration of data? – xarqt Nov 24 '20 at 16:16
  • Although Hibernate supports that operation you don't need to use it to do so. Flyway is not only meant for data migration, it's a tool that enables you to have version control on your database, you can create tables, schemas e whatever you need related to databases using Flyway – Marcos Barbero Nov 24 '20 at 16:20
  • @MarcosBarbero I want to use db to import data sequentially or update existing data so this is basically the purpose of Flyway. I used it in other project with lower versions of Spring boot but with this version I get this error. – xarqt Nov 24 '20 at 16:46
  • I understand that you want to use it that way, but that's not the purpose of it. The purpose of Flyway is to version control your database. As I already said, you do not need to use Hibernate to create your tables, you can simply rely on Flyway's features to do so. – Marcos Barbero Nov 24 '20 at 16:50
  • Hi @xarqt did you figure out how to fix this? Having the same issue. – Nicolas Ardison Aug 11 '21 at 01:07
  • @NicolasArdison I moved the flyway files from resources/db/migration to projectx/flywayfiles and on properties I added spring.flyway.locations=filesystem:sql, it solved the issue – xarqt Aug 11 '21 at 09:29

1 Answers1

22

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

ameen
  • 686
  • 5
  • 12