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:com.eclecticlogic.pedal.impl.TransactionSynchronizationAdapterData.java

static TransactionSynchronizationAdapterData instance() {
    for (TransactionSynchronization sync : TransactionSynchronizationManager.getSynchronizations()) {
        if (sync instanceof TransactionSynchronizationAdapterData) {
            return (TransactionSynchronizationAdapterData) sync;
        }//from   ww w  .j av  a  2  s  .c  o  m
    }
    return null;
}

From source file:com.eclecticlogic.pedal.impl.TransactionSynchronizationAdapterTask.java

static TransactionSynchronizationAdapterTask instance() {
    for (TransactionSynchronization txSync : TransactionSynchronizationManager.getSynchronizations()) {
        if (txSync instanceof TransactionSynchronizationAdapterTask) {
            return (TransactionSynchronizationAdapterTask) txSync;
        }//from   w  w  w  .  ja  v a2  s . c o m
    }
    return null;
}

From source file:org.motechproject.server.omod.sdsched.TxSyncManWrapperImpl.java

@SuppressWarnings("unchecked")
public boolean containsSynchronization(Class<? extends TransactionSynchronization> syncClass) {

    if (isSynchronizationActive()) {
        List<TransactionSynchronization> syncs = (List<TransactionSynchronization>) TransactionSynchronizationManager
                .getSynchronizations();/*  w  ww .j  a va2 s  .  c om*/

        for (TransactionSynchronization sync : syncs)
            if (syncClass.isAssignableFrom(sync.getClass()))
                return true;
    }

    return false;
}

From source file:org.kbac.spring.scope.TransactionScope.java

private String getCurrentTransactionId() {
    final String currentTransactionId;
    if (TransactionSynchronizationManager.isActualTransactionActive()) {
        NamedTransactionSynchronisation currentSynchronisation = null;
        for (TransactionSynchronization sync : TransactionSynchronizationManager.getSynchronizations()) {
            if (sync instanceof NamedTransactionSynchronisation) {
                currentSynchronisation = (NamedTransactionSynchronisation) sync;
                break;
            }//  ww  w  . j av  a  2s .com
        }

        if (currentSynchronisation != null) {
            currentTransactionId = currentSynchronisation.transactionId;
        } else {
            currentTransactionId = formatTransactionId(
                    TransactionSynchronizationManager.getCurrentTransactionName());
        }
    } else {
        currentTransactionId = null;
    }

    if (logger.isDebugEnabled()) {
        logger.debug("using current transaction name [" + currentTransactionId + "]");
    }

    return currentTransactionId;
}

From source file:com.avaje.ebean.springsupport.txn.SpringAwareJdbcTransactionManager.java

/**
 * Search for our specific transaction listener.
 * <p>//from  w  w  w  . j  av a2  s  .  c  o  m
 * If it exists then we have already seen and "wrapped" this transaction.
 * </p>
 */
private SpringTxnListener getSpringTxnListener() {

    if (TransactionSynchronizationManager.isSynchronizationActive()) {
        List<TransactionSynchronization> synchronizations = TransactionSynchronizationManager
                .getSynchronizations();
        if (synchronizations != null) {
            // search for our specific listener
            for (int i = 0; i < synchronizations.size(); i++) {
                if (synchronizations.get(i) instanceof SpringTxnListener) {
                    return (SpringTxnListener) synchronizations.get(i);
                }
            }
        }
    }

    return null;
}

From source file:org.apache.servicemix.transaction.GeronimoPlatformTransactionManager.java

protected void registerTransactionAssociationListener() {
    addTransactionAssociationListener(new TransactionManagerMonitor() {
        public void threadAssociated(Transaction transaction) {
            try {
                if (transaction.getStatus() == Status.STATUS_ACTIVE) {
                    SuspendedResourcesHolder holder = suspendedResources.remove(transaction);
                    if (holder != null && holder.getSuspendedSynchronizations() != null) {
                        TransactionSynchronizationManager.setActualTransactionActive(true);
                        TransactionSynchronizationManager.setCurrentTransactionReadOnly(holder.isReadOnly());
                        TransactionSynchronizationManager.setCurrentTransactionName(holder.getName());
                        TransactionSynchronizationManager.initSynchronization();
                        for (Iterator<?> it = holder.getSuspendedSynchronizations().iterator(); it.hasNext();) {
                            TransactionSynchronization synchronization = (TransactionSynchronization) it.next();
                            synchronization.resume();
                            TransactionSynchronizationManager.registerSynchronization(synchronization);
                        }/* w  w w  .  j  a v a2  s. co  m*/
                    }
                }
            } catch (SystemException e) {
                return;
            }
        }

        public void threadUnassociated(Transaction transaction) {
            try {
                if (transaction.getStatus() == Status.STATUS_ACTIVE) {
                    if (TransactionSynchronizationManager.isSynchronizationActive()) {
                        List<?> suspendedSynchronizations = TransactionSynchronizationManager
                                .getSynchronizations();
                        for (Iterator<?> it = suspendedSynchronizations.iterator(); it.hasNext();) {
                            ((TransactionSynchronization) it.next()).suspend();
                        }
                        TransactionSynchronizationManager.clearSynchronization();
                        String name = TransactionSynchronizationManager.getCurrentTransactionName();
                        TransactionSynchronizationManager.setCurrentTransactionName(null);
                        boolean readOnly = TransactionSynchronizationManager.isCurrentTransactionReadOnly();
                        TransactionSynchronizationManager.setCurrentTransactionReadOnly(false);
                        TransactionSynchronizationManager.setActualTransactionActive(false);
                        SuspendedResourcesHolder holder = new SuspendedResourcesHolder(null,
                                suspendedSynchronizations, name, readOnly);
                        suspendedResources.put(transaction, holder);
                    }
                }
            } catch (SystemException e) {
                return;
            }
        }
    });
}

From source file:org.brekka.pegasus.core.services.impl.ProfileServiceImpl.java

@Override
@Transactional(propagation = Propagation.MANDATORY)
public void currentUserProfileUpdated() {
    List<TransactionSynchronization> synchronizations = TransactionSynchronizationManager.getSynchronizations();
    for (TransactionSynchronization transactionSynchronization : synchronizations) {
        if (transactionSynchronization instanceof ProfileSynchronization) {
            // Already added for update
            return;
        }//from   ww w  .ja va  2 s. co  m
    }
    MemberContext current = memberService.retrieveCurrent();
    Profile activeProfile = current.getActiveProfile();
    if (activeProfile.getId() != null) {
        TransactionSynchronizationManager.registerSynchronization(new ProfileSynchronization(activeProfile));
    }
}

From source file:org.motechproject.server.omod.sdsched.TxSyncManWrapperImplTest.java

public void testRegisterSynchronization() {

    // Create a synchronization to register, and call if successful
    final TrialTransactionSync txSync = new TrialTransactionSync();

    Object retVal = txTempl.execute(new TransactionCallback() {
        public Object doInTransaction(TransactionStatus status) {
            txSyncManWrapper.registerSynchronization(txSync);
            return TransactionSynchronizationManager.getSynchronizations().contains(txSync);
        }/*from   www.j a v a  2 s  . c o  m*/
    });

    assertEquals(true, retVal);
    assertTrue(txSync.wasCalled());
}

From source file:de.metas.procurement.webui.event.MFEventBus.java

private final PostAfterCommitCollector getPostAfterCommit() {
    if (!TransactionSynchronizationManager.isActualTransactionActive()) {
        return null;
    }/*from  w  w  w.  j  av  a 2  s .com*/

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

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

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

    return instance;
}