Home > Software engineering >  Neo4j Java driver 4.4 IN clause
Neo4j Java driver 4.4 IN clause

Time:01-23

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.

CodePudding user response:

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());
    });
}
  • Related