@Override public KernelTransaction beginTransaction( Transaction.Type type, LoginContext loginContext, long timeout ) throws TransactionFailureException { health.assertHealthy( TransactionFailureException.class ); KernelTransaction transaction = transactions.newInstance( type, loginContext, timeout ); transactionMonitor.transactionStarted(); return transaction; }
private static void startAndCloseTransaction( KernelTransactions kernelTransactions ) { try { kernelTransactions.newInstance( KernelTransaction.Type.explicit, AUTH_DISABLED, 0L ).close(); } catch ( TransactionFailureException e ) { throw new RuntimeException( e ); } }
private static KernelTransaction getKernelTransaction( KernelTransactions transactions ) { return transactions.newInstance( KernelTransaction.Type.implicit, AnonymousContext.none(), 0L ); }
@Test public void shouldNotLeakTransactionOnSecurityContextFreezeFailure() throws Throwable { KernelTransactions kernelTransactions = newKernelTransactions(); LoginContext loginContext = mock( LoginContext.class ); when( loginContext.authorize( any(), any() ) ).thenThrow( new AuthorizationExpiredException( "Freeze failed." ) ); assertException(() -> kernelTransactions.newInstance(KernelTransaction.Type.explicit, loginContext, 0L), AuthorizationExpiredException.class, "Freeze failed."); assertThat("We should not have any transaction", kernelTransactions.activeTransactions(), is(empty())); }
@Test public void exceptionWhenStartingNewTransactionOnStoppedKernelTransactions() throws Throwable { KernelTransactions kernelTransactions = newKernelTransactions(); t2.execute( (OtherThreadExecutor.WorkerCommand<Void,Void>) state -> { stopKernelTransactions( kernelTransactions ); return null; } ).get(); expectedException.expect( IllegalStateException.class ); kernelTransactions.newInstance( KernelTransaction.Type.explicit, AUTH_DISABLED, 0L ); }
@Test public void incrementalUserTransactionId() throws Throwable { KernelTransactions kernelTransactions = newKernelTransactions(); try ( KernelTransaction kernelTransaction = kernelTransactions .newInstance( KernelTransaction.Type.explicit, AnonymousContext.none(), 0L ) ) { assertEquals( 1, kernelTransactions.activeTransactions().iterator().next().getUserTransactionId() ); } try ( KernelTransaction kernelTransaction = kernelTransactions .newInstance( KernelTransaction.Type.explicit, AnonymousContext.none(), 0L ) ) { assertEquals( 2, kernelTransactions.activeTransactions().iterator().next().getUserTransactionId() ); } try ( KernelTransaction kernelTransaction = kernelTransactions .newInstance( KernelTransaction.Type.explicit, AnonymousContext.none(), 0L ) ) { assertEquals( 3, kernelTransactions.activeTransactions().iterator().next().getUserTransactionId() ); } }
@Test public void exceptionWhenStartingNewTransactionOnShutdownInstance() throws Throwable { KernelTransactions kernelTransactions = newKernelTransactions(); databaseAvailabilityGuard.shutdown(); expectedException.expect( DatabaseShutdownException.class ); kernelTransactions.newInstance( KernelTransaction.Type.explicit, AUTH_DISABLED, 0L ); }
@Test public void startNewTransactionOnRestartedKErnelTransactions() throws Throwable { KernelTransactions kernelTransactions = newKernelTransactions(); kernelTransactions.stop(); kernelTransactions.start(); assertNotNull( "New transaction created by restarted kernel transactions component.", kernelTransactions.newInstance( KernelTransaction.Type.explicit, AUTH_DISABLED, 0L ) ); }
@Test public void threadThatBlocksNewTxsCantStartNewTxs() throws Throwable { KernelTransactions kernelTransactions = newKernelTransactions(); kernelTransactions.blockNewTransactions(); try { kernelTransactions.newInstance( KernelTransaction.Type.implicit, AnonymousContext.write(), 0L ); fail( "Exception expected" ); } catch ( Exception e ) { assertThat( e, instanceOf( IllegalStateException.class ) ); } }
@Test( timeout = TEST_TIMEOUT ) public void unblockNewTransactionsFromWrongThreadThrows() throws Throwable { KernelTransactions kernelTransactions = newKernelTransactions(); kernelTransactions.blockNewTransactions(); Future<KernelTransaction> txOpener = t2.execute( state -> kernelTransactions.newInstance( explicit, AnonymousContext.write(), 0L ) ); t2.get().waitUntilWaiting( location -> location.isAt( KernelTransactions.class, "newInstance" ) ); assertNotDone( txOpener ); Future<?> wrongUnblocker = unblockTxsInSeparateThread( kernelTransactions ); try { wrongUnblocker.get(); } catch ( Exception e ) { assertThat( e, instanceOf( ExecutionException.class ) ); assertThat( e.getCause(), instanceOf( IllegalStateException.class ) ); } assertNotDone( txOpener ); kernelTransactions.unblockNewTransactions(); assertNotNull( txOpener.get() ); }
@Test( timeout = TEST_TIMEOUT ) public void blockNewTransactions() throws Throwable { KernelTransactions kernelTransactions = newKernelTransactions(); kernelTransactions.blockNewTransactions(); Future<KernelTransaction> txOpener = t2.execute( state -> kernelTransactions.newInstance( explicit, AnonymousContext.write(), 0L ) ); t2.get().waitUntilWaiting( location -> location.isAt( KernelTransactions.class, "newInstance" ) ); assertNotDone( txOpener ); kernelTransactions.unblockNewTransactions(); assertNotNull( txOpener.get() ); }
@Override public KernelTransaction beginTransaction( Transaction.Type type, LoginContext loginContext, long timeout ) throws TransactionFailureException { health.assertHealthy( TransactionFailureException.class ); KernelTransaction transaction = transactions.newInstance( type, loginContext, timeout ); transactionMonitor.transactionStarted(); return transaction; }