Multi database feature of Neo4j 4 - select database in each API call

I'd like to maintain a separate graphs for every client in my application. For such purpose I'd like to use the multi database feature of Neo4j 4.

In case of single backend logic for all clients, I have a following question - how to quickly navigate between databases based on the database name in every API call from client application? Do I only need to include ":use database_name" prior to each Cypher query in a scope of the same transaction or how? Will it be effective from a performance point of view?

If you are using the Neo4jClient, you can quickly change the database that is used for the query with the in method.

Flux<Map<String, Object>> allActors = client
        .query("MATCH .....")
1 Like

Thanks for your answer! But under the hood it still be two separate commands - ":use database_name" and then MATCH query ?

No, under the hood there is the Bolt protocol in place used by the drivers.
It communicates directly with the dbms and tells it to use this specific database during the session when the communication for a session starts.

1 Like

Thanks, understood. But how about in case of SDN repository method? In case I'm going to execute MATCH on the particular database, before of that I have to execute another Cypher query with ":use database_name" in the same transaction?

No, this won't work as you would expect this.
Instead you will have to use the dynamic database selection feature.
This means controlled by some data within you application, you know which database to select and return its name on demand.
An example with Spring Security can be found here: Spring Data Neo4j Reference documentation - Dynamically configured database name
Sadly I cannot recap this right out of my head but I am pretty sure that there is some Spring feature that allows you to share the information coming in from the request with the context and the outcome of the dynamic database name source.