Search in sources :

Example 16 with KernelTransaction

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

the class IsolatedTransactionTokenCreator method getOrCreate.

@Override
public synchronized int getOrCreate(String name) throws org.neo4j.kernel.api.exceptions.KernelException {
    KernelAPI kernel = kernelSupplier.get();
    try (KernelTransaction transaction = kernel.newTransaction(Type.implicit, AUTH_DISABLED)) {
        try (Statement statement = transaction.acquireStatement()) {
            int id = createKey(statement, name);
            transaction.success();
            return id;
        }
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Statement(org.neo4j.kernel.api.Statement) KernelAPI(org.neo4j.kernel.api.KernelAPI)

Example 17 with KernelTransaction

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

the class KernelTransactionImplementationTest method shouldRollbackFailedTransaction.

@Test
public void shouldRollbackFailedTransaction() throws Exception {
    // GIVEN
    try (KernelTransaction transaction = newTransaction(securityContext())) {
        // WHEN
        transactionInitializer.accept(transaction);
        transaction.failure();
    }
    // THEN
    verify(transactionMonitor, times(1)).transactionFinished(false, isWriteTx);
    verifyExtraInteractionWithTheMonitor(transactionMonitor, isWriteTx);
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Test(org.junit.Test)

Example 18 with KernelTransaction

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

the class KernelTransactionImplementationTest method shouldIgnoreTerminationDuringRollback.

@Test
public void shouldIgnoreTerminationDuringRollback() throws Exception {
    KernelTransaction transaction = newTransaction(securityContext());
    transactionInitializer.accept(transaction);
    transaction.markForTermination(Status.General.UnknownError);
    transaction.close();
    // THEN
    verify(transactionMonitor, times(1)).transactionFinished(false, isWriteTx);
    verify(transactionMonitor, times(1)).transactionTerminated(isWriteTx);
    verifyExtraInteractionWithTheMonitor(transactionMonitor, isWriteTx);
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Test(org.junit.Test)

Example 19 with KernelTransaction

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

the class KernelTransactionImplementationTest method shouldAllowTerminatingFromADifferentThread.

@Test
public void shouldAllowTerminatingFromADifferentThread() throws Exception {
    // GIVEN
    final DoubleLatch latch = new DoubleLatch(1);
    final KernelTransaction transaction = newTransaction(securityContext());
    transactionInitializer.accept(transaction);
    Future<?> terminationFuture = Executors.newSingleThreadExecutor().submit(() -> {
        latch.waitForAllToStart();
        transaction.markForTermination(Status.General.UnknownError);
        latch.finish();
    });
    // WHEN
    transaction.success();
    latch.startAndWaitForAllToStartAndFinish();
    assertNull(terminationFuture.get(1, TimeUnit.MINUTES));
    try {
        transaction.close();
        fail("Exception expected");
    } catch (Exception e) {
        assertThat(e, instanceOf(TransactionTerminatedException.class));
    }
    // THEN
    verify(transactionMonitor, times(1)).transactionFinished(false, isWriteTx);
    verify(transactionMonitor, times(1)).transactionTerminated(isWriteTx);
    verifyExtraInteractionWithTheMonitor(transactionMonitor, isWriteTx);
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) DoubleLatch(org.neo4j.test.DoubleLatch) TransactionTerminatedException(org.neo4j.graphdb.TransactionTerminatedException) TransactionFailureException(org.neo4j.kernel.api.exceptions.TransactionFailureException) ExpectedException(org.junit.rules.ExpectedException) Test(org.junit.Test)

Example 20 with KernelTransaction

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

the class KernelTransactionsTest method shouldBeAbleToSnapshotDuringHeavyLoad.

@Test
public void shouldBeAbleToSnapshotDuringHeavyLoad() throws Throwable {
    // GIVEN
    final KernelTransactions transactions = newKernelTransactions();
    Race race = new Race();
    final int threads = 50;
    final AtomicBoolean end = new AtomicBoolean();
    final AtomicReferenceArray<KernelTransactionsSnapshot> snapshots = new AtomicReferenceArray<>(threads);
    // Representing "transaction" threads
    for (int i = 0; i < threads; i++) {
        final int threadIndex = i;
        race.addContestant(() -> {
            ThreadLocalRandom random = ThreadLocalRandom.current();
            while (!end.get()) {
                try (KernelTransaction transaction = getKernelTransaction(transactions)) {
                    parkNanos(MILLISECONDS.toNanos(random.nextInt(3)));
                    if (snapshots.get(threadIndex) == null) {
                        snapshots.set(threadIndex, transactions.get());
                        parkNanos(MILLISECONDS.toNanos(random.nextInt(3)));
                    }
                } catch (TransactionFailureException e) {
                    throw new RuntimeException(e);
                }
            }
        });
    }
    // Just checks snapshots
    race.addContestant(() -> {
        ThreadLocalRandom random = ThreadLocalRandom.current();
        int snapshotsLeft = 1_000;
        while (snapshotsLeft > 0) {
            int threadIndex = random.nextInt(threads);
            KernelTransactionsSnapshot snapshot = snapshots.get(threadIndex);
            if (snapshot != null && snapshot.allClosed()) {
                snapshotsLeft--;
                snapshots.set(threadIndex, null);
            }
        }
        // End condition of this test can be described as:
        //   when 1000 snapshots have been seen as closed.
        // setting this boolean to true will have all other threads end as well so that race.go() will end
        end.set(true);
    });
    // WHEN
    race.go();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) TransactionFailureException(org.neo4j.kernel.api.exceptions.TransactionFailureException) Race(org.neo4j.test.Race) AtomicReferenceArray(java.util.concurrent.atomic.AtomicReferenceArray) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Test(org.junit.Test)

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