Search in sources :

Example 1 with TransactionEventListener

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

the class DatabaseTransactionEventListeners method afterCommit.

public void afterCommit(TransactionListenersState listeners) {
    if (listeners == null) {
        // observe them.
        return;
    }
    TransactionData txData = listeners.getTxData();
    try {
        for (ListenerState listenerState : listeners.getStates()) {
            TransactionEventListener listener = listenerState.getListener();
            listener.afterCommit(txData, listenerState.getState(), databaseFacade);
        }
    } finally {
        if (txData instanceof TxStateTransactionDataSnapshot) {
            ((TxStateTransactionDataSnapshot) txData).close();
        }
    // else if could be EMPTY_DATA as well, and we don't want the user-facing TransactionData interface to have close() on it
    }
}
Also used : ListenerState(org.neo4j.kernel.internal.event.TransactionListenersState.ListenerState) TransactionData(org.neo4j.graphdb.event.TransactionData) TransactionEventListener(org.neo4j.graphdb.event.TransactionEventListener)

Example 2 with TransactionEventListener

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

the class DatabaseTransactionEventListeners method afterRollback.

public void afterRollback(TransactionListenersState listenersState) {
    if (listenersState == null) {
        // For legacy reasons, we don't call transaction listeners on implicit rollback.
        return;
    }
    TransactionData txData = listenersState.getTxData();
    for (ListenerState listenerState : listenersState.getStates()) {
        TransactionEventListener listener = listenerState.getListener();
        listener.afterRollback(txData, listenerState.getState(), databaseFacade);
    }
}
Also used : ListenerState(org.neo4j.kernel.internal.event.TransactionListenersState.ListenerState) TransactionData(org.neo4j.graphdb.event.TransactionData) TransactionEventListener(org.neo4j.graphdb.event.TransactionEventListener)

Example 3 with TransactionEventListener

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

the class TestTransactionEvents method makeSureBeforeAfterAreCalledCorrectly.

@Test
void makeSureBeforeAfterAreCalledCorrectly() {
    List<TransactionEventListener<Object>> listeners = new ArrayList<>();
    listeners.add(new FailingEventListener<>(new DummyTransactionEventListener<>(null), false));
    listeners.add(new FailingEventListener<>(new DummyTransactionEventListener<>(null), false));
    listeners.add(new FailingEventListener<>(new DummyTransactionEventListener<>(null), true));
    listeners.add(new FailingEventListener<>(new DummyTransactionEventListener<>(null), false));
    for (TransactionEventListener<Object> listener : listeners) {
        dbms.registerTransactionEventListener(DEFAULT_DATABASE_NAME, listener);
    }
    try {
        Transaction tx = db.beginTx();
        try {
            tx.createNode().delete();
            tx.commit();
            fail("Should fail commit");
        } catch (TransactionFailureException e) {
        // OK
        }
        verifyListenerCalls(listeners, false);
        dbms.unregisterTransactionEventListener(DEFAULT_DATABASE_NAME, listeners.remove(2));
        for (TransactionEventListener<Object> listener : listeners) {
            ((DummyTransactionEventListener<Object>) ((FailingEventListener<Object>) listener).source).reset();
        }
        try (Transaction transaction = db.beginTx()) {
            tx.createNode().delete();
            transaction.commit();
        }
        verifyListenerCalls(listeners, true);
    } finally {
        for (TransactionEventListener<Object> listener : listeners) {
            dbms.unregisterTransactionEventListener(DEFAULT_DATABASE_NAME, listener);
        }
    }
}
Also used : TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) Transaction(org.neo4j.graphdb.Transaction) ArrayList(java.util.ArrayList) TransactionEventListener(org.neo4j.graphdb.event.TransactionEventListener) InternalTransactionEventListener(org.neo4j.kernel.internal.event.InternalTransactionEventListener) Test(org.junit.jupiter.api.Test)

Example 4 with TransactionEventListener

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

the class DatabaseTransactionEventListeners method beforeCommit.

public TransactionListenersState beforeCommit(ReadableTransactionState state, KernelTransaction transaction, StorageReader storageReader) {
    // The iterator grabs a snapshot of our list of listenerIterator
    Collection<TransactionEventListener<?>> eventListeners = globalTransactionEventListeners.getDatabaseTransactionEventListeners(databaseName);
    if (!canInvokeBeforeCommitListeners(eventListeners, state)) {
        // Use 'null' as a signal that no event listenerIterator were registered at beforeCommit time
        return null;
    }
    TransactionData txData = new TxStateTransactionDataSnapshot(state, storageReader, transaction);
    TransactionListenersState listenersStates = new TransactionListenersState(txData);
    boolean hasDataChanges = state.hasDataChanges();
    boolean isSystem = SYSTEM_DATABASE_NAME.equals(databaseName);
    for (TransactionEventListener<?> listener : eventListeners) {
        boolean internal = listener instanceof InternalTransactionEventListener;
        if (hasDataChanges || internal || isSystem) {
            Object listenerState = null;
            try {
                listenerState = !internal ? listener.beforeCommit(txData, transaction.internalTransaction(), databaseFacade) : ((InternalTransactionEventListener) listener).beforeCommit(txData, transaction, databaseFacade);
            } catch (Throwable t) {
                listenersStates.failed(t);
            }
            listenersStates.addListenerState(listener, listenerState);
        }
    }
    return listenersStates;
}
Also used : TransactionData(org.neo4j.graphdb.event.TransactionData) TransactionEventListener(org.neo4j.graphdb.event.TransactionEventListener)

Aggregations

TransactionEventListener (org.neo4j.graphdb.event.TransactionEventListener)4 TransactionData (org.neo4j.graphdb.event.TransactionData)3 ListenerState (org.neo4j.kernel.internal.event.TransactionListenersState.ListenerState)2 ArrayList (java.util.ArrayList)1 Test (org.junit.jupiter.api.Test)1 Transaction (org.neo4j.graphdb.Transaction)1 TransactionFailureException (org.neo4j.graphdb.TransactionFailureException)1 InternalTransactionEventListener (org.neo4j.kernel.internal.event.InternalTransactionEventListener)1