Last active
December 21, 2015 06:29
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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