@Test public void shouldExecuteStatements() throws Exception { // given KernelAPI kernel = mockKernel(); ExecutionEngine executionEngine = mock(ExecutionEngine.class); ExecutionResult executionResult = mock(ExecutionResult.class); when(executionEngine.execute("query", map())).thenReturn(executionResult); TransactionRegistry registry = mock(TransactionRegistry.class); when(registry.begin()).thenReturn(1337l); TransactionHandle handle = new TransactionHandle(kernel, executionEngine, registry, uriScheme, StringLogger.DEV_NULL); ExecutionResultSerializer output = mock(ExecutionResultSerializer.class); // when handle.execute( statements(new Statement("query", map(), false, (ResultDataContent[]) null)), output); // then verify(executionEngine).execute("query", map()); InOrder outputOrder = inOrder(output); outputOrder.verify(output).transactionCommitUri(uriScheme.txCommitUri(1337)); outputOrder.verify(output).statementResult(executionResult, false, (ResultDataContent[]) null); outputOrder.verify(output).transactionStatus(anyLong()); outputOrder.verify(output).errors(argThat(hasNoErrors())); outputOrder.verify(output).finish(); verifyNoMoreInteractions(output); }
@Test public void shouldSuspendTransactionAndReleaseForOtherRequestsAfterExecutingStatements() throws Exception { // given KernelAPI kernel = mockKernel(); TransitionalTxManagementKernelTransaction transactionContext = (TransitionalTxManagementKernelTransaction) kernel.newTransaction(); TransactionRegistry registry = mock(TransactionRegistry.class); when(registry.begin()).thenReturn(1337l); ExecutionEngine engine = mock(ExecutionEngine.class); ExecutionResult executionResult = mock(ExecutionResult.class); when(engine.execute("query", map())).thenReturn(executionResult); TransactionHandle handle = new TransactionHandle(kernel, engine, registry, uriScheme, StringLogger.DEV_NULL); ExecutionResultSerializer output = mock(ExecutionResultSerializer.class); // when handle.execute( statements(new Statement("query", map(), false, (ResultDataContent[]) null)), output); // then InOrder transactionOrder = inOrder(transactionContext, registry); transactionOrder.verify(transactionContext).suspendSinceTransactionsAreStillThreadBound(); transactionOrder.verify(registry).release(1337l, handle); InOrder outputOrder = inOrder(output); outputOrder.verify(output).transactionCommitUri(uriScheme.txCommitUri(1337)); outputOrder.verify(output).statementResult(executionResult, false, (ResultDataContent[]) null); outputOrder.verify(output).transactionStatus(anyLong()); outputOrder.verify(output).errors(argThat(hasNoErrors())); outputOrder.verify(output).finish(); verifyNoMoreInteractions(output); }
@Test public void shouldLogMessageIfCommitErrorOccurs() throws Exception { // given KernelAPI kernel = mockKernel(); TransitionalTxManagementKernelTransaction transactionContext = (TransitionalTxManagementKernelTransaction) kernel.newTransaction(); doThrow(new NullPointerException()).when(transactionContext).commit(); StringLogger log = mock(StringLogger.class); TransactionRegistry registry = mock(TransactionRegistry.class); when(registry.begin()).thenReturn(1337l); ExecutionEngine engine = mock(ExecutionEngine.class); ExecutionResult executionResult = mock(ExecutionResult.class); when(engine.execute("query", map())).thenReturn(executionResult); TransactionHandle handle = new TransactionHandle(kernel, engine, registry, uriScheme, log); ExecutionResultSerializer output = mock(ExecutionResultSerializer.class); // when handle.commit( statements(new Statement("query", map(), false, (ResultDataContent[]) null)), output); // then verify(log).error(eq("Failed to commit transaction."), any(NullPointerException.class)); verify(registry).forget(1337l); InOrder outputOrder = inOrder(output); outputOrder.verify(output).statementResult(executionResult, false, (ResultDataContent[]) null); outputOrder .verify(output) .errors(argThat(hasErrors(StatusCode.INTERNAL_COMMIT_TRANSACTION_ERROR))); outputOrder.verify(output).finish(); verifyNoMoreInteractions(output); }
@Test public void shouldLogMessageIfCypherSyntaxErrorOccurs() throws Exception { // given KernelAPI kernel = mockKernel(); ExecutionEngine executionEngine = mock(ExecutionEngine.class); when(executionEngine.execute("matsch (n) return n", map())) .thenThrow(new SyntaxException("did you mean MATCH?")); StringLogger log = mock(StringLogger.class); TransactionRegistry registry = mock(TransactionRegistry.class); when(registry.begin()).thenReturn(1337l); TransactionHandle handle = new TransactionHandle(kernel, executionEngine, registry, uriScheme, log); ExecutionResultSerializer output = mock(ExecutionResultSerializer.class); // when handle.commit( statements(new Statement("matsch (n) return n", map(), false, (ResultDataContent[]) null)), output); // then verify(registry).forget(1337l); InOrder outputOrder = inOrder(output); outputOrder.verify(output).errors(argThat(hasErrors(StatusCode.STATEMENT_SYNTAX_ERROR))); outputOrder.verify(output).finish(); verifyNoMoreInteractions(output); }
@Test public void shouldRollbackTransactionIfExecutionErrorOccurs() throws Exception { // given KernelAPI kernel = mockKernel(); TransitionalTxManagementKernelTransaction transactionContext = (TransitionalTxManagementKernelTransaction) kernel.newTransaction(); TransactionRegistry registry = mock(TransactionRegistry.class); when(registry.begin()).thenReturn(1337l); ExecutionEngine executionEngine = mock(ExecutionEngine.class); when(executionEngine.execute("query", map())).thenThrow(new NullPointerException()); TransactionHandle handle = new TransactionHandle(kernel, executionEngine, registry, uriScheme, StringLogger.DEV_NULL); ExecutionResultSerializer output = mock(ExecutionResultSerializer.class); // when handle.execute( statements(new Statement("query", map(), false, (ResultDataContent[]) null)), output); // then verify(transactionContext).rollback(); verify(registry).forget(1337l); InOrder outputOrder = inOrder(output); outputOrder.verify(output).transactionCommitUri(uriScheme.txCommitUri(1337)); outputOrder .verify(output) .errors(argThat(hasErrors(StatusCode.INTERNAL_STATEMENT_EXECUTION_ERROR))); outputOrder.verify(output).finish(); verifyNoMoreInteractions(output); }
@Test public void shouldRollbackTransactionIfDeserializationErrorOccurs() throws Exception { // given KernelAPI kernel = mockKernel(); TransitionalTxManagementKernelTransaction transactionContext = (TransitionalTxManagementKernelTransaction) kernel.newTransaction(); TransactionRegistry registry = mock(TransactionRegistry.class); when(registry.begin()).thenReturn(1337l); TransactionHandle handle = new TransactionHandle( kernel, mock(ExecutionEngine.class), registry, uriScheme, StringLogger.DEV_NULL); ExecutionResultSerializer output = mock(ExecutionResultSerializer.class); // when handle.execute( deserilizationErrors(new Neo4jError(StatusCode.INVALID_REQUEST_FORMAT, new Exception())), output); // then verify(transactionContext).rollback(); verify(registry).forget(1337l); InOrder outputOrder = inOrder(output); outputOrder.verify(output).transactionCommitUri(uriScheme.txCommitUri(1337)); outputOrder.verify(output).errors(argThat(hasErrors(StatusCode.INVALID_REQUEST_FORMAT))); outputOrder.verify(output).finish(); verifyNoMoreInteractions(output); }
@Test public void shouldRollbackTransactionAndTellRegistryToForgetItsHandle() throws Exception { // given KernelAPI kernel = mockKernel(); TransitionalTxManagementKernelTransaction transactionContext = (TransitionalTxManagementKernelTransaction) kernel.newTransaction(); TransactionRegistry registry = mock(TransactionRegistry.class); when(registry.begin()).thenReturn(1337l); TransactionHandle handle = new TransactionHandle( kernel, mock(ExecutionEngine.class), registry, uriScheme, StringLogger.DEV_NULL); ExecutionResultSerializer output = mock(ExecutionResultSerializer.class); // when handle.rollback(output); // then InOrder transactionOrder = inOrder(transactionContext, registry); transactionOrder.verify(transactionContext).rollback(); transactionOrder.verify(registry).forget(1337l); InOrder outputOrder = inOrder(output); outputOrder.verify(output).errors(argThat(hasNoErrors())); outputOrder.verify(output).finish(); verifyNoMoreInteractions(output); }
@Test public void shouldCommitTransactionAndTellRegistryToForgetItsHandle() throws Exception { // given KernelAPI kernel = mockKernel(); TransitionalTxManagementKernelTransaction transactionContext = (TransitionalTxManagementKernelTransaction) kernel.newTransaction(); TransactionRegistry registry = mock(TransactionRegistry.class); when(registry.begin()).thenReturn(1337l); ExecutionEngine engine = mock(ExecutionEngine.class); ExecutionResult result = mock(ExecutionResult.class); when(engine.execute("query", map())).thenReturn(result); TransactionHandle handle = new TransactionHandle(kernel, engine, registry, uriScheme, StringLogger.DEV_NULL); ExecutionResultSerializer output = mock(ExecutionResultSerializer.class); // when handle.commit( statements(new Statement("query", map(), false, (ResultDataContent[]) null)), output); // then InOrder transactionOrder = inOrder(transactionContext, registry); transactionOrder.verify(transactionContext).commit(); transactionOrder.verify(registry).forget(1337l); InOrder outputOrder = inOrder(output); outputOrder.verify(output).statementResult(result, false, (ResultDataContent[]) null); outputOrder.verify(output).errors(argThat(hasNoErrors())); outputOrder.verify(output).finish(); verifyNoMoreInteractions(output); }
public <Child> Node<Child> getTransactionalValue(Property<? super T, Child> property) { Child currentChildValue = property.getCurrentValue(value); if (isReadingTransactionalChanges()) { UserInterfaceActor actor = null; if (value instanceof UserInterfaceActorDelegate) { actor = ((UserInterfaceActorDelegate) value).getActor(); } if ((actor != null) && actor.hasPreviews()) { List<UserInterfaceDirective> actions = TransactionRegistry.getActionsOn(actor); for (UserInterfaceDirective action : actions) { List<UserInterfaceActorPreview> previews = actor.getPreviews(action); for (UserInterfaceActorPreview preview : previews) { if (preview.affects(property)) { currentChildValue = preview.getPreview(action, currentChildValue); } } } } } return new Node<Child>(currentChildValue); }
public TransactionHandle terminate(long txId) throws TransactionLifecycleException { return registry.terminate(txId); }
public TransactionHandle findTransactionHandle(long txId) throws TransactionLifecycleException { return registry.acquire(txId); }
private void updateTransactionStatus() throws SQLException { // if there is a is an active transaction context, assure the transaction context hasn't changed if (transactionContext != null) { if (transactionContext.isActive()) { if (transactionContext != transactionRegistry.getActiveTransactionContext()) { throw new SQLException( "Connection can not be used while enlisted in another transaction"); } return; } else { // transaction should have been cleared up by TransactionContextListener, but in // rare cases another lister could have registered which uses the connection before // our listener is called. In that rare case, trigger the transaction complete call now transactionComplete(); } } // the existing transaction context ended (or we didn't have one), get the active transaction // context transactionContext = transactionRegistry.getActiveTransactionContext(); // if there is an active transaction context and it already has a shared connection, use it if (transactionContext != null && transactionContext.getSharedConnection() != null) { // A connection for the connection factory has already been enrolled // in the transaction, replace our delegate with the enrolled connection // return current connection to the pool Connection connection = getDelegateInternal(); setDelegate(null); if (connection != null) { try { pool.returnObject(connection); } catch (Exception ignored) { // whatever... try to invalidate the connection try { pool.invalidateObject(connection); } catch (Exception ignore) { // no big deal } } } // add a listener to the transaction context transactionContext.addTransactionContextListener(new CompletionListener()); // set our delegate to the shared connection setDelegate(transactionContext.getSharedConnection()); // remember that we are using a shared connection so it can be cleared after the // transaction completes isSharedConnection = true; } else { // if our delegate is null, create one if (getDelegateInternal() == null) { try { // borrow a new connection from the pool Connection connection = (Connection) pool.borrowObject(); setDelegate(connection); } catch (Exception e) { throw (SQLException) new SQLException("Unable to acquire a new connection from the pool").initCause(e); } } // if we have a transaction, out delegate becomes the shared delegate if (transactionContext != null) { // add a listener to the transaction context transactionContext.addTransactionContextListener(new CompletionListener()); // register our connection as the shared connection try { transactionContext.setSharedConnection(getDelegateInternal()); } catch (SQLException e) { // transaction is hosed transactionContext = null; throw e; } } } }
@Override public void suspend() { transaction.suspendSinceTransactionsAreStillThreadBound(); registry.release(id, transactionHandle); }