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;
}
}
}
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);
}
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);
}
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);
}
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();
}
Aggregations