问题
This might be typical Java 7 boilerplate for accessing some Neo4j data.
Is there any mechanism in existence whereby you can set up the transaction to automatically fail and rollback based upon a timeout?
try (Transaction tx = graphdb.beginTx()) {
Node node = // Get some Nodes ...
Iterable<Relationship> rels = node.getRelationships(...);
for (Relationship rel : rels) {
// Oh no! This is a super-node with a billion Relationships!
}
tx.success();
return data;
}
I guess just have int count = 0
and increment each iteration, and then:
if(count > XYZ) throw TakingTooLongException(count)
?
回答1:
You can also just check with node.getDegree()
upfront before your loop, which is a constant time operation for dense nodes and a small counter for all others (<50 rels).
Otherwise there is also an execution guard, but I'm not sure for how long it will survive.
You can just return from the tx without calling tx.success()
and it should be rolled back.
来源:https://stackoverflow.com/questions/33745865/is-it-possible-to-force-a-neo4j-transaction-to-timeout