Example usage for org.springframework.transaction.support TransactionSynchronizationManager getSynchronizations

List of usage examples for org.springframework.transaction.support TransactionSynchronizationManager getSynchronizations

Introduction

In this page you can find the example usage for org.springframework.transaction.support TransactionSynchronizationManager getSynchronizations.

Prototype

public static List<TransactionSynchronization> getSynchronizations() throws IllegalStateException 

Source Link

Document

Return an unmodifiable snapshot list of all registered synchronizations for the current thread.

Usage

From source file:de.metas.procurement.webui.sync.ServerSyncService.java

@Override
public ISyncAfterCommitCollector syncAfterCommit() {
    if (!TransactionSynchronizationManager.isActualTransactionActive()) {
        throw new RuntimeException("Not in transaction");
    }/*from   w w w  .  j a  v a 2s  .c o m*/

    SyncAfterCommit instance = null;
    for (final TransactionSynchronization sync : TransactionSynchronizationManager.getSynchronizations()) {
        if (sync instanceof SyncAfterCommit) {
            instance = (SyncAfterCommit) sync;
            logger.debug("Found SyncAfterCommit instance: {}", instance);
        }
    }

    if (instance == null) {
        instance = new SyncAfterCommit();
        TransactionSynchronizationManager.registerSynchronization(instance);

        logger.debug("Registered synchronization: {}", instance);
    }

    return instance;
}

From source file:org.springframework.transaction.support.AbstractPlatformTransactionManager.java

/**
 * Suspend all current synchronizations and deactivate transaction
 * synchronization for the current thread.
 * @return the List of suspended TransactionSynchronization objects
 *//*from   w  w w.j  a  v a2s.c  om*/
private List<TransactionSynchronization> doSuspendSynchronization() {
    List<TransactionSynchronization> suspendedSynchronizations = TransactionSynchronizationManager
            .getSynchronizations();
    for (TransactionSynchronization synchronization : suspendedSynchronizations) {
        synchronization.suspend();
    }
    TransactionSynchronizationManager.clearSynchronization();
    return suspendedSynchronizations;
}

From source file:org.springframework.transaction.support.AbstractPlatformTransactionManager.java

/**
 * Trigger {@code afterCompletion} callbacks.
 * @param status object representing the transaction
 * @param completionStatus completion status according to TransactionSynchronization constants
 *//*from w  ww . j ava  2 s  .c  om*/
private void triggerAfterCompletion(DefaultTransactionStatus status, int completionStatus) {
    if (status.isNewSynchronization()) {
        List<TransactionSynchronization> synchronizations = TransactionSynchronizationManager
                .getSynchronizations();
        TransactionSynchronizationManager.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
            invokeAfterCompletion(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.
            registerAfterCompletionWithExistingTransaction(status.getTransaction(), synchronizations);
        }
    }
}

From source file:org.springframework.transaction.support.TransactionSynchronizationUtils.java

/**
 * Trigger {@code flush} callbacks on all currently registered synchronizations.
 * @throws RuntimeException if thrown by a {@code flush} callback
 * @see TransactionSynchronization#flush()
 *///from www. j  av  a2  s  .  c  o  m
public static void triggerFlush() {
    for (TransactionSynchronization synchronization : TransactionSynchronizationManager.getSynchronizations()) {
        synchronization.flush();
    }
}

From source file:org.springframework.transaction.support.TransactionSynchronizationUtils.java

/**
 * Trigger {@code beforeCommit} callbacks on all currently registered synchronizations.
 * @param readOnly whether the transaction is defined as read-only transaction
 * @throws RuntimeException if thrown by a {@code beforeCommit} callback
 * @see TransactionSynchronization#beforeCommit(boolean)
 *///w w  w . j av a  2 s. c om
public static void triggerBeforeCommit(boolean readOnly) {
    for (TransactionSynchronization synchronization : TransactionSynchronizationManager.getSynchronizations()) {
        synchronization.beforeCommit(readOnly);
    }
}

From source file:org.springframework.transaction.support.TransactionSynchronizationUtils.java

/**
 * Trigger {@code beforeCompletion} callbacks on all currently registered synchronizations.
 * @see TransactionSynchronization#beforeCompletion()
 *///from  w w w  . j  a  va 2  s  .  co m
public static void triggerBeforeCompletion() {
    for (TransactionSynchronization synchronization : TransactionSynchronizationManager.getSynchronizations()) {
        try {
            synchronization.beforeCompletion();
        } catch (Throwable tsex) {
            logger.error("TransactionSynchronization.beforeCompletion threw exception", tsex);
        }
    }
}

From source file:org.springframework.transaction.support.TransactionSynchronizationUtils.java

/**
 * Trigger {@code afterCommit} callbacks on all currently registered synchronizations.
 * @throws RuntimeException if thrown by a {@code afterCommit} callback
 * @see TransactionSynchronizationManager#getSynchronizations()
 * @see TransactionSynchronization#afterCommit()
 *//*from ww  w  . ja v  a 2  s .  co m*/
public static void triggerAfterCommit() {
    invokeAfterCommit(TransactionSynchronizationManager.getSynchronizations());
}

From source file:org.springframework.transaction.support.TransactionSynchronizationUtils.java

/**
 * Trigger {@code afterCompletion} callbacks on all currently registered synchronizations.
 * @see TransactionSynchronizationManager#getSynchronizations()
 * @param completionStatus the completion status according to the
 * constants in the TransactionSynchronization interface
 * @see TransactionSynchronization#afterCompletion(int)
 * @see TransactionSynchronization#STATUS_COMMITTED
 * @see TransactionSynchronization#STATUS_ROLLED_BACK
 * @see TransactionSynchronization#STATUS_UNKNOWN
 *///from w ww.j  a  va2  s. com
public static void triggerAfterCompletion(int completionStatus) {
    List<TransactionSynchronization> synchronizations = TransactionSynchronizationManager.getSynchronizations();
    invokeAfterCompletion(synchronizations, completionStatus);
}