use of org.neo4j.kernel.api.QueryRegistryOperations in project neo4j by neo4j.
the class Neo4jTransactionalContextTest method neverStopsExecutingQueryDuringCommitAndRestartTx.
@SuppressWarnings("ConstantConditions")
@Test
public void neverStopsExecutingQueryDuringCommitAndRestartTx() {
// Given
KernelTransaction initialKTX = mock(KernelTransaction.class);
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);
QueryRegistryOperations initialQueryRegistry = mock(QueryRegistryOperations.class);
ExecutingQuery executingQuery = mock(ExecutingQuery.class);
PropertyContainerLocker locker = null;
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());
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
context.commitAndRestartTx();
// Then
Object[] mocks = { txBridge, initialTransaction, initialQueryRegistry, initialKTX, secondQueryRegistry, secondKTX };
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).unbindTransactionFromCurrentThread();
// (4) Rebind new
order.verify(txBridge).bindTransactionToCurrentThread(secondKTX);
verifyNoMoreInteractions(mocks);
}
use of org.neo4j.kernel.api.QueryRegistryOperations 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.QueryRegistryOperations in project neo4j by neo4j.
the class Neo4jTransactionalContext method commitAndRestartTx.
@Override
public void commitAndRestartTx() {
/*
* This method is use by the Cypher runtime to cater for PERIODIC COMMIT, which allows a single query to
* periodically, after x number of rows, to commit a transaction and spawn a new one.
*
* To still keep track of the running stream after switching transactions, we need to open the new transaction
* before closing the old one. This way, a query will not disappear and appear when switching transactions.
*
* Since our transactions are thread bound, we must first unbind the old transaction from the thread before
* creating a new one. And then we need to do that thread switching again to close the old transaction.
*/
// (1) Unbind current transaction
QueryRegistryOperations oldQueryRegistryOperations = statement.queryRegistration();
InternalTransaction oldTransaction = transaction;
KernelTransaction oldKernelTx = txBridge.getKernelTransactionBoundToThisThread(true);
txBridge.unbindTransactionFromCurrentThread();
// (2) Create, bind, register, and unbind new transaction
transaction = graph.beginTransaction(transactionType, securityContext);
statement = txBridge.get();
statement.queryRegistration().registerExecutingQuery(executingQuery);
KernelTransaction kernelTx = txBridge.getKernelTransactionBoundToThisThread(true);
txBridge.unbindTransactionFromCurrentThread();
// (3) Rebind old transaction just to commit and close it (and unregister as a side effect of that)
txBridge.bindTransactionToCurrentThread(oldKernelTx);
oldQueryRegistryOperations.unregisterExecutingQuery(executingQuery);
try {
oldTransaction.success();
oldTransaction.close();
} catch (Throwable t) {
// Corner case: The old transaction might have been terminated by the user. Now we also need to
// terminate the new transaction.
txBridge.bindTransactionToCurrentThread(kernelTx);
transaction.failure();
transaction.close();
txBridge.unbindTransactionFromCurrentThread();
throw t;
}
// (4) Unbind the now closed old transaction and rebind the new transaction for continued execution
txBridge.unbindTransactionFromCurrentThread();
txBridge.bindTransactionToCurrentThread(kernelTx);
}
use of org.neo4j.kernel.api.QueryRegistryOperations in project neo4j by neo4j.
the class Neo4jTransactionalContextTest method setUpMocks.
private void setUpMocks() {
queryService = mock(GraphDatabaseQueryService.class);
DependencyResolver resolver = mock(DependencyResolver.class);
ThreadToStatementContextBridge txBridge = mock(ThreadToStatementContextBridge.class);
guard = mock(Guard.class);
initialStatement = mock(KernelStatement.class);
QueryRegistryOperations queryRegistryOperations = mock(QueryRegistryOperations.class);
when(initialStatement.queryRegistration()).thenReturn(queryRegistryOperations);
when(queryService.getDependencyResolver()).thenReturn(resolver);
when(resolver.resolveDependency(ThreadToStatementContextBridge.class)).thenReturn(txBridge);
when(resolver.resolveDependency(Guard.class)).thenReturn(guard);
}
use of org.neo4j.kernel.api.QueryRegistryOperations in project neo4j by neo4j.
the class CypherExecutorTest method setUpMocks.
private void setUpMocks() {
database = mock(Database.class);
databaseFacade = mock(GraphDatabaseFacade.class);
resolver = mock(DependencyResolver.class);
executionEngine = mock(ExecutionEngine.class);
statementBridge = mock(ThreadToStatementContextBridge.class);
databaseQueryService = mock(GraphDatabaseQueryService.class);
kernelTransaction = mock(KernelTransaction.class);
statement = mock(Statement.class);
request = mock(HttpServletRequest.class);
InternalTransaction transaction = new TopLevelTransaction(kernelTransaction, () -> statement);
SecurityContext securityContext = AUTH_DISABLED;
KernelTransaction.Type type = KernelTransaction.Type.implicit;
QueryRegistryOperations registryOperations = mock(QueryRegistryOperations.class);
when(statement.queryRegistration()).thenReturn(registryOperations);
when(statementBridge.get()).thenReturn(statement);
when(kernelTransaction.securityContext()).thenReturn(securityContext);
when(kernelTransaction.transactionType()).thenReturn(type);
when(database.getGraph()).thenReturn(databaseFacade);
when(databaseFacade.getDependencyResolver()).thenReturn(resolver);
when(resolver.resolveDependency(QueryExecutionEngine.class)).thenReturn(executionEngine);
when(resolver.resolveDependency(ThreadToStatementContextBridge.class)).thenReturn(statementBridge);
when(resolver.resolveDependency(GraphDatabaseQueryService.class)).thenReturn(databaseQueryService);
when(databaseQueryService.beginTransaction(type, securityContext)).thenReturn(transaction);
when(databaseQueryService.beginTransaction(type, securityContext, CUSTOM_TRANSACTION_TIMEOUT, TimeUnit.MILLISECONDS)).thenReturn(transaction);
when(databaseQueryService.getDependencyResolver()).thenReturn(resolver);
when(request.getScheme()).thenReturn("http");
when(request.getRemoteAddr()).thenReturn("127.0.0.1");
when(request.getRemotePort()).thenReturn(5678);
when(request.getServerName()).thenReturn("127.0.0.1");
when(request.getServerPort()).thenReturn(7474);
when(request.getRequestURI()).thenReturn("/");
}
Aggregations