Liking cljdoc? Tell your friends :D

Neo4j JDBC Driver Documentation

Technical Reference

The HTTP Driver

The HTTP driver uses the existing (since Neo4j 2.x) transaction Cypher HTTP API, and is implemented using Apache Commons httpclient and Jackson for JSON (de)serialization.

The JDBC URL has this format: jdbc:neo4j:http://host:port/?username=neo4j,password=xxxx

The Bolt Driver

The Bolt driver connects to any Neo4j 3.x database that has the binary "Bolt" transport enabled.

It uses the neo4j-java-driver to provide the connectivity.

Just provide your username and password and you can connect to Neo4j.

The JDBC URL has this format: jdbc:neo4j:bolt://host:port/?username=neo4j,password=xxxx

The Bolt+Routing Driver

Starting from version 3.3.1, the JDBC driver brings with it the bolt+routing protocol.

Again, it uses the neo4j-java-driver to provide the connectivity.

If you’re connecting to a Neo4j Causal Cluster the JDBC URL has this format:

jdbc:neo4j:bolt+routing://host1:port1,host2:port2,…​,hostN:portN/?username=neo4j,password=xxxx

The list of "hostX:portX" pairs in the URL correspond to the list of servers that are partecipating in the Neo4j cluster (both as Core and Read-Replica nodes), but if you or your preferred tool don’t like this format you can then fall back to the dedicated parameter "routing:servers", as in the following example:

jdbc:neo4j:bolt+routing://host1:port1?username=neo4j,password=xxxx,routing:region=EU&country=Italy&routing:servers=host2:port2;…​;hostN:portN

In that case, the address in the URL must be that of a Core server and the alternative servers must be ; separated (instead of ,).

Routing Context

Routing driver with routing context is an available option with a Neo4j Causal Cluster of version 3.2 or above.

In such a setup you can include a preferred routing context via the "routing:policy" parameter.

jdbc:neo4j:bolt+routing://host1:port1,host2:port2,…​,hostN:portN?username=neo4j,password=xxxx,routing:policy=EU

And for custom routing strategies via the generic "routing:" parameter:

jdbc:neo4j:bolt+routing://host1:port1,host2:port2,…​,hostN:portN?username=neo4j,password=xxxx,routing:region=EU&country=Italy

Access Mode (READ, WRITE)

In order to start a transaction in read or write mode just use the Connection#setReadOnly method.

Be aware not to invoke it while the transaction is open otherwise the driver will raise an SQLException.

If you’re accessing to a Neo4j Causal Cluster, by calling this method write operations will be forwarded to CORE instances, while read operations will be managed by READ REPLICA nodes.

Bookmarks

When working with a causal cluster, causal chaining is carried out by passing bookmarks between transactions in a session (see causal chaining).

The JDBC driver allows you to read bookmarks by calling the following method:

connection.getClientInfo(BoltRoutingNeo4jDriver.BOOKMARK);

Also you can set the bookmark by calling the corresponding method:

connection.setClientInfo(BoltRoutingNeo4jDriver.BOOKMARK, "my bookmark");

Bolt+Routing with Bookmark Example

Run Query
String connectionUrl = "jdbc:neo4j:bolt+routing://localhost:17681,localhost:17682,localhost:17683,localhost:17684,localhost:17685,localhost:17686,localhost:17687?noSsl&routing:policy=EU";

try  (Connection connection = DriverManager.getConnection(connectionUrl, "neo4j", password)) {
    connection.setAutoCommit(false);

    // Access to CORE nodes, as the connection is opened by default in write mode
    try (Statement statement = connection.createStatement()) {
        statement.execute("create (:BoltRoutingTest { protocol: 'BOLT+ROUTING' })");
    }

    // closing transaction before changing access mode
    connection.commit();

    // printing the transaction bookmark
    String bookmark = connection.getClientInfo(BoltRoutingNeo4jDriver.BOOKMARK);
    System.out.println(bookmark);

    // Switching to read-only mode to access READ REPLICA nodes
    connection.setReadOnly(true);

    try (Statement statement = connection.createStatement()) {
        try (ResultSet resultSet = statement.executeQuery("match (t:BoltRoutingTest) return count(t) as tot")) {
            if (resultSet.next()) {
                Long tot = resultSet.getLong("tot");
            }
        }
    }

    connection.commit();
}

JDBC Compatibility

We cover these aspects of the JDBC-APIs, everything that’s not explicitely mentioned should be assumed to be not implemented:

  • Driver handling automatic loading and JDBC URLs

  • Connection, incl. autocommit and manual commit

  • Read-only and write transactions

  • Statements for reads and writes

  • PreparedStatement for reads and writes, including parameters, both question marks ? and named numbered parameters {1}

  • ResultSet retrieving all columns as String, Object and their native type with optional conversions

Libraries and Frameworks

Java JDBC Usage

Plain JDBC usage was already shown before:

Can you improve this documentation? These fine people already did:
Lorenzo Speranzoni, Michael Hunger, Benoît Simard (@logisima) & Frank Pavageau
Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close