spring-projects/spring-data-neo4j

[springboot2.4+sdn6.3.18]The provided database selection provider differs from the Neo4jClient's one.

papadave66 opened this issue · 1 comments

Hi, I am using springboot 2.4.9 + spring-data-neo4j 6.3.18 + JDK11
and it gives the following error log when starts up

2024-06-21 09:27:54.163 ERROR 74671 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'neo4jTemplate' defined in class path resource [org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.neo4j.core.Neo4jTemplate]: Factory method 'neo4jTemplate' threw exception; nested exception is java.lang.IllegalStateException: The provided database selection provider differs from the Neo4jClient's one.
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658)
	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:638)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1334)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:144)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:771)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:763)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:438)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:339)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1329)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1318)
	at cn.asiic.datalake.Application.main(Application.java:10)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.neo4j.core.Neo4jTemplate]: Factory method 'neo4jTemplate' threw exception; nested exception is java.lang.IllegalStateException: The provided database selection provider differs from the Neo4jClient's one.
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653)
	... 20 common frames omitted
Caused by: java.lang.IllegalStateException: The provided database selection provider differs from the Neo4jClient's one.
	at org.springframework.data.neo4j.core.Neo4jTemplate.<init>(Neo4jTemplate.java:138)
	at org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration.neo4jTemplate(Neo4jDataAutoConfiguration.java:105)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
	... 21 common frames omitted


Process finished with exit code 1

My current config about neo4j looks like this

@Configuration
@EnableNeo4jRepositories("cn.test.group.metadata.neo4j.repository")
public class Neo4jConfig {
}

and my application.yml configs like following

  data:
    neo4j:
      database: neo4j
  neo4j:
    uri: neo4j://127.0.0.1
    database: neo4j
    authentication:
      username: neo4j
      password: 'dolphin'

Then i start to debug the program, it shows that in org.springframework.data.neo4j.core.Neo4jTemplate line 108

  /** @deprecated */
    @Deprecated
    public Neo4jTemplate(Neo4jClient neo4jClient, Neo4jMappingContext neo4jMappingContext, DatabaseSelectionProvider databaseSelectionProvider) {
        this(neo4jClient, neo4jMappingContext, EntityCallbacks.create());
        if (!Objects.equals(databaseSelectionProvider, neo4jClient.getDatabaseSelectionProvider())) {
            throw new IllegalStateException("The provided database selection provider differs from the Neo4jClient's one.");
        }
    }

databaseSelectionProvider has value neo4j but neo4jClient.getDatabaseSelectionProvider()i s null.
I am not sure why this happened.but i tried to add following code in my neo4jConfig file

    @Bean
    public Neo4jClient neo4jClient(
            Driver driver,
            DatabaseSelectionProvider databaseSelectionProvider
    ) {

        return Neo4jClient.with(driver)
                .withDatabaseSelectionProvider(databaseSelectionProvider)
                .build();
    }

then new error occurs. the log shows as follow. the error looks very familar with #2488

2024-06-21 09:46:31.627 ERROR 76875 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'reactiveNeo4jTemplate' defined in class path resource [org/springframework/boot/autoconfigure/data/neo4j/Neo4jReactiveDataAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.neo4j.core.ReactiveNeo4jTemplate]: Factory method 'reactiveNeo4jTemplate' threw exception; nested exception is java.lang.IllegalStateException: The provided database selection provider differs from the ReactiveNeo4jClient's one.
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658)
	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:638)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1334)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:144)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:771)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:763)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:438)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:339)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1329)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1318)
	at cn.asiic.datalake.Application.main(Application.java:10)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.neo4j.core.ReactiveNeo4jTemplate]: Factory method 'reactiveNeo4jTemplate' threw exception; nested exception is java.lang.IllegalStateException: The provided database selection provider differs from the ReactiveNeo4jClient's one.
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653)
	... 20 common frames omitted
Caused by: java.lang.IllegalStateException: The provided database selection provider differs from the ReactiveNeo4jClient's one.
	at org.springframework.data.neo4j.core.ReactiveNeo4jTemplate.<init>(ReactiveNeo4jTemplate.java:141)
	at org.springframework.boot.autoconfigure.data.neo4j.Neo4jReactiveDataAutoConfiguration.reactiveNeo4jTemplate(Neo4jReactiveDataAutoConfiguration.java:69)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
	... 21 common frames omitted


Process finished with exit code 1

I am sure i don't have any reactive code nor repository code. just some entity with node annotation, but this error shows when starts up.
So i add following ugly workaround into my neo4jconfig to make it boots up

//     ugly workaround. don't use this reactiveNeo4jTemplate.
    @Bean
    public ReactiveNeo4jTemplate reactiveNeo4jTemplate(ReactiveNeo4jClient neo4jClient, Neo4jMappingContext neo4jMappingContext, ReactiveDatabaseSelectionProvider databaseSelectionProvider) {
        ReactiveNeo4jClient reactiveNeo4jClient = new ReactiveNeo4jClient() {
            @Override
            public Mono<RxQueryRunner> getQueryRunner() {
                return ReactiveNeo4jClient.super.getQueryRunner();
            }

            @Override
            public Mono<RxQueryRunner> getQueryRunner(Mono<DatabaseSelection> databaseSelection) {
                return ReactiveNeo4jClient.super.getQueryRunner(databaseSelection);
            }

            @Override
            public Mono<RxQueryRunner> getQueryRunner(Mono<DatabaseSelection> databaseSelection, Mono<UserSelection> userSelection) {
                return null;
            }

            @Override
            public UnboundRunnableSpec query(String cypher) {
                return null;
            }

            @Override
            public UnboundRunnableSpec query(Supplier<String> cypherSupplier) {
                return null;
            }

            @Override
            public <T> OngoingDelegation<T> delegateTo(Function<RxQueryRunner, Mono<T>> callback) {
                return null;
            }

            @Override
            public ReactiveDatabaseSelectionProvider getDatabaseSelectionProvider() {
                return databaseSelectionProvider;
            }
        };
        return new ReactiveNeo4jTemplate(reactiveNeo4jClient, neo4jMappingContext, databaseSelectionProvider);
    }

at the end it boots up smoothly. but i don't know why it happens. so i think report to here is a good idea.

oh. the fix code in #2488 works. sorry i will close my issue now

@Configuration
public class FixInvalidConfigurationOfReactiveClient {

	@Bean
	public ReactiveNeo4jClient reactiveNeo4jClient(Driver driver,
		ReactiveDatabaseSelectionProvider reactiveDatabaseSelectionProvider) {
		return ReactiveNeo4jClient.create(driver, reactiveDatabaseSelectionProvider);
	}

	@Bean
	public Neo4jClient neo4jClient(Driver driver,
		DatabaseSelectionProvider databaseSelectionProvider) {
		return Neo4jClient.create(driver, databaseSelectionProvider);
	}
}