0

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.
firemancer
  • 25
  • 7
  • I don't think this has anything to do with the IN query. Wouldn't you need to read the result of `list()` _inside_ your transaction, not outside? – knittl Jan 22 '23 at 12:37
  • @knittl Yep, you are right. Moving the list() inside of the transaction solved the problem. Thank you. – firemancer Jan 22 '23 at 12:46

1 Answers1

0

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());
    });
}
knittl
  • 246,190
  • 53
  • 318
  • 364