0

In SpringBoot Maven application I have configured two datasources - one Neo4j for Liquigraph

@Bean
@LiquigraphDataSource
public DataSource neo4jDataSource() {
    final HikariConfig config = new HikariConfig();
    config.setJdbcUrl(jdbcServerDatabaseUri);
    config.setUsername(username);
    config.setPassword(password);
    return new HikariDataSource(config);
}

and one PostgreSQL for Liquibase

@Bean
@LiquibaseDataSource
public DataSource postgreSQLDataSource() {
    DataSource dataSource = DataSourceBuilder.create()
            .username(username)
            .password(password)
            .url(databaseUrl)
            .build();
    return dataSource;
}

but during the application start up it fails with the following exception:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'matrixServiceImpl': Unsatisfied dependency expressed through field 'session'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.data.neo4j.repository.config.Neo4jOgmEntityInstantiatorConfigurationBean#0': Cannot resolve reference to bean 'sessionFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/decisionwanted/domain/configuration/Neo4jConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.neo4j.ogm.session.SessionFactory]: Factory method 'sessionFactory' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.liquigraph.spring.starter.LiquigraphAutoConfiguration$LiquigraphConfiguration': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.liquigraph.spring.starter.LiquigraphAutoConfiguration$LiquigraphConfiguration$$EnhancerBySpringCGLIB$$cb17993a]: Constructor threw exception; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected single matching bean but found 2: postgreSQLDataSource,neo4jDataSource
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:660)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640)

What am I doing wrong and how to fix it?

alexanoid
  • 24,051
  • 54
  • 210
  • 410
  • If you only use the Neo4j datasource bean for Liquigraph, you can annotate the `PostgreSQL` bean with @Primary. – fbiville May 31 '21 at 12:35

1 Answers1

0

I reconfigured Liquigraph with the following:

@Bean
@ConfigurationProperties(
        prefix = "liquigraph",
        ignoreUnknownFields = false
)
public LiquigraphProperties liquigraphProperties() {
    return new LiquigraphProperties();
}

@Bean
public SpringLiquigraph liquigraph(ResourceLoader loader, LiquigraphProperties properties) {
    String jdbcServerDatabaseUri = properties.getUrl();

    final HikariConfig config = new HikariConfig();
    config.setJdbcUrl(jdbcServerDatabaseUri);
    config.setUsername(properties.getUser());
    config.setPassword(properties.getPassword());
    DataSource dataSource = new HikariDataSource(config);

    SpringChangelogLoader changelogLoader = new SpringChangelogLoader(loader);
    return new SpringLiquigraph(dataSource, changelogLoader, properties.getChangeLog(), properties.getExecutionContexts());
}
alexanoid
  • 24,051
  • 54
  • 210
  • 410