@Override public void close() throws TransactionFailureException { internal.close(); }
private static void close( KernelTransaction tx ) { try { tx.close(); } catch ( TransactionFailureException e ) { throw new RuntimeException( e ); } }
public void rollback() { try { KernelTransaction kernelTransactionBoundToThisThread = bridge.getKernelTransactionBoundToThisThread( false ); kernelTransactionBoundToThisThread.failure(); kernelTransactionBoundToThisThread.close(); } catch ( TransactionFailureException e ) { throw new RuntimeException( e ); } finally { bridge.unbindTransactionFromCurrentThread(); } }
private static void startAndCloseTransaction( KernelTransactions kernelTransactions ) { try { kernelTransactions.newInstance( KernelTransaction.Type.explicit, AUTH_DISABLED, 0L ).close(); } catch ( TransactionFailureException e ) { throw new RuntimeException( e ); } }
private void commitTx() throws TransactionFailureException { tx.success(); tx.close(); }
@Test public void shouldIgnoreTerminationDuringRollback() throws Exception { KernelTransaction transaction = newTransaction( loginContext() ); 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 ); }
@Test public void shouldIgnoreTerminateAfterRollback() throws Exception { KernelTransaction transaction = newTransaction( loginContext() ); transactionInitializer.accept( transaction ); transaction.close(); transaction.markForTermination( Status.General.UnknownError ); // THEN verify( transactionMonitor, times( 1 ) ).transactionFinished( false, isWriteTx ); verifyExtraInteractionWithTheMonitor( transactionMonitor, isWriteTx ); }
@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 } }
@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 shouldThrowTransientExceptionOnTransientKernelException() throws Exception { // GIVEN KernelTransaction kernelTransaction = mock( KernelTransaction.class ); when( kernelTransaction.isOpen() ).thenReturn( true ); doThrow( new TransactionFailureException( Status.Transaction.ConstraintsChanged, "Proving that TopLevelTransaction does the right thing" ) ).when( kernelTransaction ).close(); TopLevelTransaction transaction = new TopLevelTransaction( kernelTransaction ); // WHEN transaction.success(); try { transaction.close(); fail( "Should have failed" ); } catch ( TransientTransactionFailureException e ) { // THEN Good } }
@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() ); } }
@Test void shouldDoNothingInAutoCommitTransactionUponInitialisationWhenValidated() throws Exception { KernelTransaction transaction = newTimedOutTransaction(); TransactionStateMachineV1SPI stateMachineSPI = newTransactionStateMachineSPI( transaction ); TransactionStateMachine stateMachine = newTransactionStateMachine( stateMachineSPI ); // We're in auto-commit state assertThat( stateMachine.state, is( TransactionStateMachine.State.AUTO_COMMIT ) ); assertNull( stateMachine.ctx.currentTransaction ); // call validate transaction stateMachine.validateTransaction(); assertThat( stateMachine.state, is( TransactionStateMachine.State.AUTO_COMMIT ) ); assertNull( stateMachine.ctx.currentTransaction ); verify( transaction, never() ).getReasonIfTerminated(); verify( transaction, never() ).failure(); verify( transaction, never() ).close(); }
@Test( expected = TransactionTerminatedException.class ) public void shouldThrowOnTerminationInCommit() throws Exception { KernelTransaction transaction = newTransaction( loginContext() ); transactionInitializer.accept( transaction ); transaction.success(); transaction.markForTermination( Status.General.UnknownError ); transaction.close(); }
@Test public void shouldDisposeTransactionsWhenAsked() throws Throwable { // Given KernelTransactions transactions = newKernelTransactions(); transactions.disposeAll(); KernelTransaction first = getKernelTransaction( transactions ); KernelTransaction second = getKernelTransaction( transactions ); KernelTransaction leftOpen = getKernelTransaction( transactions ); first.close(); second.close(); // When transactions.disposeAll(); // Then KernelTransaction postDispose = getKernelTransaction( transactions ); assertThat( postDispose, not( equalTo( first ) ) ); assertThat( postDispose, not( equalTo( second ) ) ); assertNotNull( leftOpen.getReasonIfTerminated() ); }
@Test public void transactionCloseRemovesTxFromActiveTransactions() throws Throwable { KernelTransactions kernelTransactions = newTestKernelTransactions(); KernelTransaction tx1 = getKernelTransaction( kernelTransactions ); KernelTransaction tx2 = getKernelTransaction( kernelTransactions ); KernelTransaction tx3 = getKernelTransaction( kernelTransactions ); tx1.close(); tx3.close(); assertEquals( asSet( newHandle( tx2 ) ), kernelTransactions.activeTransactions() ); }
@Test public void shouldReuseClosedTransactionObjects() throws Throwable { // GIVEN KernelTransactions transactions = newKernelTransactions(); KernelTransaction a = getKernelTransaction( transactions ); // WHEN a.close(); KernelTransaction b = getKernelTransaction( transactions ); // THEN assertSame( a, b ); }
@Test public void shouldListActiveTransactions() throws Throwable { // Given KernelTransactions transactions = newTestKernelTransactions(); // When KernelTransaction first = getKernelTransaction( transactions ); KernelTransaction second = getKernelTransaction( transactions ); KernelTransaction third = getKernelTransaction( transactions ); first.close(); // Then assertThat( transactions.activeTransactions(), equalTo( asSet( newHandle( second ), newHandle( third ) ) ) ); }
@Test public void testRollbackError() throws Throwable { // Given BoltStateMachine machine = init( newMachine() ); // Given there is a running transaction machine.process( new RunMessage( "BEGIN", EMPTY_PARAMS ), nullResponseHandler() ); machine.process( DiscardAllMessage.INSTANCE, nullResponseHandler() ); // And given that transaction will fail to roll back TransactionStateMachine txMachine = txStateMachine( machine ); when( txMachine.ctx.currentTransaction.isOpen() ).thenReturn( true ); doThrow( new TransactionFailureException( "No Mr. Bond, I expect you to die." ) ). when( txMachine.ctx.currentTransaction ).close(); // When machine.process( new RunMessage( "ROLLBACK", EMPTY_PARAMS ), nullResponseHandler() ); machine.process( DiscardAllMessage.INSTANCE, nullResponseHandler() ); // Then assertThat( machine, inState( FailedState.class ) ); }
@Test( expected = NotInTransactionException.class ) public void shouldThrowNotInTransactionWhenTransactionClosedAndAccessingOperations() throws KernelException { KernelTransaction transaction = newTransaction( AnonymousContext.write() ); transaction.success(); transaction.close(); transactionOperation.operate( transaction.dataRead(), transaction.dataWrite(), transaction.schemaRead() ); }
@Test( expected = NotInTransactionException.class ) public void shouldThrowNotInTransactionWhenTransactionClosedAndAttemptingOperations() throws KernelException { KernelTransaction transaction = newTransaction( AnonymousContext.write() ); Read read = transaction.dataRead(); Write write = transaction.dataWrite(); SchemaRead schemaRead = transaction.schemaRead(); transaction.success(); transaction.close(); transactionOperation.operate( read, write, schemaRead ); }