Search in sources :

Example 1 with TransactionHook

use of org.neo4j.kernel.api.TransactionHook in project neo4j by neo4j.

the class TransactionHookIT method shouldRecieveTxStateOnCommit.

@Test
public void shouldRecieveTxStateOnCommit() throws Exception {
    // Given
    TransactionHook hook = mock(TransactionHook.class);
    kernel.registerTransactionHook(hook);
    // When
    DataWriteOperations ops = dataWriteOperationsInNewTransaction();
    ops.nodeCreate();
    commit();
    // Then
    verify(hook).beforeCommit(any(ReadableTransactionState.class), any(KernelTransaction.class), any(StoreReadLayer.class), any(StorageStatement.class));
    verify(hook).afterCommit(any(ReadableTransactionState.class), any(KernelTransaction.class), any(TransactionHook.Outcome.class));
    verifyNoMoreInteractions(hook);
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) TransactionHook(org.neo4j.kernel.api.TransactionHook) StorageStatement(org.neo4j.storageengine.api.StorageStatement) DataWriteOperations(org.neo4j.kernel.api.DataWriteOperations) StoreReadLayer(org.neo4j.storageengine.api.StoreReadLayer) ReadableTransactionState(org.neo4j.storageengine.api.txstate.ReadableTransactionState) Test(org.junit.Test)

Example 2 with TransactionHook

use of org.neo4j.kernel.api.TransactionHook in project neo4j by neo4j.

the class TransactionHookIT method shouldRollbackOnFailureInBeforeCommit.

@Test
public void shouldRollbackOnFailureInBeforeCommit() throws Exception {
    // Given
    TransactionHook hook = mock(TransactionHook.class);
    final String message = "Original";
    when(hook.beforeCommit(any(ReadableTransactionState.class), any(KernelTransaction.class), any(StoreReadLayer.class), any(StorageStatement.class))).thenReturn(new TransactionHook.Outcome() {

        @Override
        public boolean isSuccessful() {
            return false;
        }

        @Override
        public Throwable failure() {
            return new Throwable(message);
        }
    });
    kernel.registerTransactionHook(hook);
    // When
    DataWriteOperations ops = dataWriteOperationsInNewTransaction();
    ops.nodeCreate();
    try {
        commit();
        fail("Expected this to fail.");
    } catch (org.neo4j.kernel.api.exceptions.TransactionFailureException e) {
        assertThat(e.getCause().getMessage(), equalTo("Transaction handler failed."));
        assertThat(e.getCause().getCause().getMessage(), equalTo(message));
    }
    // Then
    verify(hook).beforeCommit(any(ReadableTransactionState.class), any(KernelTransaction.class), any(StoreReadLayer.class), any(StorageStatement.class));
    verify(hook).afterRollback(any(ReadableTransactionState.class), any(KernelTransaction.class), any(TransactionHook.Outcome.class));
    verifyNoMoreInteractions(hook);
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) StorageStatement(org.neo4j.storageengine.api.StorageStatement) StoreReadLayer(org.neo4j.storageengine.api.StoreReadLayer) DataWriteOperations(org.neo4j.kernel.api.DataWriteOperations) ReadableTransactionState(org.neo4j.storageengine.api.txstate.ReadableTransactionState) TransactionHook(org.neo4j.kernel.api.TransactionHook) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 DataWriteOperations (org.neo4j.kernel.api.DataWriteOperations)2 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)2 TransactionHook (org.neo4j.kernel.api.TransactionHook)2 StorageStatement (org.neo4j.storageengine.api.StorageStatement)2 StoreReadLayer (org.neo4j.storageengine.api.StoreReadLayer)2 ReadableTransactionState (org.neo4j.storageengine.api.txstate.ReadableTransactionState)2