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
}
}
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);
}
}
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);
}
}
}
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;
}
Aggregations