private StatementResult runStatement(@Name("kernelTransaction") String statement, Session session, Map<String, Object> finalParams, boolean read) { if (read) return session.readTransaction((Transaction tx) -> tx.run(statement, finalParams)); else return session.writeTransaction((Transaction tx) -> tx.run(statement, finalParams)); }
private void createIndexes() { Session session = this.driver.session(); if (session != null) { try (Transaction tx = session.beginTransaction()) { tx.run("CREATE INDEX ON :" + NodeTypes.PHYSICAL_ENTITY + "(id)"); tx.run("CREATE INDEX ON :" + NodeTypes.PHYSICAL_ENTITY + "(name)"); tx.run("CREATE INDEX ON :" + NodeTypes.INTERACTION + "(id)"); tx.run("CREATE INDEX ON :" + NodeTypes.INTERACTION + "(name)"); tx.run("CREATE INDEX ON :" + NodeTypes.XREF + "(id)"); tx.run("CREATE INDEX ON :Tissue(tissue)"); tx.run("CREATE INDEX ON :TimeSeries(timeseries)"); tx.success(); } session.close(); } }
for (Statement statement : statements) { if (statement.parameterMap != null) result = tx.run(statement.cypher, statement.parameterMap); else result = tx.run(statement.cypher); ret.add(new JcQueryResult(result, this)); tx.success(); } catch (Throwable e) { dbException = e; if (btx != null) btx.failure(); tx.failure(); } finally { if (btx == null && tx != null) { try { tx.close(); } catch(Throwable e1) { dbException = e1;
@Override public void commit() { final boolean canCommit = transactionManager.canCommit(); try { if (canCommit) { LOGGER.debug("Committing native transaction: {}", nativeTransaction); if (nativeTransaction.isOpen()) { nativeTransaction.success(); nativeTransaction.close(); nativeSession.close(); } else { throw new IllegalStateException("Transaction is already closed"); } } } catch (ClientException ce) { closeNativeSessionIfPossible(); if (ce.code().startsWith(NEO_CLIENT_ERROR_SECURITY)) { throw new ConnectionException("Security Error: " + ce.code() + ", " + ce.getMessage(), ce); } throw new CypherException(ce.code(), ce.getMessage(), ce); } catch (Exception e) { closeNativeSessionIfPossible(); throw new TransactionException(e.getLocalizedMessage(), e); } finally { super.commit(); if (canCommit) { transactionManager.bookmark(nativeSession.lastBookmark()); } } }
@Override public void rollback() { try { if (transactionManager.canRollback()) { LOGGER.debug("Rolling back native transaction: {}", nativeTransaction); if (nativeTransaction.isOpen()) { nativeTransaction.failure(); nativeTransaction.close(); } else { LOGGER.warn("Transaction is already closed"); } closeNativeSessionIfPossible(); } } catch (Exception e) { closeNativeSessionIfPossible(); throw new TransactionException(e.getLocalizedMessage(), e); } finally { super.rollback(); } }
private <T> T transaction( AccessMode mode, TransactionWork<T> work, TransactionConfig config ) { // use different code path compared to async so that work is executed in the caller thread // caller thread will also be the one who sleeps between retries; // it is unsafe to execute retries in the event loop threads because this can cause a deadlock // event loop thread will bock and wait for itself to read some data return retryLogic.retry( () -> { try ( Transaction tx = beginTransaction( mode, config ) ) { try { T result = work.execute( tx ); tx.success(); return result; } catch ( Throwable t ) { // mark transaction for failure if the given unit of work threw exception // this will override any success marks that were made by the unit of work tx.failure(); throw t; } } } ); }
private StatementResult execute( List<Statement> statements ) { Driver driver = client.getDriver(); Session session = null; try { session = driver.session(); Transaction tx = null; try { tx = session.beginTransaction(); StatementResult result = runAll( tx, statements ); tx.success(); return result; } finally { close( tx ); } } finally { close( session ); } }
@Test public void rollbackShouldCloseSessionWhenTransactionCloseThrows() { Transaction nativeTx = openTransactionMock(); doThrow(new RuntimeException("Close failed")).when(nativeTx).close(); Session nativeSession = openSessionMock(); BoltTransaction boltTx = new BoltTransaction(rollingBackTxManager(), nativeTx, nativeSession, READ_ONLY); try { boltTx.rollback(); fail("Exception expected"); } catch (Exception e) { assertThat(e.getMessage()).isEqualTo("Close failed"); } verify(nativeSession).close(); }
final Session session = tx == null ? driver.session() : null; final Transaction transaction = session == null ? tx : session.beginTransaction(); CompletionStage<StatementResultCursor> runAsync = transaction.runAsync(sql.getSql(), Values.value(sql.getParameters())); Object obj = runAsync.thenComposeAsync(src -> deSerializer.deSerialize(src , sqlMethod)) .whenComplete((obj2,ex)->{ if(ex != null) { log.error("SQL PROCESS ERROR", ex); transaction.rollbackAsync().thenAcceptAsync(v -> { if(session != null) session.closeAsync(); }); } else { if(session != null) transaction.commitAsync().whenComplete((v, ex2) -> session.closeAsync());
public Object process(ProceedingJoinPoint pjp) throws Throwable { Object[] args = pjp.getArgs(); int index = -1; Transaction tx = null; MethodSignature signature = (MethodSignature) pjp.getSignature(); Class<?>[] parameterTypes = signature.getParameterTypes(); for(int i=0; i<parameterTypes.length; i++) { if(parameterTypes[i].isAssignableFrom(Transaction.class)) { index = i; tx = (Transaction) args[i]; } } @SuppressWarnings("resource") final Session session = tx == null ? this.driver.session() : null; final Transaction transaction = tx == null ? session.beginTransaction() : tx; if(tx == null) args[index] = transaction; Object obj = pjp.proceed(args); CompletionStage<?> cs = (CompletionStage<?>) obj; if(index != -1) cs.thenAcceptAsync(o -> transaction.commitAsync().thenAcceptAsync(v -> session.closeAsync())) .exceptionally(ex -> { log.error("SQL process has an exception" , ex); transaction.rollbackAsync().thenAcceptAsync(v -> session.closeAsync()); return null; }); return obj; } }
private static Transaction openTransactionMock() { Transaction tx = mock(Transaction.class); when(tx.isOpen()).thenReturn(true); return tx; }
@Override public CompletableFuture<TransactionStatistics> commit() { if (!result.isDone()) { neo4jTransaction.commitAsync().thenAccept(v -> result.complete(statistics)).exceptionally(t -> { result.completeExceptionally(t); return null; }); } return result; }
@Override public CompletableFuture<TransactionStatistics> cancel() { if (!result.isDone()) { neo4jTransaction.rollbackAsync().thenAccept(v -> result.complete(statistics)).exceptionally(t -> { result.completeExceptionally(t); return null; }); } return result; }
for (Statement statement : statements) { if (statement.parameterMap != null) result = tx.run(statement.cypher, statement.parameterMap); else result = tx.run(statement.cypher); ret.add(new JcQueryResult(result, this)); tx.success(); } catch (Throwable e) { dbException = e; if (btx != null) btx.failure(); tx.failure(); } finally { if (btx == null && tx != null) { try { tx.close(); } catch(Throwable e1) { dbException = e1;
@Override public void commit() { final boolean canCommit = transactionManager.canCommit(); try { if (canCommit) { LOGGER.debug("Committing native transaction: {}", nativeTransaction); if (nativeTransaction.isOpen()) { nativeTransaction.success(); nativeTransaction.close(); nativeSession.close(); } else { throw new IllegalStateException("Transaction is already closed"); } } } catch (ClientException ce) { closeNativeSessionIfPossible(); if (ce.code().startsWith(NEO_CLIENT_ERROR_SECURITY)) { throw new ConnectionException("Security Error: " + ce.code() + ", " + ce.getMessage(), ce); } throw new CypherException(ce.code(), ce.getMessage(), ce); } catch (Exception e) { closeNativeSessionIfPossible(); throw new TransactionException(e.getLocalizedMessage(), e); } finally { super.commit(); if (canCommit) { transactionManager.bookmark(nativeSession.lastBookmark()); } } }
@Override public void rollback() { try { if (transactionManager.canRollback()) { LOGGER.debug("Rolling back native transaction: {}", nativeTransaction); if (nativeTransaction.isOpen()) { nativeTransaction.failure(); nativeTransaction.close(); } else { LOGGER.warn("Transaction is already closed"); } closeNativeSessionIfPossible(); } } catch (Exception e) { closeNativeSessionIfPossible(); throw new TransactionException(e.getLocalizedMessage(), e); } finally { super.rollback(); } }