javaneo4jneo4j-java-api

Neo4j Java driver 4.4 IN clause


I want to run the following query however, I am getting some errors. I tested my query on neo4j workspace and it was working. I could not find any source for Java driver using IN query so I am not sure what is wrong with my code. I am using Neo4j Java driver 4.4.

ArrayList<String> changedMethods = ...

Query query = new Query(
                "MATCH (changedFunction: Function) WHERE changedFunction.signature IN $changedMethods \n" +
                "MATCH (affectedFunction: Function)-[:CALLS]->(changedFunction) \n" +
                "RETURN affectedFunction.functionName", parameters("changedMethods", changedMethods));

try (Session session = driver.session(SessionConfig.forDatabase("neo4j"))) {
     List<Record> a = session.readTransaction(tx -> tx.run(query)).list();
     System.out.println(a.get(0).toString());
}

After running this code, I get the following error

org.neo4j.driver.exceptions.ResultConsumedException: Cannot access records on this result any more as the result has already been consumed or the query runner where the result is created has already been closed.

Solution

  • You cannot read the result of a transaction outside the transaction. You have to read the result from inside your transaction:

    try (Session session = driver.session(SessionConfig.forDatabase("neo4j"))) {
         List<Record> a = session.readTransaction(tx -> tx.run(query).list());
         System.out.println(a.get(0).toString());
    }
    

    or

    try (Session session = driver.session(SessionConfig.forDatabase("neo4j"))) {
        session.readTransaction(tx -> {
                List<Record> a = tx.run(query).list();
                System.out.println(a.get(0).toString());
        });
    }