Reputation: 2147
Please review the following code that adds a property to every node in an arbitrarily large graph. An iterator is created within a transaction. During the course of iterating the transaction it was created in is finished and a new one is created (to limit the memory used for the writes that are being made). From that point forward we continue to pull nodes from that iterator but from a different transaction. Is this OK? Are these methods designed to work without a transaction once they are invoked?
The JavaDoc contains a mysterious warning: Please take care that the returned ResourceIterable
is closed correctly and as soon as possible inside your transaction to avoid potential blocking of write operations. Obviously the ResourceIterable
cannot be closed before we are finished iterating, so what is this warning attempting to convey?
Transaction tx = database.beginTx();
try {ResourceIterable<Node> nodes = GlobalGraphOperations.at(database).getAllNodesWithLabel(label);
long i = 0L;
for (Node node : nodes) {
node.setProperty("property", "update");
i++;
if (i % commitInterval == 0) {
tx.success();
tx.close();
tx = database.beginTx();
}
}
tx.success();
}
finally {
tx.close();
}
Upvotes: 3
Views: 108
Reputation: 41706
You shouldn't pull the nodes from the same iterator instance, but you can continue to pull them from a different one, you will see some nodes with updates and some without depending on how far your update has gone through.
Don't use an iterator from another tx somewhere else.
Upvotes: 1
Reputation: 18022
The warning message is trying to say that if you take a lock like this, other things in the DB may not be able to concurrently modify the DB. Since you're doing this globally, this could be problematic for you.
Fortunately, it looks like what you're trying to do can be accomplished much more simply using a single cypher command, then by java and this method. The query would be:
MATCH n SET n.property="update";
You can consult the docs for how to run that cypher query from java.
Also, I think in general GlobalGraphOperations
may be deprecated, so you may wish to examine other ways of mutating the entire graph (like what I'm providing here) rather than using that java class.
Upvotes: 1