Drivers changes

This section the difference between the Neo4j 1.7 drivers and the Neo4j 4.x drivers.

1. High-level changes

From 4.0, the drivers are built to provide a user-friendly and unified API across all languages.

In previous versions of Neo4j, client-server communication uses encrypted local connections and generates a self-signed certificate out of the box. From 4.0 however, the default is set to unencrypted. For more information, see Driver Manual → Connection URIs.

Neo4j 4.0 introduces a reactive API compatible with the Reactive Streams standard. This enables fine-grained control of the data flow for Cypher query results, including the ability to pause or cancel part-way through. For more information, see Driver Manual → Queries and results.

When using the 4.x driver to connect to a 4.x database, it is possible to work with multiple databases. From a driver API perspective, this means that one database must be selected for use as an execution context for transactions within a session. This can be configured on session construction. If no database is selected, the driver connects to the server’s default database.

Drivers 1.7 work in fallback mode with Neo4j 4.x. They do not support features introduced in Neo4j 4.0 and later, such as multiple databases, Neo4j Fabric, and fine-grained access control. To run multiple databases online concurrently and do distributed queries over them, you must migrate from 1.7 to 4.x.

The examples in this chapter are mainly written in Java, using the Java driver. However, similar code can be translated to other languages.

2. New features

  • Starting with Neo4j 4.0, the versioning scheme for the database, driver, and protocol are all aligned. For supported drivers, this means that the version number goes from 1.7 to any 4.x.

  • A 1.7+ driver communicating with a 4.x server may need encryption explicitly switched off. This is due to a change in the defaults between Neo4j 3.x and 4.x.

  • Bolt v4.0 is implemented in both 4.x drivers and 4.0 servers.

  • Reactive API is now available with 4.0 servers. To make use of the reactive API, the starting point is RxSession on the driver object.

  • With 4.0 servers, session instances should now be acquired against a specific database. Causal chaining is still respected on each database (transactions cannot span across multiple databases). The driver itself connects to Neo4j DBMS.

  • A new feature detection method driver.supportsMultiDb() is added for querying whether the remote database supports multiple databases.

  • A new driver.verifyConnectivity() method is introduced for connectivity verification purposes. By default, the driver instances do not verify DBMS availability after construction.

  • New connection URI schemes with variants that contain extra encryption and trust information - neo4j+s, bolt+s, neo4j+ssc, and bolt+ssc. The +s variants enable encryption with a full certificate check, and the +ssc variants enable encryption with no certificate check. The latter variant is designed specifically for use with self-signed certificates. For more information, see Additional URI Schemes.

Example 1. Detecting multiple database support
import org.neo4j.driver.Driver;
import org.neo4j.driver.Result;
import org.neo4j.driver.Session;
import org.neo4j.driver.SessionConfig;
import org.neo4j.driver.Values;

...
private final Driver driver;
...

public void printGreeting( final String message )
{
   SessionConfig sessionConfig = driver.supportsMultiDb() ? SessionConfig.forDatabase( "neo4j" )
                                                          : SessionConfig.defaultConfig();

   try ( Session session = driver.session( sessionConfig ) )
   {
       String greeting = session.writeTransaction( tx -> {
           Result result = tx.run( "CREATE (a:Greeting) SET a.message = $message RETURN a.message + ', from node ' + id(a)",
                                   Values.parameters( "message", message ) );
           return result.single().get( 0 ).asString();
       } );
       System.out.println( greeting );
   }
}