@Override public CompletableFuture<TransactionStatistics> cancel() { if (!result.isDone()) { neo4jTransaction.rollbackAsync().thenAccept(v -> result.complete(statistics)).exceptionally(t -> { result.completeExceptionally(t); return null; }); } return result; }
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; } }
if(ex != null) { log.error("SQL PROCESS ERROR", ex); transaction.rollbackAsync().thenAcceptAsync(v -> { if(session != null) session.closeAsync(); });