@Override public CompletionStage<Boolean> commit() { // 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"); } // check rollback only if (isRollbackOnly()) { return rollback().thenApply(r -> Boolean.FALSE); } // commit return CompletableFutureSubscriber.fromPublisher(getSession().commitTransaction()).thenAccept(r -> { // set as completed setCompleted(); // log LOGGER.debug(() -> "MongoDB transaction [" + this + "] committed"); }).thenApply(r -> Boolean.TRUE); }
@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; }
@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); } }
@Override public void end() throws TransactionException { // check active if (!isActive()) { throw new IllegalTransactionStatusException("The transaction is not active"); } // check completed if (!isCompleted()) { if (isRollbackOnly()) { rollback(); } else { if (getConfiguration().isAutoCommit()) { commit(); } } } LOGGER.debug(() -> "MongoDB transaction [" + this + "] finalized"); }
@Override public CompletionStage<Void> end() throws TransactionException { // check active if (!isActive()) { throw new IllegalTransactionStatusException("The transaction is not active"); } // check completed if (!isCompleted()) { if (isRollbackOnly()) { return rollback(); } else { if (getConfiguration().isAutoCommit()) { return commit().thenApply(r -> null); } } } LOGGER.debug(() -> "MongoDB transaction [" + this + "] finalized"); return CompletableFuture.completedFuture(null); }
@Override public CompletionStage<Void> rollback() { // 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"); } // rollback return CompletableFutureSubscriber.fromPublisher(getSession().abortTransaction()).thenAccept(r -> { // set as completed setCompleted(); // log LOGGER.debug(() -> "MongoDB transaction [" + this + "] rolled back"); }); }
@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); } }
@Override public void start() throws TransactionException { throw new IllegalTransactionStatusException("A delegated transaction should not be started"); }
@Override public void end() throws TransactionException { throw new IllegalTransactionStatusException("A delegated transaction should not be ended"); }
@Override public CompletionStage<Void> end() throws TransactionException { throw new IllegalTransactionStatusException("A delegated transaction should not be ended"); }
@Override public void start() throws TransactionException { throw new IllegalTransactionStatusException("A delegated transaction should not be started"); }
@Override public void setRollbackOnly() { if (isCompleted()) { throw new IllegalTransactionStatusException( "Cannot mark the transaction as rollback-only: the transaction is already completed"); } this.rollbackOnly = true; }
@Override public void start() throws TransactionException { // check not already started if (isActive()) { throw new IllegalTransactionStatusException( "The transaction is already active and bound to session [" + getSession() + "]"); } // start transaction getSession().startTransaction(getTransactionOptions()); LOGGER.debug(() -> "MongoDB transaction [" + this + "] started"); }
@Override public void start() throws TransactionException { // check not already started if (isActive()) { throw new IllegalTransactionStatusException( "The transaction is already active and bound to session [" + getSession() + "]"); } // start transaction getSession().startTransaction(getTransactionOptions()); LOGGER.debug(() -> "MongoDB transaction [" + this + "] started"); }
@Override public boolean isRollbackOnly() { return getTransactionStatus() .orElseThrow(() -> new IllegalTransactionStatusException("the transaction is not active")) .isRollbackOnly(); }
@Override public void setRollbackOnly() { getTransactionStatus().orElseThrow(() -> new IllegalTransactionStatusException("the transaction is not active")) .setRollbackOnly(); }