Search in sources :

Example 1 with TransactionEventHandler

use of org.neo4j.graphdb.event.TransactionEventHandler in project neo4j by neo4j.

the class TestTransactionEvents method makeSureBeforeAfterAreCalledCorrectly.

@Test
public void makeSureBeforeAfterAreCalledCorrectly() {
    List<TransactionEventHandler<Object>> handlers = new ArrayList<>();
    handlers.add(new FailingEventHandler<>(new DummyTransactionEventHandler<>(null), false));
    handlers.add(new FailingEventHandler<>(new DummyTransactionEventHandler<>(null), false));
    handlers.add(new FailingEventHandler<>(new DummyTransactionEventHandler<>(null), true));
    handlers.add(new FailingEventHandler<>(new DummyTransactionEventHandler<>(null), false));
    GraphDatabaseService db = dbRule.getGraphDatabaseAPI();
    for (TransactionEventHandler<Object> handler : handlers) {
        db.registerTransactionEventHandler(handler);
    }
    try {
        Transaction tx = db.beginTx();
        try {
            db.createNode().delete();
            tx.success();
            tx.close();
            fail("Should fail commit");
        } catch (TransactionFailureException e) {
        // OK
        }
        verifyHandlerCalls(handlers, false);
        db.unregisterTransactionEventHandler(handlers.remove(2));
        for (TransactionEventHandler<Object> handler : handlers) {
            ((DummyTransactionEventHandler<Object>) ((FailingEventHandler<Object>) handler).source).reset();
        }
        try (Transaction transaction = db.beginTx()) {
            db.createNode().delete();
            transaction.success();
        }
        verifyHandlerCalls(handlers, true);
    } finally {
        for (TransactionEventHandler<Object> handler : handlers) {
            db.unregisterTransactionEventHandler(handler);
        }
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) Transaction(org.neo4j.graphdb.Transaction) TransactionEventHandler(org.neo4j.graphdb.event.TransactionEventHandler) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)1 Transaction (org.neo4j.graphdb.Transaction)1 TransactionFailureException (org.neo4j.graphdb.TransactionFailureException)1 TransactionEventHandler (org.neo4j.graphdb.event.TransactionEventHandler)1