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