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

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

Introduction

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

Prototype

@Nullable
public static String getCurrentTransactionName() 

Source Link

Document

Return the name of the current transaction, or null if none set.

Usage

From source file:org.drools.container.spring.beans.persistence.DroolsSpringTransactionManager.java

/**
 * Borrowed from Seam/*from w  ww  . j  a  va 2 s. co  m*/
 */
public int getStatus() {
    if (ptm == null) {
        return TransactionManager.STATUS_NO_TRANSACTION;
    }

    logger.debug("Current TX name (According to TransactionSynchronizationManager) : "
            + TransactionSynchronizationManager.getCurrentTransactionName());
    if (TransactionSynchronizationManager.isActualTransactionActive()) {
        TransactionStatus transaction = null;
        try {
            if (currentTransaction == null) {
                transaction = ptm.getTransaction(td);
                if (transaction.isNewTransaction()) {
                    return TransactionManager.STATUS_COMMITTED;
                }
            } else {
                transaction = currentTransaction;
            }
            logger.debug("Current TX: " + transaction);
            // If SynchronizationManager thinks it has an active transaction but
            // our transaction is a new one
            // then we must be in the middle of committing
            if (transaction.isCompleted()) {
                if (transaction.isRollbackOnly()) {
                    return TransactionManager.STATUS_ROLLEDBACK;
                }
                return TransactionManager.STATUS_COMMITTED;
            } else {
                // Using the commented-out code in means that if rollback with this manager,
                //  I always have to catch and check the exception 
                //  because ROLLEDBACK can mean both "rolled back" and "rollback only".
                // if ( transaction.isRollbackOnly() ) {
                //     return TransactionManager.STATUS_ROLLEDBACK;
                // }

                return TransactionManager.STATUS_ACTIVE;
            }
        } finally {
            if (currentTransaction == null) {
                ptm.commit(transaction);
            }
        }
    }
    return TransactionManager.STATUS_NO_TRANSACTION;
}

From source file:com._4dconcept.springframework.data.marklogic.datasource.DatabaseConnectorJtaTransactionTest.java

@After
public void verifyTransactionSynchronizationManagerState() {
    assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    assertNull(TransactionSynchronizationManager.getCurrentTransactionName());
    assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
    assertNull(TransactionSynchronizationManager.getCurrentTransactionIsolationLevel());
    assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
}

From source file:eu.domibus.ebms3.sender.MessageSender.java

/**
 * This method gets called from {@link MessageSender#sendAllUserMessages()}
 * for all unsent messages/*from   w ww  .  j ava 2s. com*/
 *
 * @param messageId id of the message to send
 */
@Transactional(Transactional.TxType.REQUIRES_NEW)
private void sendUserMessage(final String messageId) {
    boolean reliabilityCheckSuccessful = false;
    EbmsErrorChecker.CheckResult errorCheckResult = null;

    LegConfiguration legConfiguration = null;
    String pModeKey = null;
    if (MessageSender.LOG.isTraceEnabled()) {
        MessageSender.LOG
                .trace("Transaction active: " + TransactionSynchronizationManager.getCurrentTransactionName());
    }
    final UserMessage userMessage = this.messagingDao.findUserMessageByMessageId(messageId);
    try {
        pModeKey = this.pModeProvider.findPModeKeyForUserMesssage(userMessage);
        legConfiguration = this.pModeProvider.getLegConfiguration(pModeKey);

        MessageSender.LOG.debug("PMode found : " + pModeKey);
        SOAPMessage soapMessage = this.messageBuilder.buildSOAPMessage(userMessage, legConfiguration);
        SOAPMessage response = this.mshDispatcher.dispatch(soapMessage, pModeKey);
        errorCheckResult = this.ebmsErrorChecker.check(soapMessage, response, pModeKey);
        if (EbmsErrorChecker.CheckResult.ERROR.equals(errorCheckResult)) {
            throw new EbMS3Exception(EbMS3Exception.EbMS3ErrorCode.EBMS_0004,
                    "Problem occured during marshalling", messageId, null, MSHRole.SENDING);
        }
        reliabilityCheckSuccessful = this.reliabilityChecker.check(soapMessage, response, pModeKey);
    } catch (SOAPFaultException f) {
        if (f.getCause() instanceof Fault && f.getCause().getCause() instanceof EbMS3Exception) {
            this.handleEbms3Exception((EbMS3Exception) f.getCause().getCause(), messageId, legConfiguration);
        }

    } catch (EbMS3Exception e) {
        this.handleEbms3Exception(e, messageId, legConfiguration);
    } finally {
        if (reliabilityCheckSuccessful) {

            switch (errorCheckResult) {
            case OK:
                this.messageLogDao.setMessageAsSent(messageId);
                break;
            case WARNING:
                this.messageLogDao.setMessageAsSentWithWarnings(messageId);
                break;
            case ERROR:
                assert false;
                break;
            default:
                assert false;
            }
        } else {
            this.updateRetryLogging(messageId, legConfiguration);
        }

    }
}

