private ResourceIterator<Node> allNodesWithLabel( final Label myLabel ) { KernelTransaction ktx = statementContext.getKernelTransactionBoundToThisThread( true ); Statement statement = ktx.acquireStatement(); int labelId = ktx.tokenRead().nodeLabel( myLabel.name() ); if ( labelId == TokenRead.NO_TOKEN ) { statement.close(); return Iterators.emptyResourceIterator(); } NodeLabelIndexCursor cursor = ktx.cursors().allocateNodeLabelIndexCursor(); ktx.dataRead().nodeLabelScan( labelId, cursor ); return new NodeCursorResourceIterator<>( cursor, statement, this::newNodeProxy ); }
@Override public Node createNode() { KernelTransaction transaction = statementContext.getKernelTransactionBoundToThisThread( true ); try ( Statement ignore = transaction.acquireStatement() ) { return newNodeProxy( transaction.dataWrite().nodeCreate() ); } catch ( InvalidTransactionTypeKernelException e ) { throw new ConstraintViolationException( e.getMessage(), e ); } }
void closeTransaction( MutableTransactionState ctx, boolean success ) throws TransactionFailureException { KernelTransaction tx = ctx.currentTransaction; ctx.currentTransaction = null; if ( tx != null ) { try { if ( success ) { tx.success(); } else { tx.failure(); } if ( tx.isOpen() ) { tx.close(); } } finally { ctx.currentTransaction = null; } } }
@Override public String getUserMessage( KernelException e ) { KernelTransaction transaction = safeAcquireTransaction( transactionSupplier ); try ( Statement ignore = transaction.acquireStatement() ) { return e.getUserMessage( new SilentTokenNameLookup( transaction.tokenRead() ) ); } }
@Test public void shouldThrowTransactionExceptionOnTransientKernelException() throws Exception { // GIVEN KernelTransaction kernelTransaction = mock( KernelTransaction.class ); when( kernelTransaction.isOpen() ).thenReturn( true ); doThrow( new RuntimeException( "Just a random failure" ) ).when( kernelTransaction ).close(); TopLevelTransaction transaction = new TopLevelTransaction( kernelTransaction ); // WHEN transaction.success(); try { transaction.close(); fail( "Should have failed" ); } catch ( org.neo4j.graphdb.TransactionFailureException e ) { // THEN Good } }
when( initialStatement.queryRegistration() ).thenReturn( initialQueryRegistry ); when( queryService.beginTransaction( transactionType, securityContext ) ).thenReturn( secondTransaction ); when( txBridge.getKernelTransactionBoundToThisThread( true ) ).thenReturn( initialKTX, initialKTX, secondKTX ); when( secondStatement.queryRegistration() ).thenReturn( secondQueryRegistry ); Kernel kernel = mock( Kernel.class ); order.verify( txBridge ).getKernelTransactionBoundToThisThread( true ); order.verify( initialKTX ).executionStatistics(); order.verify( txBridge ).getKernelTransactionBoundToThisThread( true ); order.verify( txBridge ).unbindTransactionFromCurrentThread(); order.verify( txBridge ).getKernelTransactionBoundToThisThread( true ); order.verify( secondKTX ).acquireStatement( ); order.verify( secondQueryRegistry ).registerExecutingQuery( executingQuery ); order.verify( txBridge ).unbindTransactionFromCurrentThread();
@Test public void closeStatementOnClose() { KernelTransaction kernelTransaction = mock( KernelTransaction.class ); Statement statement = mock( Statement.class ); when( kernelTransaction.acquireStatement() ).thenReturn( statement ); //noinspection EmptyTryBlock try ( IndexProcedures ignored = new IndexProcedures( kernelTransaction, null ) ) { } verify( statement ).close(); } }
@Before public void setup() { KernelTransaction transaction = mock( KernelTransaction.class ); tokenRead = mock( TokenRead.class ); schemaRead = mock( SchemaRead.class ); procedure = new IndexProcedures( transaction, null ); when( transaction.tokenRead() ).thenReturn( tokenRead ); when( transaction.schemaRead() ).thenReturn( schemaRead ); indexingService = mock( IndexingService.class ); procedure = new IndexProcedures( transaction, indexingService ); }
private KernelTransaction mockTransaction( Statement statement ) { KernelTransaction kernelTransaction = mock( KernelTransaction.class, new ReturnsDeepStubs() ); when( kernelTransaction.executionStatistics() ).thenReturn( statistics ); when( kernelTransaction.acquireStatement() ).thenReturn( statement ); return kernelTransaction; }
private static KernelTransaction newTransaction() { KernelTransaction transaction = mock( KernelTransaction.class ); when( transaction.isOpen() ).thenReturn( true ); return transaction; }
private static KernelTransaction mockKernelTransaction() throws IndexNotFoundKernelException { SchemaRead schemaRead = mock( SchemaRead.class ); when( schemaRead.indexGetState( any( IndexReference.class ) ) ).thenReturn( InternalIndexState.FAILED ); when( schemaRead.indexGetFailure( any( IndexReference.class ) ) ).thenReturn( Exceptions.stringify( cause ) ); KernelTransaction kt = mock( KernelTransaction.class ); when( kt.tokenRead() ).thenReturn( mock( TokenRead.class ) ); when( kt.schemaRead() ).thenReturn( schemaRead ); when( kt.isTerminated() ).thenReturn( false ); when( kt.acquireStatement() ).thenReturn( mock( Statement.class ) ); return kt; } }
@Before public void setup() throws PropertyKeyIdNotFoundKernelException { when( transaction.tokenRead() ).thenReturn( tokenRead ); when( bridge.get() ).thenReturn( stmt ); when( tokenRead.propertyKeyName( anyInt() ) ).thenAnswer( invocationOnMock -> { int id = invocationOnMock.getArgument( 0 ); return ops.propertyKeyTokenHolder().getTokenById( id ).name(); } ); }
@Test public void shouldIgnoreTerminateAfterCommit() throws Exception { KernelTransaction transaction = newTransaction( loginContext() ); transactionInitializer.accept( transaction ); transaction.success(); transaction.close(); transaction.markForTermination( Status.General.UnknownError ); // THEN verify( transactionMonitor, times( 1 ) ).transactionFinished( true, isWriteTx ); verifyExtraInteractionWithTheMonitor( transactionMonitor, isWriteTx ); }
@Test public void shouldShowTransactionTerminatedExceptionAsTransient() throws Exception { KernelTransaction kernelTransaction = mock( KernelTransaction.class ); doReturn( true ).when( kernelTransaction ).isOpen(); RuntimeException error = new TransactionTerminatedException( Status.Transaction.Terminated ); doThrow( error ).when( kernelTransaction ).close(); TopLevelTransaction transaction = new TopLevelTransaction( kernelTransaction ); transaction.success(); try { transaction.close(); fail( "Should have failed" ); } catch ( Exception e ) { assertThat( e, instanceOf( TransientTransactionFailureException.class ) ); assertSame( error, e.getCause() ); } }
@Deprecated @Description( "Search nodes in explicit automatic index. Replaces `START n=node:node_auto_index('key:foo*')`" ) @Procedure( name = "db.index.explicit.auto.searchNodes", mode = READ, deprecatedBy = EXPLICIT_INDEX_DEPRECATION ) public Stream<WeightedNodeResult> nodeAutoIndexSearch( @Name( "query" ) Object query ) { try ( Statement ignore = tx.acquireStatement() ) { NodeExplicitIndexCursor cursor = tx.cursors().allocateNodeExplicitIndexCursor(); tx.indexRead().nodeExplicitIndexQuery( cursor, "node_auto_index", query ); return toWeightedNodeResultStream( cursor ); } catch ( KernelException e ) { // auto index will not exist if no nodes have been added that match the auto-index rules return Stream.empty(); } }