Skip to content

Instantly share code, notes, and snippets.

View bachmanm's full-sized avatar

Michal Bachman bachmanm

View GitHub Profile
@bachmanm
bachmanm / neo4j-tx-event-handler-101.java
Last active March 19, 2020 13:17
An example of a Neo4j transaction event handler (for a blog post)
@Test
public void showSimpleEventHandling() {
GraphDatabaseService database = new TestGraphDatabaseFactory().newImpermanentDatabase();
database.registerTransactionEventHandler(new TransactionEventHandler<Void>() {
@Override
public Void beforeCommit(TransactionData data) throws Exception {
System.out.println("Committing transaction");
return null;
}
@bachmanm
bachmanm / neo4j-tx-event-handling-failed.java
Last active December 21, 2015 06:29
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())
@bachmanm
bachmanm / ImprovedTransactionData.java
Created September 26, 2013 16:02
ImprovedTransactionData with methods for nodes only, JavaDoc ommitted
public interface ImprovedTransactionData {
boolean hasBeenCreated(Node node);
Collection<Node> getAllCreatedNodes();
boolean hasBeenDeleted(Node node);
Node getDeleted(Node node);
Collection<Node> getAllDeletedNodes();
boolean hasBeenChanged(Node node);