From source file:org.vader.common.spring.TransactionScope.java

@Override
public String getConversationId() {
    return TransactionSynchronizationManager.getCurrentTransactionName();
}

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;
            }// w  w  w.  jav a 2s.co m
        }

        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: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);
                        }//from   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.seedstack.spring.internal.SpringEntityManagerLink.java

private EntityManager getEntityManager(Map<String, EntityManagerFactory> mapEntityFactories) {
    if (mapEntityFactories.isEmpty()) {
        throw SeedException.createNew(SpringErrorCode.NO_SPRING_ENTITYMANAGER).put("class", currentClass);
    }//w  ww .j ava 2  s  .co  m

    EntityManager entityManager;
    String unitFromClass = configurationProvider.getConfiguration(currentClass).getString(JPA_UNIT_PROPERTY);
    if (StringUtils.isBlank(unitFromClass)) {
        if (mapEntityFactories.size() > 1) {
            throw SeedException.createNew(SpringErrorCode.AMBIGUOUS_SPRING_ENTITYMANAGER)
                    .put("class", currentClass.getName())
                    .put("currentTransaction", TransactionSynchronizationManager.getCurrentTransactionName());
        } else {
            entityManager = ((EntityManagerHolder) TransactionSynchronizationManager
                    .getResource(mapEntityFactories.entrySet().iterator().next().getValue()))
                            .getEntityManager();
        }
    } else {
        EntityManagerFactory entityManagerFactory = mapEntityFactories.get(unitFromClass);
        if (entityManagerFactory == null) {
            throw SeedException.createNew(SpringErrorCode.UNKNOWN_SPRING_ENTITYMANAGER)
                    .put("class", currentClass).put("unit", unitFromClass)
                    .put("currentTransaction", TransactionSynchronizationManager.getCurrentTransactionName());
        }
        entityManager = ((EntityManagerHolder) TransactionSynchronizationManager
                .getResource(entityManagerFactory)).getEntityManager();
    }

    return entityManager;
}

From source file:org.springframework.orm.jpa.EntityManagerFactoryUtils.java

/**
 * Prepare a transaction on the given EntityManager, if possible.
 * @param em the EntityManager to prepare
 * @param emf the EntityManagerFactory that the EntityManager has been created with
 * @return an arbitrary object that holds transaction data, if any
 * (to be passed into cleanupTransaction)
 * @see JpaDialect#prepareTransaction/*  ww  w .  ja  v a2s  .  c om*/
 */
@Nullable
private static Object prepareTransaction(EntityManager em, EntityManagerFactory emf) {
    if (emf instanceof EntityManagerFactoryInfo) {
        EntityManagerFactoryInfo emfInfo = (EntityManagerFactoryInfo) emf;
        JpaDialect jpaDialect = emfInfo.getJpaDialect();
        if (jpaDialect != null) {
            return jpaDialect.prepareTransaction(em,
                    TransactionSynchronizationManager.isCurrentTransactionReadOnly(),
                    TransactionSynchronizationManager.getCurrentTransactionName());
        }
    }
    return null;
}

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

/**
 * Suspend the given transaction. Suspends transaction synchronization first,
 * then delegates to the {@code doSuspend} template method.
 * @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//w  w  w.  j  a  v  a  2  s.co  m
 * @see #resume
 */
@Nullable
protected final SuspendedResourcesHolder suspend(@Nullable Object transaction) throws TransactionException {
    if (TransactionSynchronizationManager.isSynchronizationActive()) {
        List<TransactionSynchronization> suspendedSynchronizations = doSuspendSynchronization();
        try {
            Object suspendedResources = null;
            if (transaction != null) {
                suspendedResources = doSuspend(transaction);
            }
            String name = TransactionSynchronizationManager.getCurrentTransactionName();
            TransactionSynchronizationManager.setCurrentTransactionName(null);
            boolean readOnly = TransactionSynchronizationManager.isCurrentTransactionReadOnly();
            TransactionSynchronizationManager.setCurrentTransactionReadOnly(false);
            Integer isolationLevel = TransactionSynchronizationManager.getCurrentTransactionIsolationLevel();
            TransactionSynchronizationManager.setCurrentTransactionIsolationLevel(null);
            boolean wasActive = TransactionSynchronizationManager.isActualTransactionActive();
            TransactionSynchronizationManager.setActualTransactionActive(false);
            return new SuspendedResourcesHolder(suspendedResources, suspendedSynchronizations, name, readOnly,
                    isolationLevel, wasActive);
        } catch (RuntimeException | Error ex) {
            // doSuspend failed - original transaction is still active...
            doResumeSynchronization(suspendedSynchronizations);
            throw ex;
        }
    } else if (transaction != null) {
        // Transaction active but no synchronization active.
        Object suspendedResources = doSuspend(transaction);
        return new SuspendedResourcesHolder(suspendedResources);
    } else {
        // Neither transaction nor synchronization active.
        return null;
    }
}