Search in sources :

Example 6 with BoltTransaction

use of org.neo4j.bolt.dbapi.BoltTransaction in project neo4j by neo4j.

the class TransactionStateMachineTest method shouldResetInExplicitTransactionWhileStatementIsRunningWhenValidated.

@Test
void shouldResetInExplicitTransactionWhileStatementIsRunningWhenValidated() throws Exception {
    BoltTransaction transaction = newTimedOutTransaction();
    TransactionStateMachineSPI stateMachineSPI = newTransactionStateMachineSPI(transaction);
    TransactionStateMachine stateMachine = newTransactionStateMachine(stateMachineSPI);
    // start an explicit transaction
    beginTx(stateMachine);
    assertThat(stateMachine.state).isEqualTo(TransactionStateMachine.State.EXPLICIT_TRANSACTION);
    assertNotNull(stateMachine.ctx.currentTransaction);
    stateMachine.run("RETURN 1", null);
    // verify transaction, which is timed out
    stateMachine.validateTransaction();
    assertThat(stateMachine.state).isEqualTo(TransactionStateMachine.State.AUTO_COMMIT);
    assertNull(stateMachine.ctx.currentTransaction);
    assertThat(stateMachine.ctx.statementOutcomes.entrySet()).hasSize(0);
    verify(transaction).getReasonIfTerminated();
    verify(transaction).rollback();
}
Also used : BoltTransaction(org.neo4j.bolt.dbapi.BoltTransaction) TransactionStateMachineSPI(org.neo4j.bolt.runtime.statemachine.TransactionStateMachineSPI) Test(org.junit.jupiter.api.Test)

Example 7 with BoltTransaction

use of org.neo4j.bolt.dbapi.BoltTransaction in project neo4j by neo4j.

the class TransactionStateMachineTest method newTimedOutTransaction.

private static BoltTransaction newTimedOutTransaction() {
    BoltTransaction transaction = newTransaction();
    when(transaction.getReasonIfTerminated()).thenReturn(Optional.of(Status.Transaction.TransactionTimedOut));
    return transaction;
}
Also used : BoltTransaction(org.neo4j.bolt.dbapi.BoltTransaction)

Example 8 with BoltTransaction

use of org.neo4j.bolt.dbapi.BoltTransaction in project neo4j by neo4j.

the class TransactionStateMachineTest method shouldDoNothingInAutoCommitTransactionUponInitialisationWhenValidated.

@Test
void shouldDoNothingInAutoCommitTransactionUponInitialisationWhenValidated() throws Exception {
    BoltTransaction transaction = newTimedOutTransaction();
    TransactionStateMachineSPI stateMachineSPI = newTransactionStateMachineSPI(transaction);
    TransactionStateMachine stateMachine = newTransactionStateMachine(stateMachineSPI);
    // We're in auto-commit state
    assertThat(stateMachine.state).isEqualTo(TransactionStateMachine.State.AUTO_COMMIT);
    assertNull(stateMachine.ctx.currentTransaction);
    // call validate transaction
    stateMachine.validateTransaction();
    assertThat(stateMachine.state).isEqualTo(TransactionStateMachine.State.AUTO_COMMIT);
    assertNull(stateMachine.ctx.currentTransaction);
    verify(transaction, never()).getReasonIfTerminated();
    verify(transaction, never()).rollback();
}
Also used : BoltTransaction(org.neo4j.bolt.dbapi.BoltTransaction) TransactionStateMachineSPI(org.neo4j.bolt.runtime.statemachine.TransactionStateMachineSPI) Test(org.junit.jupiter.api.Test)

Example 9 with BoltTransaction

use of org.neo4j.bolt.dbapi.BoltTransaction in project neo4j by neo4j.

the class TransactionStateMachineTest method shouldTryToTerminateAllActiveStatements.

