Search in sources :

Example 6 with ThreadToStatementContextBridge

use of org.neo4j.kernel.impl.core.ThreadToStatementContextBridge in project neo4j by neo4j.

the class Neo4jTransactionalContextTest method rollsBackNewlyCreatedTransactionIfTerminationDetectedOnCloseDuringPeriodicCommit.

@SuppressWarnings("ConstantConditions")
@Test
public void rollsBackNewlyCreatedTransactionIfTerminationDetectedOnCloseDuringPeriodicCommit() {
    // Given
    InternalTransaction initialTransaction = mock(InternalTransaction.class, new ReturnsDeepStubs());
    KernelTransaction.Type transactionType = KernelTransaction.Type.implicit;
    SecurityContext securityContext = SecurityContext.AUTH_DISABLED;
    when(initialTransaction.transactionType()).thenReturn(transactionType);
    when(initialTransaction.securityContext()).thenReturn(securityContext);
    GraphDatabaseQueryService queryService = mock(GraphDatabaseQueryService.class);
    KernelTransaction initialKTX = mock(KernelTransaction.class);
    Statement initialStatement = mock(Statement.class);
    QueryRegistryOperations initialQueryRegistry = mock(QueryRegistryOperations.class);
    ExecutingQuery executingQuery = mock(ExecutingQuery.class);
    PropertyContainerLocker locker = new PropertyContainerLocker();
    ThreadToStatementContextBridge txBridge = mock(ThreadToStatementContextBridge.class);
    KernelTransaction secondKTX = mock(KernelTransaction.class);
    InternalTransaction secondTransaction = mock(InternalTransaction.class);
    Statement secondStatement = mock(Statement.class);
    QueryRegistryOperations secondQueryRegistry = mock(QueryRegistryOperations.class);
    when(executingQuery.queryText()).thenReturn("X");
    when(executingQuery.queryParameters()).thenReturn(Collections.emptyMap());
    Mockito.doThrow(RuntimeException.class).when(initialTransaction).close();
    when(initialStatement.queryRegistration()).thenReturn(initialQueryRegistry);
    when(queryService.beginTransaction(transactionType, securityContext)).thenReturn(secondTransaction);
    when(txBridge.getKernelTransactionBoundToThisThread(true)).thenReturn(initialKTX, secondKTX);
    when(txBridge.get()).thenReturn(secondStatement);
    when(secondStatement.queryRegistration()).thenReturn(secondQueryRegistry);
    Neo4jTransactionalContext context = new Neo4jTransactionalContext(queryService, null, guard, txBridge, locker, initialTransaction, initialStatement, executingQuery);
    // When
    try {
        context.commitAndRestartTx();
        throw new AssertionError("Expected RuntimeException to be thrown");
    } catch (RuntimeException e) {
        // Then
        Object[] mocks = { txBridge, initialTransaction, initialQueryRegistry, initialKTX, secondQueryRegistry, secondKTX, secondTransaction };
        InOrder order = Mockito.inOrder(mocks);
        // (0) Constructor
        order.verify(initialTransaction).transactionType();
        order.verify(initialTransaction).securityContext();
        // (1) Unbind old
        order.verify(txBridge).getKernelTransactionBoundToThisThread(true);
        order.verify(txBridge).unbindTransactionFromCurrentThread();
        // (2) Register and unbind new
        order.verify(txBridge).get();
        order.verify(secondQueryRegistry).registerExecutingQuery(executingQuery);
        order.verify(txBridge).getKernelTransactionBoundToThisThread(true);
        order.verify(txBridge).unbindTransactionFromCurrentThread();
        // (3) Rebind, unregister, and close old
        order.verify(txBridge).bindTransactionToCurrentThread(initialKTX);
        order.verify(initialQueryRegistry).unregisterExecutingQuery(executingQuery);
        order.verify(initialTransaction).success();
        order.verify(initialTransaction).close();
        order.verify(txBridge).bindTransactionToCurrentThread(secondKTX);
        order.verify(secondTransaction).failure();
        order.verify(secondTransaction).close();
        order.verify(txBridge).unbindTransactionFromCurrentThread();
        verifyNoMoreInteractions(mocks);
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) InOrder(org.mockito.InOrder) GraphDatabaseQueryService(org.neo4j.kernel.GraphDatabaseQueryService) Statement(org.neo4j.kernel.api.Statement) KernelStatement(org.neo4j.kernel.impl.api.KernelStatement) QueryRegistryOperations(org.neo4j.kernel.api.QueryRegistryOperations) ThreadToStatementContextBridge(org.neo4j.kernel.impl.core.ThreadToStatementContextBridge) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) ExecutingQuery(org.neo4j.kernel.api.query.ExecutingQuery) PropertyContainerLocker(org.neo4j.kernel.impl.coreapi.PropertyContainerLocker) SecurityContext(org.neo4j.kernel.api.security.SecurityContext) ReturnsDeepStubs(org.mockito.internal.stubbing.defaultanswers.ReturnsDeepStubs) Test(org.junit.Test)

Example 7 with ThreadToStatementContextBridge

use of org.neo4j.kernel.impl.core.ThreadToStatementContextBridge in project neo4j by neo4j.

the class FullCheckIntegrationTest method statementOn.

