tx.getSession().close(); } catch (Exception e) { throw new TransactionException("Failed to close client session", e);
@Override public boolean commit() throws TransactionException { // check active if (!isActive()) { throw new IllegalTransactionStatusException("Cannot commit the transaction: the transaction is not active"); } // check completed if (isCompleted()) { throw new IllegalTransactionStatusException( "Cannot commit the transaction: the transaction is already completed"); } final boolean committed; try { // check rollback only if (isRollbackOnly()) { rollback(); committed = false; } else { // commit getSession().commitTransaction(); committed = true; LOGGER.debug(() -> "MongoDB transaction [" + this + "] committed"); } } catch (Exception e) { throw new TransactionException("Failed to commit the transaction", e); } // set as completed setCompleted(); return committed; }
tx.getSession().close(); } catch (Exception e) { throw new TransactionException("Failed to close client session", e);
@Override public <R> CompletionStage<R> withTransaction(AsyncTransactionalOperation<R> operation, TransactionConfiguration transactionConfiguration) { ObjectUtils.argumentNotNull(operation, "TransactionalOperation must be not null"); // check active transaction or create a new one return getCurrentTransaction() .map(t -> (CompletionStage<AsyncMongoTransaction>) CompletableFuture .completedFuture(AsyncMongoTransaction.delegate(t))) .orElseGet(() -> startTransaction(transactionConfiguration)).thenCompose(tx -> { // execute operation try { return operation.execute(tx).thenApply(r -> new TransactionalOperationResult<>(tx, r)); } catch (Exception e) { // check rollback transaction if (tx.getConfiguration().isRollbackOnError() && !tx.isCompleted()) { tx.setRollbackOnly(); } return CompletableFuture.completedFuture(new TransactionalOperationResult<R>(tx, e)); } }).thenApply(result -> { // finalize transaction endTransaction(result.getTransaction()); // check execution error if (result.getError() != null) { throw new TransactionException("Failed to execute operation", result.getError()); } // return the result return result.getResult(); }); }
@Override public void rollback() throws TransactionException { // check active if (!isActive()) { throw new IllegalTransactionStatusException( "Cannot rollback the transaction: the transaction is not active"); } // check completed if (isCompleted()) { throw new IllegalTransactionStatusException( "Cannot rollback the transaction: the transaction is already completed"); } try { getSession().abortTransaction(); LOGGER.debug(() -> "MongoDB transaction [" + this + "] rolled back"); } catch (Exception e) { throw new TransactionException("Failed to rollback the transaction", e); } // set as completed setCompleted(); }
@Override public boolean commit() throws TransactionException { final TransactionStatus tx = getTransactionStatus() .orElseThrow(() -> new IllegalTransactionStatusException("the transaction is not active")); if (tx.isCompleted()) { throw new IllegalTransactionStatusException( "Cannot commit the transaction: the transaction is already completed"); } try { // check rollback only if (isRollbackOnly()) { getTransactionManager().rollback(tx); return false; } else { getTransactionManager().commit(tx); return true; } } catch (Exception e) { throw new TransactionException("Failed to commit the transaction", e); } }
/** * Finalize the transaction. * @throws TransactionException If an error occurred */ protected void endTransaction() throws TransactionException { getTransactionStatus().ifPresent(tx -> { if (!tx.isCompleted()) { try { if (isRollbackOnly()) { getTransactionManager().rollback(tx); } else { if (getConfiguration().isAutoCommit()) { getTransactionManager().commit(tx); } } } catch (Exception e) { throw new TransactionException("Failed to finalize transaction", e); } } }); }
/** * Start a transaction * @throws TransactionException If an error occurred */ protected void startTransaction() throws TransactionException { if (getTransactionStatus().isPresent()) { throw new TransactionException("The transaction has already been started"); } // transaction definition DefaultTransactionDefinition definition = new DefaultTransactionDefinition(); getIsolationLevel(getConfiguration()).ifPresent(i -> { definition.setIsolationLevel(i); }); // start transaction transactionStatus = getTransactionManager().getTransaction(definition); }
@Override public void rollback() throws TransactionException { final TransactionStatus tx = getTransactionStatus() .orElseThrow(() -> new IllegalTransactionStatusException("the transaction is not active")); if (tx.isCompleted()) { throw new IllegalTransactionStatusException( "Cannot commit the transaction: the transaction is already completed"); } try { getTransactionManager().rollback(tx); } catch (Exception e) { throw new TransactionException("Failed to rollback the transaction", e); } }