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

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

Introduction

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

Prototype

public static boolean isSynchronizationActive() 

Source Link

Document

Return if transaction synchronization is active for the current thread.

Usage

From source file:nl.nn.adapterframework.extensions.sap.jco2.SapSenderBase.java

public JCO.Client getClient(IPipeLineSession session, SapSystem sapSystem)
        throws SenderException, SapException {
    JCO.Client result;//from  w  w w  .j a  v  a 2 s  . co m
    if (isSynchronous()) {
        if (StringUtils.isNotEmpty(getLuwHandleSessionKey())) {
            SapLUWHandle handle = SapLUWHandle.retrieveHandle(session, getLuwHandleSessionKey(), true,
                    sapSystem, false);
            if (handle == null) {
                throw new SenderException(
                        "cannot find LUW handle from session key [" + getLuwHandleSessionKey() + "]");
            }
            result = handle.getClient();
        } else {
            result = sapSystem.getClient();
        }
    } else {
        result = ClientFactoryUtils.getTransactionalClient(sapSystem, true);
        if (result == null) {
            if (!TransactionSynchronizationManager.isSynchronizationActive()) {
                throw new SenderException("can only be called from within a transaction");
            }
            throw new SenderException(getLogPrefix() + "Could not obtain Jco Client");
        }
    }
    return result;
}

From source file:nl.nn.adapterframework.extensions.sap.jco3.SapSenderBase.java

public JCoDestination getDestination(IPipeLineSession session, SapSystem sapSystem)
        throws SenderException, SapException, JCoException {
    JCoDestination result;//from  w w  w. ja  va2 s  . c  om
    if (isSynchronous()) {
        if (StringUtils.isNotEmpty(getLuwHandleSessionKey())) {
            SapLUWHandle handle = SapLUWHandle.retrieveHandle(session, getLuwHandleSessionKey(), true,
                    sapSystem, false);
            if (handle == null) {
                throw new SenderException(
                        "cannot find LUW handle from session key [" + getLuwHandleSessionKey() + "]");
            }
            result = handle.getDestination();
        } else {
            result = sapSystem.getDestination();
        }
    } else {
        result = DestinationFactoryUtils.getTransactionalDestination(sapSystem, true);
        if (result == null) {
            if (!TransactionSynchronizationManager.isSynchronizationActive()) {
                throw new SenderException("can only be called from within a transaction");
            }
            throw new SenderException(getLogPrefix() + "Could not obtain Jco Destination");
        }
    }
    return result;
}

From source file:ome.tools.hibernate.SessionStatus.java

private void bindSession(Session session) {
    debug("Binding session to thread.");
    SessionHolder sessionHolder = new SessionHolder(session);
    sessionHolder.setTransaction(sessionHolder.getSession().beginTransaction()); // FIXME TODO
    // If we reach this point, it's ok to bind the new SessionHolder,
    // however the DUMMY EmptySessionHolder may be present so unbind
    // just in case.
    if (TransactionSynchronizationManager.hasResource(factory)) {
        TransactionSynchronizationManager.unbindResource(factory);
    }//from   w  w  w .j av a  2 s.  com
    TransactionSynchronizationManager.bindResource(factory, sessionHolder);
    if (!TransactionSynchronizationManager.isSynchronizationActive()) {
        throw new InternalException("Synchronization not active for " + "TransactionSynchronizationManager");
    }
}

From source file:org.alfresco.repo.search.impl.lucene.AbstractLuceneIndexerAndSearcherFactory.java

/**
 * Get the transaction identifier used to store it in the transaction map.
 * //from w  ww.  ja  v  a  2s  .  c om
 * @param tx Transaction
 * @param storeRef StoreRef
 * @return - the transaction id
 */
@SuppressWarnings("unchecked")
private String getTransactionId(Transaction tx, StoreRef storeRef) {
    if (tx instanceof SimpleTransaction) {
        SimpleTransaction simpleTx = (SimpleTransaction) tx;
        return simpleTx.getGUID();
    } else if (TransactionSynchronizationManager.isSynchronizationActive()) {
        Map<StoreRef, LuceneIndexer> indexers = (Map<StoreRef, LuceneIndexer>) AlfrescoTransactionSupport
                .getResource(indexersKey);
        if (indexers != null) {
            LuceneIndexer indexer = indexers.get(storeRef);
            if (indexer != null) {
                return indexer.getDeltaId();
            }
        }
    }
    return null;
}

From source file:org.alfresco.repo.transaction.AlfrescoTransactionSupport.java

/**
 * @return      Returns the read-write state of the current transaction
 * @since 2.1.4//w w  w.  j av  a2s.c  om
 */