private static KernelStatement statementOn(GraphDatabaseService db) {
    DependencyResolver resolver = ((GraphDatabaseAPI) db).getDependencyResolver();
    ThreadToStatementContextBridge bridge = resolver.resolveDependency(ThreadToStatementContextBridge.class);
    return (KernelStatement) bridge.get();
}
Also used : GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) KernelStatement(org.neo4j.kernel.impl.api.KernelStatement) ThreadToStatementContextBridge(org.neo4j.kernel.impl.core.ThreadToStatementContextBridge) DependencyResolver(org.neo4j.graphdb.DependencyResolver)

Example 8 with ThreadToStatementContextBridge

use of org.neo4j.kernel.impl.core.ThreadToStatementContextBridge in project neo4j by neo4j.

the class TestPlaceboTransaction method before.

@Before
public void before() throws Exception {
    ThreadToStatementContextBridge bridge = mock(ThreadToStatementContextBridge.class);
    when(bridge.get()).thenReturn(mock(Statement.class));
    kernelTransaction = spy(KernelTransaction.class);
    Statement statement = mock(Statement.class);
    readOps = mock(ReadOperations.class);
    when(statement.readOperations()).thenReturn(readOps);
    when(bridge.get()).thenReturn(statement);
    placeboTx = new PlaceboTransaction(() -> kernelTransaction, bridge);
    resource = mock(Node.class);
    when(resource.getId()).thenReturn(1L);
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) ReadOperations(org.neo4j.kernel.api.ReadOperations) PlaceboTransaction(org.neo4j.kernel.impl.coreapi.PlaceboTransaction) Statement(org.neo4j.kernel.api.Statement) Node(org.neo4j.graphdb.Node) ThreadToStatementContextBridge(org.neo4j.kernel.impl.core.ThreadToStatementContextBridge) Before(org.junit.Before)

Example 9 with ThreadToStatementContextBridge

use of org.neo4j.kernel.impl.core.ThreadToStatementContextBridge in project neo4j by neo4j.

the class TopLevelTransactionTest method shouldThrowTransactionExceptionOnTransientKernelException.

@Test
public void shouldThrowTransactionExceptionOnTransientKernelException() throws Exception {
    // GIVEN
    KernelTransaction kernelTransaction = mock(KernelTransaction.class);
    when(kernelTransaction.isOpen()).thenReturn(true);
    doThrow(new RuntimeException("Just a random failure")).when(kernelTransaction).close();
    ThreadToStatementContextBridge bridge = new ThreadToStatementContextBridge();
    TopLevelTransaction transaction = new TopLevelTransaction(kernelTransaction, bridge);
    // WHEN
    transaction.success();
    try {
        transaction.close();
        fail("Should have failed");
    } catch (org.neo4j.graphdb.TransactionFailureException e) {
    // THEN Good
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) ThreadToStatementContextBridge(org.neo4j.kernel.impl.core.ThreadToStatementContextBridge) TopLevelTransaction(org.neo4j.kernel.impl.coreapi.TopLevelTransaction) Test(org.junit.Test)

Example 10 with ThreadToStatementContextBridge

use of org.neo4j.kernel.impl.core.ThreadToStatementContextBridge in project neo4j by neo4j.

the class TopLevelTransactionTest method shouldLetThroughTransientFailureException.

@Test
public void shouldLetThroughTransientFailureException() throws Exception {
    // GIVEN
    KernelTransaction kernelTransaction = mock(KernelTransaction.class);
    when(kernelTransaction.isOpen()).thenReturn(true);
    doThrow(new TransientDatabaseFailureException("Just a random failure")).when(kernelTransaction).close();
    ThreadToStatementContextBridge bridge = new ThreadToStatementContextBridge();
    TopLevelTransaction transaction = new TopLevelTransaction(kernelTransaction, bridge);
    // WHEN
    transaction.success();
    try {
        transaction.close();
        fail("Should have failed");
    } catch (TransientFailureException e) {
    // THEN Good
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) TransientFailureException(org.neo4j.graphdb.TransientFailureException) ThreadToStatementContextBridge(org.neo4j.kernel.impl.core.ThreadToStatementContextBridge) TransientDatabaseFailureException(org.neo4j.graphdb.TransientDatabaseFailureException) TopLevelTransaction(org.neo4j.kernel.impl.coreapi.TopLevelTransaction) Test(org.junit.Test)

Aggregations

ThreadToStatementContextBridge (org.neo4j.kernel.impl.core.ThreadToStatementContextBridge)29 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)15 Statement (org.neo4j.kernel.api.Statement)14 Test (org.junit.Test)8 Transaction (org.neo4j.graphdb.Transaction)8 DependencyResolver (org.neo4j.graphdb.DependencyResolver)6 TopLevelTransaction (org.neo4j.kernel.impl.coreapi.TopLevelTransaction)6 GraphDatabaseQueryService (org.neo4j.kernel.GraphDatabaseQueryService)5 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)5 Node (org.neo4j.graphdb.Node)4 Guard (org.neo4j.kernel.guard.Guard)4 ReadOperations (org.neo4j.kernel.api.ReadOperations)3 ExecutingQuery (org.neo4j.kernel.api.query.ExecutingQuery)3 KernelStatement (org.neo4j.kernel.impl.api.KernelStatement)3 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)3 RemoteException (java.rmi.RemoteException)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Before (org.junit.Before)2