Java driver issue

Hi, I have read jdbc driver issue before, right I have the same issue. Here is the example I copied from neo4j website, it runs with jdbc error.

import org.neo4j.driver.*;
import static org.neo4j.driver.Values.parameters;

public class SmallExample
{
	    // Driver objects are thread-safe and are typically made available application-wide.
	    Driver driver;

	    public SmallExample(String uri, String user, String password)
	    {
	        driver = GraphDatabase.driver(uri, AuthTokens.basic(user, password));
	       
	    }

	    private void addPerson(String name)
	    {
	        // Sessions are lightweight and disposable connection wrappers.
	        try (Session session = driver.session())
	        {
	            // Wrapping a Cypher Query in a Managed Transaction provides atomicity
	            // and makes handling errors much easier.
	            // Use `session.writeTransaction` for writes and `session.readTransaction` for reading data.
	            // These methods are also able to handle connection problems and transient errors using an automatic retry mechanism.
	            session.writeTransaction(tx -> tx.run("MERGE (a:Person {name: $x})", parameters("x", name)));
	        }
	    }

	    private void printPeople(String initial)
	    {
	        try (Session session = driver.session())
	        {
	            // A Managed transaction is a quick and easy way to wrap a Cypher Query.
	            // The `session.run` method will run the specified Query.
	            // This simpler method does not use any automatic retry mechanism.
	            Result result = session.run(
	                    "MATCH (a:Person) WHERE a.name STARTS WITH $x RETURN a.name AS name",
	                    parameters("x", initial));
	            // Each Cypher execution returns a stream of records.
	            while (result.hasNext())
	            {
	                org.neo4j.driver.Record record = result.next();
	                // Values can be extracted from a record by index or name.
	                System.out.println(record.get("name").asString());
	            }
	        }
	    }

	    public void close()
	    {
	        // Closing a driver immediately shuts down all open connections.
	        driver.close();
	    }
   
	    public static void main(String... args)
	    {
	        SmallExample example = new SmallExample("bolt://localhost:7687", "neo4j", "password");
	        example.addPerson("Ada");
	        example.addPerson("Alice");
	        example.addPerson("Bob");
	        example.printPeople("A");
	        example.close();
	    }
}

The error is "INFO: Closing connection pool towards localhost:7687
java.sql.SQLException: org.neo4j.driver.v1.exceptions.ServiceUnavailableException: Connection to the database terminated. This can happen due to network instabilities, or due to restarts of the database." error points to this line:

driver = GraphDatabase.driver(uri, AuthTokens.basic(user, password));

Any helps are appreicated. Thanks.

Hi @jzhou002 ,

A few questions:

How are you running Neo4j?

Are you able to connect to the local server using Neo4j browser? With the Neo4j DBMS running, try opening a web browser at http://localhost:7474 -- this should load the Neo4j Browser, where you can login to the database.

Is the connection string used by Neo4j Browser the same as you are using in the driver?

Best,
ABK

Hi Adreas, thanks for the reply. I run neo4j locally, I can use Chrome to connect to neo4j http://localhost?:7687 port, that part runs smoothly. The java program I used was copied/paste from neo4j examples, the error points to jdbc:neo4j.bolt://localhost, it seems the uri got nulled by the jdbc driver. So I do not know what else I should look at.
Thanks.

Hello, you are using a JDBC URI with a non-JDBC driver, so it is not going to work.
The code you posted is an example of using the native Java Bolt driver, not a JDBC driver.
The URI you want to use here is bolt://localhost.

P.S.: if you could point us to the README you were reading, there is probably something we need to fix there

This is also from neo4j example, it uses "bolt://localhost", and error point to the uri

import org.neo4j.driver.*;
import org.neo4j.driver.exceptions.DatabaseException;
import static org.neo4j.driver.Values.parameters;

public class SmallExample
{
	    // Driver objects are thread-safe and are typically made available application-wide.
	    Driver driver;

	    public SmallExample(String uri, String user, String password) throws DatabaseException
	    {
	        driver = GraphDatabase.driver(uri, AuthTokens.basic(user, password));
	        driver.verifyConnectivity();
	    }

	    private void addPerson(String name)
	    {
	        // Sessions are lightweight and disposable connection wrappers.
	        try (Session session = driver.session())
	        {
	            // Wrapping a Cypher Query in a Managed Transaction provides atomicity
	            // and makes handling errors much easier.
	            // Use `session.writeTransaction` for writes and `session.readTransaction` for reading data.
	            // These methods are also able to handle connection problems and transient errors using an automatic retry mechanism.
	            session.writeTransaction(tx -> tx.run("MERGE (a:Person {name: $x})", parameters("x", name)));
	        }
	    }

	    private void printPeople(String initial)
	    {
	        try (Session session = driver.session())
	        {
	            // A Managed transaction is a quick and easy way to wrap a Cypher Query.
	            // The `session.run` method will run the specified Query.
	            // This simpler method does not use any automatic retry mechanism.
	            Result result = session.run(
	                    "MATCH (a:Person) WHERE a.name STARTS WITH $x RETURN a.name AS name",
	                    parameters("x", initial));
	            // Each Cypher execution returns a stream of records.
	            while (result.hasNext())
	            {
	                org.neo4j.driver.Record record = result.next();
	                // Values can be extracted from a record by index or name.
	                System.out.println(record.get("name").asString());
	            }
	        }
	    }

	    public void close()
	    {
	        // Closing a driver immediately shuts down all open connections.
	        driver.close();
	    }
   
	    public static void main(String... args)
	    {
	        SmallExample example = new SmallExample("bolt://localhost:7687", "neo4j", "password");
	        example.addPerson("Ada");
	        example.addPerson("Alice");
	        example.addPerson("Bob");
	        example.printPeople("A");
	        example.close();
	    }
}

Here is where the debug stops, the program is "Values.java", the info is "Values:() line:85

public abstract class Values
{
    public static final Value EmptyMap = value( Collections.emptyMap() );
    public static final Value NULL = NullValue.NULL;

Thank you.

I have neo4j-java-driver-4.3.4.jar and neo4j-jdbc-driver-3.4.0.jar in the build-path.

The code looks fine. Seems to be a connectivity issue with your local Neo4j server.
How do you start it?

I started neo4jDesktop, then start the neo4j default database. From Chrom browser, I use http://localhost::7474/browser, it reads " You are connected as user neo4jto neo4j://localhost:7687 Connection credentials are stored in your web browser." I use windows 10.

I open eclipse to bring up the java program, debug it, then th error message occurs as presented in previous posts.

python driver works perfectly, only jdbc driver not work.

You keep mentioning the JDBC driver, but the only code you have shown is from the Java Bolt driver, not the JDBC driver. Which one are you using:

Ooops, I posted two the same examples. This is the different java I meant post .

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class neo4jjdbc {

	protected static Connection neoConnection() throws SQLException {
	    return DriverManager.getConnection("jdbc:neo4j:bolt://localhost:7687", "neo4j","password");
	}
	
	public static void main(String[] args) 
	{
		try
		{
			Connection connection = neoConnection();			
			connection.close();
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}

the error point to getConnection. Is this a jdbc example?

I assume you are using a 4.3.x Neo4j server in Neo4j Desktop (or at least a 4.x).
If this is the case, you're using a JDBC driver that is too old.
Please use a recent release like https://github.com/neo4j-contrib/neo4j-jdbc/releases/tag/4.0.4 for instance.

1 Like

That solves the issue, errors are gone. Now I try to manipulate data in neo4j. Thank you so very much for your patience and helps, really appreciate.

1 Like

Glad to read it worked out!