Search code examples
neo4jmicronaut

ResultConsumedException with Micronaut and Neo4j: Cannot access records on this result any more as the result has already been consumed


I'm attempting to get a simple app working with Micronaut 1.3.5 and Neo4J 4.0.0. I'm using the io.micronaut.configuration:micronaut-neo4j-bolt:2.0.0 driver. I have code like this:

public Stream<Map<String, Object>> findAll() {
   try (Session s = driver.session()) {
       String stmt = "MATCH (n:Product) RETURN n";
       return s.readTransaction(tx -> tx.run(stmt)).list(r -> r.asMap()).stream();
   }
}

And I'm getting the following exception:

result has already been consumed or the query runner where the result is created has already been closed.
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.

I've done some googling and found a few things that would suggest this is normal behavior, but nothing that discusses the actual solution. I'm assuming it has something to do with the async nature of the driver/session.


Solution

  • The way that would work is,

    Stream<Map<String, Object>> stream = s.readTransaction(tx -> {
        Result result = tx.run(stmt);
        return result.list(r -> r.asMap()).stream();
     });
    

    The results need to be processed within the readTransaction

    There are bunch of examples at https://github.com/neo4j/neo4j-java-driver/tree/4.1/examples/src/main/java/org/neo4j/docs/driver which you may find useful