Multiple databases

This section explains how to use drivers with multiple databases.

With the addition of multiple databases in 4.0, you can now specify which database to work with. When constructing a session, you can specify in the session configuration which database the session is linked to. Queries will then be executed against that database for the duration of the session. Not specifying a database results in the session being linked to the default database as specified in the server configuration, see Operations manual → The default database. When using 4.0 drivers with 4.0 Neo4j Servers, it is recommended to specify the database of each session explicitly.

Example 1. Selecting a database for a session.
import org.neo4j.driver.Driver;
import org.neo4j.driver.Session;
import org.neo4j.driver.SessionConfig;
...

try ( Session session = driver.session( SessionConfig.forDatabase( "neo4j" ) ) ) {...}

Neo4j Community Edition does not support multiple databases beyond the `system`and a default database.

For more information on managing multiple databases in Neo4j, see Operations manual → The system database and Cypher® manual → Administration.

Bookmarks

Bookmarks are generally handled internally by the driver. Applications work with bookmarks directly when chaining sessions.

In a multiple database context, bookmarks can only be passed among sessions for the same database. This is because the bookmarks (and/or transactions) cannot cross multiple databases in Neo4j 4.x except those generated by the system database.

Example 2. Using system bookmark with another database to ensure the updated system status.
import org.neo4j.driver.Bookmark;
import org.neo4j.driver.Driver;
import org.neo4j.driver.Result;
import org.neo4j.driver.Session;
import org.neo4j.driver.SessionConfig;
...

Bookmark sysBookmark;
try ( Session session = driver.session( SessionConfig.forDatabase( "system" ) ) )
{
   session.writeTransaction( tx -> {
       Result result = tx.run( "CREATE database foo" );
       return result.consume();
   } );
   sysBookmark = session.lastBookmark();
}

try ( Session session = driver.session( SessionConfig.builder().withDatabase( "foo" ).withBookmarks( sysBookmark ).build() ) )
{
   session.writeTransaction( tx -> {
       Result result = tx.run( "CREATE (n)" );
       return result.consume();
   } );
}