Search in sources :

Example 31 with KernelTransaction

use of org.neo4j.kernel.api.KernelTransaction 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 32 with KernelTransaction

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

the class KernelIT method txReturnsCorrectIdWhenReadOnly.

@Test
public void txReturnsCorrectIdWhenReadOnly() throws Exception {
    executeDummyTxs(db, 42);
    KernelTransaction tx = kernel.newTransaction(KernelTransaction.Type.implicit, AUTH_DISABLED);
    try (Statement statement = tx.acquireStatement();
        Cursor<NodeItem> cursor = statement.readOperations().nodeCursorById(1)) {
    }
    tx.success();
    assertEquals(KernelTransaction.READ_ONLY, tx.closeTransaction());
    assertFalse(tx.isOpen());
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) NodeItem(org.neo4j.storageengine.api.NodeItem) Statement(org.neo4j.kernel.api.Statement) Test(org.junit.Test)

Example 33 with KernelTransaction

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

the class AuthProceduresBase method terminateTransactionsForValidUser.

protected void terminateTransactionsForValidUser(String username) {
    KernelTransaction currentTx = getCurrentTx();
    getActiveTransactions().stream().filter(tx -> tx.securityContext().subject().hasUsername(username) && !tx.isUnderlyingTransaction(currentTx)).forEach(tx -> tx.markForTermination(Status.Transaction.Terminated));
}
Also used : EnterpriseSecurityContext(org.neo4j.kernel.enterprise.api.security.EnterpriseSecurityContext) BoltConnectionTracker(org.neo4j.kernel.api.bolt.BoltConnectionTracker) Context(org.neo4j.procedure.Context) Status(org.neo4j.kernel.api.exceptions.Status) Collections.emptyList(java.util.Collections.emptyList) ThreadToStatementContextBridge(org.neo4j.kernel.impl.core.ThreadToStatementContextBridge) Set(java.util.Set) User(org.neo4j.kernel.impl.security.User) ArrayList(java.util.ArrayList) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) KernelTransactionHandle(org.neo4j.kernel.api.KernelTransactionHandle) ManagedBoltStateMachine(org.neo4j.kernel.api.bolt.ManagedBoltStateMachine) List(java.util.List) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) SecurityLog(org.neo4j.server.security.enterprise.log.SecurityLog) KernelTransactions(org.neo4j.kernel.impl.api.KernelTransactions) KernelTransaction(org.neo4j.kernel.api.KernelTransaction)

Example 34 with KernelTransaction

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

the class IndexPopulationFlipRaceIT method verifyThatThereAreExactlyOneIndexEntryPerNodeInTheIndexes.

private void verifyThatThereAreExactlyOneIndexEntryPerNodeInTheIndexes(int i, Pair<long[], long[]> data) throws Exception {
    KernelAPI kernelAPI = db.getDependencyResolver().resolveDependency(KernelAPI.class);
    try (KernelTransaction tx = kernelAPI.newTransaction(KernelTransaction.Type.implicit, AnonymousContext.read());
        Statement statement = tx.acquireStatement()) {
        int labelAId = statement.readOperations().labelGetForName(labelA(i).name());
        int keyAId = statement.readOperations().propertyKeyGetForName(keyA(i));
        int labelBId = statement.readOperations().labelGetForName(labelB(i).name());
        int keyBId = statement.readOperations().propertyKeyGetForName(keyB(i));
        NewIndexDescriptor indexA = NewIndexDescriptorFactory.forLabel(labelAId, keyAId);
        NewIndexDescriptor indexB = NewIndexDescriptorFactory.forLabel(labelBId, keyBId);
        for (int j = 0; j < NODES_PER_INDEX; j++) {
            long nodeAId = data.first()[j];
            assertEquals(1, statement.readOperations().nodesCountIndexed(indexA, nodeAId, nodeAId));
            long nodeBId = data.other()[j];
            assertEquals(1, statement.readOperations().nodesCountIndexed(indexB, nodeBId, nodeBId));
        }
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) Statement(org.neo4j.kernel.api.Statement) KernelAPI(org.neo4j.kernel.api.KernelAPI)

Example 35 with KernelTransaction

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

the class ExecutionResultTest method activeTransaction.

private TopLevelTransaction activeTransaction() {
    ThreadToStatementContextBridge bridge = db.getDependencyResolver().resolveDependency(ThreadToStatementContextBridge.class);
    KernelTransaction kernelTransaction = bridge.getTopLevelTransactionBoundToThisThread(false);
    return kernelTransaction == null ? null : new TopLevelTransaction(kernelTransaction, null);
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) ThreadToStatementContextBridge(org.neo4j.kernel.impl.core.ThreadToStatementContextBridge) TopLevelTransaction(org.neo4j.kernel.impl.coreapi.TopLevelTransaction)

Aggregations

KernelTransaction (org.neo4j.kernel.api.KernelTransaction)77 Test (org.junit.Test)37 Statement (org.neo4j.kernel.api.Statement)30 TransactionFailureException (org.neo4j.kernel.api.exceptions.TransactionFailureException)14 ThreadToStatementContextBridge (org.neo4j.kernel.impl.core.ThreadToStatementContextBridge)14 KernelAPI (org.neo4j.kernel.api.KernelAPI)9 TopLevelTransaction (org.neo4j.kernel.impl.coreapi.TopLevelTransaction)8 NewIndexDescriptor (org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor)7 TransactionTerminatedException (org.neo4j.graphdb.TransactionTerminatedException)6 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)6 QueryRegistryOperations (org.neo4j.kernel.api.QueryRegistryOperations)4 SecurityContext (org.neo4j.kernel.api.security.SecurityContext)4 KernelStatement (org.neo4j.kernel.impl.api.KernelStatement)4 RemoteException (java.rmi.RemoteException)3 ExpectedException (org.junit.rules.ExpectedException)3 DependencyResolver (org.neo4j.graphdb.DependencyResolver)3 GraphDatabaseQueryService (org.neo4j.kernel.GraphDatabaseQueryService)3 ShellException (org.neo4j.shell.ShellException)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2