Example usage for org.springframework.transaction.reactive TransactionSynchronizationManager isActualTransactionActive

List of usage examples for org.springframework.transaction.reactive TransactionSynchronizationManager isActualTransactionActive

Introduction

In this page you can find the example usage for org.springframework.transaction.reactive TransactionSynchronizationManager isActualTransactionActive.

Prototype

public boolean isActualTransactionActive() 

Source Link

Document

Return whether there currently is an actual transaction active.

Usage

From source file:org.springframework.transaction.reactive.AbstractReactiveTransactionManager.java

/**
 * Suspend the given transaction. Suspends transaction synchronization first,
 * then delegates to the {@code doSuspend} template method.
 * @param synchronizationManager the synchronization manager bound to the current transaction
 * @param transaction the current transaction object
 * (or {@code null} to just suspend active synchronizations, if any)
 * @return an object that holds suspended resources
 * (or {@code null} if neither transaction nor synchronization active)
 * @see #doSuspend//from w w  w  .  j a  v a  2s. co  m
 * @see #resume
 */
private Mono<SuspendedResourcesHolder> suspend(TransactionSynchronizationManager synchronizationManager,
        @Nullable Object transaction) throws TransactionException {

    if (synchronizationManager.isSynchronizationActive()) {
        Mono<List<TransactionSynchronization>> suspendedSynchronizations = doSuspendSynchronization(
                synchronizationManager);
        return suspendedSynchronizations.flatMap(synchronizations -> {
            Mono<Optional<Object>> suspendedResources = (transaction != null
                    ? doSuspend(synchronizationManager, transaction).map(Optional::of)
                            .defaultIfEmpty(Optional.empty())
                    : Mono.just(Optional.empty()));
            return suspendedResources.map(it -> {
                String name = synchronizationManager.getCurrentTransactionName();
                synchronizationManager.setCurrentTransactionName(null);
                boolean readOnly = synchronizationManager.isCurrentTransactionReadOnly();
                synchronizationManager.setCurrentTransactionReadOnly(false);
                Integer isolationLevel = synchronizationManager.getCurrentTransactionIsolationLevel();
                synchronizationManager.setCurrentTransactionIsolationLevel(null);
                boolean wasActive = synchronizationManager.isActualTransactionActive();
                synchronizationManager.setActualTransactionActive(false);
                return new SuspendedResourcesHolder(it.orElse(null), synchronizations, name, readOnly,
                        isolationLevel, wasActive);
            }).onErrorResume(ErrorPredicates.RUNTIME_OR_ERROR,
                    ex -> doResumeSynchronization(synchronizationManager, synchronizations)
                            .cast(SuspendedResourcesHolder.class));
        });
    } else if (transaction != null) {
        // Transaction active but no synchronization active.
        Mono<Optional<Object>> suspendedResources = doSuspend(synchronizationManager, transaction)
                .map(Optional::of).defaultIfEmpty(Optional.empty());
        return suspendedResources.map(it -> new SuspendedResourcesHolder(it.orElse(null)));
    } else {
        // Neither transaction nor synchronization active.
        return Mono.empty();
    }
}