List of usage examples for org.springframework.transaction.reactive GenericReactiveTransaction isDebug
public boolean isDebug()
From source file:org.springframework.transaction.reactive.AbstractReactiveTransactionManager.java
/** * This implementation of commit handles participating in existing * transactions and programmatic rollback requests. * Delegates to {@code isRollbackOnly}, {@code doCommit} * and {@code rollback}.// ww w. ja v a 2 s . c o m * @see ReactiveTransaction#isRollbackOnly() * @see #doCommit * @see #rollback */ @Override public final Mono<Void> commit(ReactiveTransaction transaction) throws TransactionException { if (transaction.isCompleted()) { return Mono.error(new IllegalTransactionStateException( "Transaction is already completed - do not call commit or rollback more than once per transaction")); } return TransactionSynchronizationManager.currentTransaction().flatMap(synchronizationManager -> { GenericReactiveTransaction reactiveTx = (GenericReactiveTransaction) transaction; if (reactiveTx.isRollbackOnly()) { if (reactiveTx.isDebug()) { logger.debug("Transactional code has requested rollback"); } return processRollback(synchronizationManager, reactiveTx); } return processCommit(synchronizationManager, reactiveTx); }); }
From source file:org.springframework.transaction.reactive.AbstractReactiveTransactionManager.java
/** * Process an actual commit./*from w w w . j av a 2 s . c om*/ * Rollback-only flags have already been checked and applied. * @param synchronizationManager the synchronization manager bound to the current transaction * @param status object representing the transaction * @throws TransactionException in case of commit failure */ private Mono<Void> processCommit(TransactionSynchronizationManager synchronizationManager, GenericReactiveTransaction status) throws TransactionException { AtomicBoolean beforeCompletionInvoked = new AtomicBoolean(false); Mono<Object> commit = prepareForCommit(synchronizationManager, status) .then(triggerBeforeCommit(synchronizationManager, status)) .then(triggerBeforeCompletion(synchronizationManager, status)).then(Mono.defer(() -> { beforeCompletionInvoked.set(true); if (status.isNewTransaction()) { if (status.isDebug()) { logger.debug("Initiating transaction commit"); } return doCommit(synchronizationManager, status); } return Mono.empty(); })).then(Mono.empty().onErrorResume(ex -> { Mono<Object> propagateException = Mono.error(ex); // Store result in a local variable in order to appease the // Eclipse compiler with regard to inferred generics. Mono<Object> result = propagateException; if (ErrorPredicates.UNEXPECTED_ROLLBACK.test(ex)) { result = triggerAfterCompletion(synchronizationManager, status, TransactionSynchronization.STATUS_ROLLED_BACK).then(propagateException); } else if (ErrorPredicates.TRANSACTION_EXCEPTION.test(ex)) { result = triggerAfterCompletion(synchronizationManager, status, TransactionSynchronization.STATUS_UNKNOWN).then(propagateException); } else if (ErrorPredicates.RUNTIME_OR_ERROR.test(ex)) { Mono<Void> mono; if (!beforeCompletionInvoked.get()) { mono = triggerBeforeCompletion(synchronizationManager, status); } else { mono = Mono.empty(); } result = mono.then(doRollbackOnCommitException(synchronizationManager, status, ex)) .then(propagateException); } return result; })) .then(Mono.defer(() -> triggerAfterCommit(synchronizationManager, status) .onErrorResume(ex -> triggerAfterCompletion(synchronizationManager, status, TransactionSynchronization.STATUS_COMMITTED).then(Mono.error(ex))) .then(triggerAfterCompletion(synchronizationManager, status, TransactionSynchronization.STATUS_COMMITTED)))); return commit .onErrorResume(ex -> cleanupAfterCompletion(synchronizationManager, status).then(Mono.error(ex))) .then(cleanupAfterCompletion(synchronizationManager, status)); }
From source file:org.springframework.transaction.reactive.AbstractReactiveTransactionManager.java
/** * Process an actual rollback./*from ww w .j a v a 2 s . c om*/ * The completed flag has already been checked. * @param synchronizationManager the synchronization manager bound to the current transaction * @param status object representing the transaction * @throws TransactionException in case of rollback failure */ private Mono<Void> processRollback(TransactionSynchronizationManager synchronizationManager, GenericReactiveTransaction status) { return triggerBeforeCompletion(synchronizationManager, status).then(Mono.defer(() -> { if (status.isNewTransaction()) { if (status.isDebug()) { logger.debug("Initiating transaction rollback"); } return doRollback(synchronizationManager, status); } else { Mono<Void> beforeCompletion = Mono.empty(); // Participating in larger transaction if (status.hasTransaction()) { if (status.isDebug()) { logger.debug( "Participating transaction failed - marking existing transaction as rollback-only"); } beforeCompletion = doSetRollbackOnly(synchronizationManager, status); } else { logger.debug("Should roll back transaction but cannot - no transaction available"); } return beforeCompletion; } })).onErrorResume(ErrorPredicates.RUNTIME_OR_ERROR, ex -> triggerAfterCompletion(synchronizationManager, status, TransactionSynchronization.STATUS_UNKNOWN).then(Mono.error(ex))) .then(Mono.defer(() -> triggerAfterCompletion(synchronizationManager, status, TransactionSynchronization.STATUS_ROLLED_BACK))) .onErrorResume(ex -> cleanupAfterCompletion(synchronizationManager, status).then(Mono.error(ex))) .then(cleanupAfterCompletion(synchronizationManager, status)); }
From source file:org.springframework.transaction.reactive.AbstractReactiveTransactionManager.java
/** * Invoke {@code doRollback}, handling rollback exceptions properly. * @param synchronizationManager the synchronization manager bound to the current transaction * @param status object representing the transaction * @param ex the thrown application exception or error * @throws TransactionException in case of rollback failure * @see #doRollback// w ww .j a v a 2 s . c o m */ private Mono<Void> doRollbackOnCommitException(TransactionSynchronizationManager synchronizationManager, GenericReactiveTransaction status, Throwable ex) throws TransactionException { return Mono.defer(() -> { if (status.isNewTransaction()) { if (status.isDebug()) { logger.debug("Initiating transaction rollback after commit exception", ex); } return doRollback(synchronizationManager, status); } else if (status.hasTransaction()) { if (status.isDebug()) { logger.debug("Marking existing transaction as rollback-only after commit exception", ex); } return doSetRollbackOnly(synchronizationManager, status); } return Mono.empty(); }).onErrorResume(ErrorPredicates.RUNTIME_OR_ERROR, rbex -> { logger.error("Commit exception overridden by rollback exception", ex); return triggerAfterCompletion(synchronizationManager, status, TransactionSynchronization.STATUS_UNKNOWN) .then(Mono.error(rbex)); }).then(triggerAfterCompletion(synchronizationManager, status, TransactionSynchronization.STATUS_ROLLED_BACK)); }
From source file:org.springframework.transaction.reactive.AbstractReactiveTransactionManager.java
/** * Trigger {@code beforeCommit} callbacks. * @param synchronizationManager the synchronization manager bound to the current transaction * @param status object representing the transaction *//* w w w . j ava 2 s. co m*/ private Mono<Void> triggerBeforeCommit(TransactionSynchronizationManager synchronizationManager, GenericReactiveTransaction status) { if (status.isNewSynchronization()) { if (status.isDebug()) { logger.trace("Triggering beforeCommit synchronization"); } return TransactionSynchronizationUtils.triggerBeforeCommit(synchronizationManager.getSynchronizations(), status.isReadOnly()); } return Mono.empty(); }
From source file:org.springframework.transaction.reactive.AbstractReactiveTransactionManager.java
/** * Trigger {@code beforeCompletion} callbacks. * @param synchronizationManager the synchronization manager bound to the current transaction * @param status object representing the transaction *///from w ww . j a va 2 s.c om private Mono<Void> triggerBeforeCompletion(TransactionSynchronizationManager synchronizationManager, GenericReactiveTransaction status) { if (status.isNewSynchronization()) { if (status.isDebug()) { logger.trace("Triggering beforeCompletion synchronization"); } return TransactionSynchronizationUtils .triggerBeforeCompletion(synchronizationManager.getSynchronizations()); } return Mono.empty(); }
From source file:org.springframework.transaction.reactive.AbstractReactiveTransactionManager.java
/** * Trigger {@code afterCommit} callbacks. * @param synchronizationManager the synchronization manager bound to the current transaction * @param status object representing the transaction *//*from ww w. ja v a2 s.com*/ private Mono<Void> triggerAfterCommit(TransactionSynchronizationManager synchronizationManager, GenericReactiveTransaction status) { if (status.isNewSynchronization()) { if (status.isDebug()) { logger.trace("Triggering afterCommit synchronization"); } return TransactionSynchronizationUtils.invokeAfterCommit(synchronizationManager.getSynchronizations()); } return Mono.empty(); }
From source file:org.springframework.transaction.reactive.AbstractReactiveTransactionManager.java
/** * Trigger {@code afterCompletion} callbacks. * @param synchronizationManager the synchronization manager bound to the current transaction * @param status object representing the transaction * @param completionStatus completion status according to TransactionSynchronization constants */// w w w . j av a 2 s .c om private Mono<Void> triggerAfterCompletion(TransactionSynchronizationManager synchronizationManager, GenericReactiveTransaction status, int completionStatus) { if (status.isNewSynchronization()) { List<TransactionSynchronization> synchronizations = synchronizationManager.getSynchronizations(); synchronizationManager.clearSynchronization(); if (!status.hasTransaction() || status.isNewTransaction()) { if (status.isDebug()) { logger.trace("Triggering afterCompletion synchronization"); } // No transaction or new transaction for the current scope -> // invoke the afterCompletion callbacks immediately return invokeAfterCompletion(synchronizationManager, synchronizations, completionStatus); } else if (!synchronizations.isEmpty()) { // Existing transaction that we participate in, controlled outside // of the scope of this Spring transaction manager -> try to register // an afterCompletion callback with the existing (JTA) transaction. return registerAfterCompletionWithExistingTransaction(synchronizationManager, status.getTransaction(), synchronizations); } } return Mono.empty(); }
From source file:org.springframework.transaction.reactive.AbstractReactiveTransactionManager.java
/** * Clean up after completion, clearing synchronization if necessary, * and invoking doCleanupAfterCompletion. * @param synchronizationManager the synchronization manager bound to the current transaction * @param status object representing the transaction * @see #doCleanupAfterCompletion// w ww . j a v a 2 s .co m */ private Mono<Void> cleanupAfterCompletion(TransactionSynchronizationManager synchronizationManager, GenericReactiveTransaction status) { return Mono.defer(() -> { status.setCompleted(); if (status.isNewSynchronization()) { synchronizationManager.clear(); } Mono<Void> cleanup = Mono.empty(); if (status.isNewTransaction()) { cleanup = doCleanupAfterCompletion(synchronizationManager, status.getTransaction()); } if (status.getSuspendedResources() != null) { if (status.isDebug()) { logger.debug("Resuming suspended transaction after completion of inner transaction"); } Object transaction = (status.hasTransaction() ? status.getTransaction() : null); return cleanup.then(resume(synchronizationManager, transaction, (SuspendedResourcesHolder) status.getSuspendedResources())); } return cleanup; }); }