Resource iterator

Inside a long-running transaction, it is а good practice to ensure that any org.neo4j.graphdb.ResourceIterator obtained inside the transaction are closed as early as possible. This is either achieved by exhausting the iterator or by explicitly calling its close method.

The following is an example of how to work with ResourceIterator. If you do not exhaust the iterator, you can close it explicitly using the close() method.

Label label = Label.label( "User" );
int idToFind = 45;
String nameToFind = "user" + idToFind + "@neo4j.org";
try ( Transaction tx = graphDb.beginTx();
      ResourceIterator<Node> users = tx.findNodes( label, "username", nameToFind ) )
{
    Node firstUserNode;
    if ( users.hasNext() )
    {
        firstUserNode = users.next();
    }
    users.close();
    // ... Do stuff with the firstUserNode we found ...
}