public static TxnReadState getTransactionReadState() {
    if (!TransactionSynchronizationManager.isSynchronizationActive()) {
        return TxnReadState.TXN_NONE;
    }
    // Find the read-write state of the txn
    if (getResource(RESOURCE_KEY_TXN_COMPLETING) != null) {
        // Transaction is completing.  For all intents and purposes, we are not in a transaction.
        return TxnReadState.TXN_NONE;
    } else if (TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {
        return TxnReadState.TXN_READ_ONLY;
    } else {
        return TxnReadState.TXN_READ_WRITE;
    }
}

From source file:org.apache.ranger.common.db.RangerTransactionSynchronizationAdapter.java

public void executeOnTransactionCompletion(Runnable runnable) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Submitting new runnable {" + runnable + "} to run after completion");
    }// w  w w  .  j  ava  2s.  co m

    /*
    From TransactionSynchronizationManager documentation:
    TransactionSynchronizationManager is a central helper that manages resources and transaction synchronizations per thread.
    Resource management code should only register synchronizations when this manager is active,
    which can be checked via isSynchronizationActive(); it should perform immediate resource cleanup else.
    If transaction synchronization isn't active, there is either no current transaction,
    or the transaction manager doesn't support transaction synchronization.
            
    Note: Synchronization is an Interface for transaction synchronization callbacks which is implemented by
    TransactionSynchronizationAdapter
    */

    if (!TransactionSynchronizationManager.isSynchronizationActive()) {
        LOG.info("Transaction synchronization is NOT ACTIVE. Executing right now runnable {" + runnable + "}");
        runnable.run();
        return;
    }
    List<Runnable> threadRunnables = RUNNABLES.get();
    if (threadRunnables == null) {
        threadRunnables = new ArrayList<Runnable>();
        RUNNABLES.set(threadRunnables);
        // Register a new transaction synchronization for the current thread.
        // TransactionSynchronizationManage will call afterCompletion() when current transaction completes.
        TransactionSynchronizationManager.registerSynchronization(this);
    }
    threadRunnables.add(runnable);
}

From source file:org.codehaus.groovy.grails.orm.hibernate.GrailsSessionContext.java

/**
 * Retrieve the Spring-managed Session for the current thread, if any.
 *///www .java  2s  . c  o  m
public Session currentSession() throws HibernateException {
    Object value = TransactionSynchronizationManager.getResource(sessionFactory);
    if (value instanceof Session) {
        return (Session) value;
    }

    if (value instanceof SessionHolder) {
        SessionHolder sessionHolder = (SessionHolder) value;
        Session session = sessionHolder.getSession();
        if (TransactionSynchronizationManager.isSynchronizationActive()
                && !sessionHolder.isSynchronizedWithTransaction()) {
            TransactionSynchronizationManager
                    .registerSynchronization(createSpringSessionSynchronization(sessionHolder));
            sessionHolder.setSynchronizedWithTransaction(true);
            // Switch to FlushMode.AUTO, as we have to assume a thread-bound Session
            // with FlushMode.MANUAL, which needs to allow flushing within the transaction.
            FlushMode flushMode = session.getFlushMode();
            if (FlushMode.isManualFlushMode(flushMode)
                    && !TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {
                session.setFlushMode(FlushMode.AUTO);
                sessionHolder.setPreviousFlushMode(flushMode);
            }
        }
        return session;
    }

    if (jtaSessionContext != null) {
        Session session = jtaSessionContext.currentSession();
        if (TransactionSynchronizationManager.isSynchronizationActive()) {
            TransactionSynchronizationManager
                    .registerSynchronization(createSpringFlushSynchronization(session));
        }
        return session;
    }

    if (allowCreate) {
        // be consistent with older HibernateTemplate behavior
        return createSession(value);
    }

    throw new HibernateException("No Session found for current thread");
}

From source file:org.kuali.rice.kcb.service.impl.MessagingServiceImpl.java

private void queueJob(MessageProcessingJob.Mode mode, long messageId, String user, String cause) {
    // queue up the processing job after the transaction has committed
    LOG.debug("registering synchronization");

    if (!TransactionSynchronizationManager.isSynchronizationActive()) {
        throw new RiceRuntimeException("transaction syncronization is not active "
                + "(!TransactionSynchronizationManager.isSynchronizationActive())");
    } else if (!TransactionSynchronizationManager.isActualTransactionActive()) {
        throw new RiceRuntimeException("actual transaction is not active "
                + "(!TransactionSynchronizationManager.isActualTransactionActive())");
    }/*from www  . java 2s . com*/

    TransactionSynchronizationManager.registerSynchronization(new QueueProcessingJobSynchronization(jobName,
            jobGroup, mode, messageId, user, cause, synchronous));
}

From source file:org.openvpms.hl7.impl.MessageDispatcherImpl.java

/**
 * Queues a message to a sender./*from  www. j a v a2 s  .co  m*/
 *
 * @param message the message
 * @param sender  the sender
 * @param user    the user responsible for the message
 * @return the queued message
 * @throws HL7Exception if the message cannot be encoded
 */
protected DocumentAct queue(Message message, final MLLPSender sender, User user) throws HL7Exception {
    DocumentAct result;
    MessageQueue queue = getMessageQueue(sender);
    if (log.isDebugEnabled()) {
        log.debug("queue() - " + sender);
    }
    result = queue.add(message, user);
    if (TransactionSynchronizationManager.isSynchronizationActive()) {
        // a transaction is in progress, so only invoke schedule() once the transaction has committed
        TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
            @Override
            public void afterCommit() {
                schedule();
            }
        });
    } else {
        schedule();
    }
    return result;
}

From source file:org.sakaiproject.chat2.model.impl.ChatManagerImpl.java

/**
 * {@inheritDoc}/*from   ww w . j av  a 2s  .  c  om*/
 */
public void sendDeleteMessage(ChatMessage message) {
    ChatMessageDeleteTxSync txSync = new ChatMessageDeleteTxSync(message);

    if (TransactionSynchronizationManager.isSynchronizationActive()) {
        TransactionSynchronizationManager.registerSynchronization(txSync);
    } else {
        txSync.afterCompletion(ChatMessageDeleteTxSync.STATUS_COMMITTED);
    }
}