@Test
void shouldTryToTerminateAllActiveStatements() throws Exception {
    BoltTransaction transaction = newTimedOutTransaction();
    BoltResultHandle resultHandle = newResultHandle();
    doThrow(new RuntimeException("You shall not pass")).doThrow(new RuntimeException("Not pass twice")).when(resultHandle).terminate();
    TransactionStateMachineSPI stateMachineSPI = mock(TransactionStateMachineSPI.class);
    when(stateMachineSPI.beginTransaction(any(), any(), any(), any(), any(), any())).thenReturn(transaction);
    when(stateMachineSPI.executeQuery(any(), anyString(), any())).thenReturn(resultHandle);
    // V4
    when(stateMachineSPI.supportsNestedStatementsInTransaction()).thenReturn(true);
    TransactionStateMachine stateMachine = newTransactionStateMachine(stateMachineSPI);
    // We're in explicit-commit state
    beginTx(stateMachine, List.of());
    assertThat(stateMachine.state).isEqualTo(TransactionStateMachine.State.EXPLICIT_TRANSACTION);
    assertNotNull(stateMachine.ctx.currentTransaction);
    // We run two statements
    stateMachine.run("RETURN 1", null);
    stateMachine.run("RETURN 2", null);
    assertThat(stateMachine.state).isEqualTo(TransactionStateMachine.State.EXPLICIT_TRANSACTION);
    assertNotNull(stateMachine.ctx.currentTransaction);
    assertThat(stateMachine.ctx.statementCounter).isEqualTo(2);
    RuntimeException error = assertThrows(RuntimeException.class, () -> stateMachine.reset());
    assertThat(error.getCause().getMessage()).isEqualTo("You shall not pass");
    assertThat(error.getSuppressed().length).isEqualTo(1);
    assertThat(error.getSuppressed()[0].getMessage()).isEqualTo("Not pass twice");
}
Also used : BoltTransaction(org.neo4j.bolt.dbapi.BoltTransaction) BoltResultHandle(org.neo4j.bolt.runtime.BoltResultHandle) TransactionStateMachineSPI(org.neo4j.bolt.runtime.statemachine.TransactionStateMachineSPI) Test(org.junit.jupiter.api.Test)

Example 10 with BoltTransaction

use of org.neo4j.bolt.dbapi.BoltTransaction in project neo4j by neo4j.

the class TransactionStateMachineTest method shouldCloseResultHandlesWhenExecutionFailsInExplicitTransaction.

@Test
void shouldCloseResultHandlesWhenExecutionFailsInExplicitTransaction() throws Exception {
    BoltTransaction transaction = newTransaction();
    BoltResultHandle resultHandle = newResultHandle(new RuntimeException("some error"));
    TransactionStateMachineSPI stateMachineSPI = newTransactionStateMachineSPI(transaction, resultHandle);
    TransactionStateMachine stateMachine = newTransactionStateMachine(stateMachineSPI);
    RuntimeException e = assertThrows(RuntimeException.class, () -> {
        beginTx(stateMachine);
        stateMachine.run("SOME STATEMENT", null);
    });
    assertEquals("some error", e.getMessage());
    assertThat(stateMachine.ctx.statementOutcomes.entrySet()).hasSize(0);
    assertNotNull(stateMachine.ctx.currentTransaction);
}
Also used : BoltTransaction(org.neo4j.bolt.dbapi.BoltTransaction) BoltResultHandle(org.neo4j.bolt.runtime.BoltResultHandle) TransactionStateMachineSPI(org.neo4j.bolt.runtime.statemachine.TransactionStateMachineSPI) Test(org.junit.jupiter.api.Test)

Aggregations

BoltTransaction (org.neo4j.bolt.dbapi.BoltTransaction)16 Test (org.junit.jupiter.api.Test)13 TransactionStateMachineSPI (org.neo4j.bolt.runtime.statemachine.TransactionStateMachineSPI)13 BoltResultHandle (org.neo4j.bolt.runtime.BoltResultHandle)3 StatementOutcome (org.neo4j.bolt.runtime.statemachine.impl.TransactionStateMachine.StatementOutcome)2 InOrder (org.mockito.InOrder)1 BoltQueryExecutor (org.neo4j.bolt.dbapi.BoltQueryExecutor)1 TransactionFailureException (org.neo4j.internal.kernel.api.exceptions.TransactionFailureException)1 LoginContext (org.neo4j.internal.kernel.api.security.LoginContext)1 Status (org.neo4j.kernel.api.exceptions.Status)1