Skip to content

Instantly share code, notes, and snippets.

@bachmanm
Last active December 21, 2015 06:29
Show Gist options
  • Save bachmanm/6264777 to your computer and use it in GitHub Desktop.
Save bachmanm/6264777 to your computer and use it in GitHub Desktop.
A failed attempt to log each about-to-be-deleted node and its properties in Neo4j (for a blog post)
@Test(expected = TransactionFailureException.class)
public void attemptLoggingDeletedNodes() {
GraphDatabaseService database = new TestGraphDatabaseFactory().newImpermanentDatabase();
database.registerTransactionEventHandler(new TransactionEventHandler.Adapter<Void>() {
@Override
public Void beforeCommit(TransactionData data) throws Exception {
for (Node deletedNode : data.deletedNodes()) {
StringBuilder message = new StringBuilder("About to delete node ID ")
.append(deletedNode.getId())
.append(" ");
for (String key : deletedNode.getPropertyKeys()) {
message.append("key=").append(key);
message.append("value=").append(deletedNode.getProperty(key));
}
System.out.println(message.toString());
}
return null;
}
});
Transaction tx = database.beginTx();
try {
database.getNodeById(0).setProperty("test key", "test value");
tx.success();
} finally {
tx.finish();
}
tx = database.beginTx();
try {
database.getNodeById(0).delete();
tx.success();
} finally {
tx.finish();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment