Search in sources :

Example 1 with TransactionSynchronization

use of org.springframework.transaction.support.TransactionSynchronization in project spring-framework by spring-projects.

the class ApplicationListenerMethodTransactionalAdapter method onApplicationEvent.

@Override
public void onApplicationEvent(ApplicationEvent event) {
    if (TransactionSynchronizationManager.isSynchronizationActive()) {
        TransactionSynchronization transactionSynchronization = createTransactionSynchronization(event);
        TransactionSynchronizationManager.registerSynchronization(transactionSynchronization);
    } else if (this.annotation.fallbackExecution()) {
        if (this.annotation.phase() == TransactionPhase.AFTER_ROLLBACK && logger.isWarnEnabled()) {
            logger.warn("Processing " + event + " as a fallback execution on AFTER_ROLLBACK phase");
        }
        processEvent(event);
    } else {
        // No transactional event execution at all
        if (logger.isDebugEnabled()) {
            logger.debug("No transaction is active - skipping " + event);
        }
    }
}
Also used : TransactionSynchronization(org.springframework.transaction.support.TransactionSynchronization)

Example 2 with TransactionSynchronization

use of org.springframework.transaction.support.TransactionSynchronization in project iaf by ibissource.

the class JtaUtil method displayTransactionStatus.

// /**
// * returns a meaningful string describing the transaction status.
// */
// public static String displayTransactionStatus(int status) {
// switch (status) {
// case 	Status.STATUS_ACTIVE 			 : return status+"=STATUS_ACTIVE:"+ 	    " A transaction is associated with the target object and it is in the active state.";
// case 	Status.STATUS_COMMITTED 		 : return status+"=STATUS_COMMITTED:"+ 	    " A transaction is associated with the target object and it has been committed.";
// case 	Status.STATUS_COMMITTING 		 : return status+"=STATUS_COMMITTING:"+ 	" A transaction is associated with the target object and it is in the process of committing.";
// case 	Status.STATUS_MARKED_ROLLBACK 	 : return status+"=STATUS_MARKED_ROLLBACK:"+" A transaction is associated with the target object and it has been marked for rollback, perhaps as a result of a setRollbackOnly operation.";
// case 	Status.STATUS_NO_TRANSACTION 	 : return status+"=STATUS_NO_TRANSACTION:"+ " No transaction is currently associated with the target object.";
// case 	Status.STATUS_PREPARED 			 : return status+"=STATUS_PREPARED:"+ 	    " A transaction is associated with the target object and it has been prepared.";
// case 	Status.STATUS_PREPARING 		 : return status+"=STATUS_PREPARING:"+ 	    " A transaction is associated with the target object and it is in the process of preparing.";
// case 	Status.STATUS_ROLLEDBACK 		 : return status+"=STATUS_ROLLEDBACK:"+ 	" A transaction is associated with the target object and the outcome has been determined to be rollback.";
// case 	Status.STATUS_ROLLING_BACK 		 : return status+"=STATUS_ROLLING_BACK:"+ 	" A transaction is associated with the target object and it is in the process of rolling back.";
// case 	Status.STATUS_UNKNOWN 	 		 : return status+"=STATUS_UNKNOWN:"+ 	    " A transaction is associated with the target object but its current status cannot be determined.";
// default : return "unknown transaction status";
// }
// }
public static String displayTransactionStatus(TransactionStatus txStatus) {
    String result;
    result = "txName [" + TransactionSynchronizationManager.getCurrentTransactionName() + "]";
    if (txStatus != null) {
        result += " status new [" + txStatus.isNewTransaction() + "]";
        result += " status completeted [" + txStatus.isCompleted() + "]";
        result += " status rollbackOnly [" + txStatus.isRollbackOnly() + "]";
        result += " status hasSavepoint [" + txStatus.hasSavepoint() + "]";
    } else {
        result += " currently not in a transaction";
    }
    result += " isolation [" + TransactionSynchronizationManager.getCurrentTransactionIsolationLevel() + "]";
    result += " active [" + TransactionSynchronizationManager.isActualTransactionActive() + "]";
    boolean syncActive = TransactionSynchronizationManager.isSynchronizationActive();
    result += " synchronization active [" + syncActive + "]";
    result += "\n";
    Map<Object, Object> resources = TransactionSynchronizationManager.getResourceMap();
    result += "resources:\n";
    if (resources == null) {
        result += "  map is null\n";
    } else {
        for (Iterator<Object> it = resources.keySet().iterator(); it.hasNext(); ) {
            Object key = it.next();
            Object resource = resources.get(key);
            result += ClassUtils.nameOf(key) + "(" + key + "): " + ClassUtils.nameOf(resource) + "(" + resource + ")\n";
            if (resource instanceof JmsResourceHolder) {
                JmsResourceHolder jrh = (JmsResourceHolder) resource;
                result += "  connection: " + jrh.getConnection() + ", session: " + jrh.getSession() + "\n";
            }
        }
    }
    if (syncActive) {
        List<TransactionSynchronization> synchronizations = TransactionSynchronizationManager.getSynchronizations();
        result += "synchronizations:\n";
        for (int i = 0; i < synchronizations.size(); i++) {
            TransactionSynchronization synchronization = synchronizations.get(i);
            result += ClassUtils.nameOf(synchronization) + "(" + synchronization + ")\n";
        }
    }
    return result;
}
Also used : JmsResourceHolder(org.springframework.jms.connection.JmsResourceHolder) TransactionSynchronization(org.springframework.transaction.support.TransactionSynchronization)

Example 3 with TransactionSynchronization

use of org.springframework.transaction.support.TransactionSynchronization in project spring-integration by spring-projects.

the class AbstractPollingEndpoint method bindResourceHolderIfNecessary.

private IntegrationResourceHolder bindResourceHolderIfNecessary(String key, Object resource) {
    if (this.transactionSynchronizationFactory != null && resource != null && TransactionSynchronizationManager.isActualTransactionActive()) {
        TransactionSynchronization synchronization = this.transactionSynchronizationFactory.create(resource);
        if (synchronization != null) {
            TransactionSynchronizationManager.registerSynchronization(synchronization);
            if (synchronization instanceof IntegrationResourceHolderSynchronization) {
                IntegrationResourceHolderSynchronization integrationSynchronization = ((IntegrationResourceHolderSynchronization) synchronization);
                integrationSynchronization.setShouldUnbindAtCompletion(false);
                if (!TransactionSynchronizationManager.hasResource(resource)) {
                    TransactionSynchronizationManager.bindResource(resource, integrationSynchronization.getResourceHolder());
                }
            }
        }
        Object resourceHolder = TransactionSynchronizationManager.getResource(resource);
        if (resourceHolder instanceof IntegrationResourceHolder) {
            IntegrationResourceHolder integrationResourceHolder = (IntegrationResourceHolder) resourceHolder;
            if (key != null) {
                integrationResourceHolder.addAttribute(key, resource);
            }
            return integrationResourceHolder;
        }
    }
    return null;
}
Also used : IntegrationResourceHolderSynchronization(org.springframework.integration.transaction.IntegrationResourceHolderSynchronization) TransactionSynchronization(org.springframework.transaction.support.TransactionSynchronization) IntegrationResourceHolder(org.springframework.integration.transaction.IntegrationResourceHolder)

Example 4 with TransactionSynchronization

use of org.springframework.transaction.support.TransactionSynchronization in project spring-integration by spring-projects.

the class ImapIdleChannelAdapter method createMessageSendingTask.

private Runnable createMessageSendingTask(final Object mailMessage) {
    Runnable sendingTask = () -> {
        @SuppressWarnings("unchecked") org.springframework.messaging.Message<?> message = mailMessage instanceof Message ? ImapIdleChannelAdapter.this.getMessageBuilderFactory().withPayload(mailMessage).build() : (org.springframework.messaging.Message<Object>) mailMessage;
        if (TransactionSynchronizationManager.isActualTransactionActive()) {
            if (ImapIdleChannelAdapter.this.transactionSynchronizationFactory != null) {
                TransactionSynchronization synchronization = ImapIdleChannelAdapter.this.transactionSynchronizationFactory.create(ImapIdleChannelAdapter.this);
                if (synchronization != null) {
                    TransactionSynchronizationManager.registerSynchronization(synchronization);
                    if (synchronization instanceof IntegrationResourceHolderSynchronization && !TransactionSynchronizationManager.hasResource(ImapIdleChannelAdapter.this)) {
                        TransactionSynchronizationManager.bindResource(ImapIdleChannelAdapter.this, ((IntegrationResourceHolderSynchronization) synchronization).getResourceHolder());
                    }
                    Object resourceHolder = TransactionSynchronizationManager.getResource(ImapIdleChannelAdapter.this);
                    if (resourceHolder instanceof IntegrationResourceHolder) {
                        ((IntegrationResourceHolder) resourceHolder).setMessage(message);
                    }
                }
            }
        }
        sendMessage(message);
    };
    // wrap in the TX proxy if necessary
    if (!CollectionUtils.isEmpty(this.adviceChain)) {
        ProxyFactory proxyFactory = new ProxyFactory(sendingTask);
        if (!CollectionUtils.isEmpty(this.adviceChain)) {
            for (Advice advice : this.adviceChain) {
                proxyFactory.addAdvice(advice);
            }
        }
        sendingTask = (Runnable) proxyFactory.getProxy(this.classLoader);
    }
    return sendingTask;
}
Also used : IntegrationResourceHolderSynchronization(org.springframework.integration.transaction.IntegrationResourceHolderSynchronization) TransactionSynchronization(org.springframework.transaction.support.TransactionSynchronization) Message(javax.mail.Message) ProxyFactory(org.springframework.aop.framework.ProxyFactory) IntegrationResourceHolder(org.springframework.integration.transaction.IntegrationResourceHolder) Advice(org.aopalliance.aop.Advice)

Example 5 with TransactionSynchronization

use of org.springframework.transaction.support.TransactionSynchronization in project rpki-validator-3 by RIPE-NCC.

the class Transactions method afterCommitOnce.

public static void afterCommitOnce(Object key, Runnable runnable) {
    Map<Object, Runnable> runnables = AFTER_COMMIT_ONCE_RUNNABLES.get();
    if (runnables.isEmpty()) {
        TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {

            @Override
            public void beforeCompletion() {
                AFTER_COMMIT_ONCE_RUNNABLES.remove();
            }

            @Override
            public void afterCommit() {
                runnables.values().forEach(Runnable::run);
            }
        });
    }
    runnables.putIfAbsent(key, runnable);
}
Also used : TransactionSynchronization(org.springframework.transaction.support.TransactionSynchronization)

Aggregations

TransactionSynchronization (org.springframework.transaction.support.TransactionSynchronization)32 Test (org.junit.jupiter.api.Test)25 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)23 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)23 UserTransaction (jakarta.transaction.UserTransaction)20 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)20 TransactionStatus (org.springframework.transaction.TransactionStatus)5 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)4 SystemException (jakarta.transaction.SystemException)3 HeuristicRollbackException (jakarta.transaction.HeuristicRollbackException)2 List (java.util.List)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 BDDMockito.given (org.mockito.BDDMockito.given)2 BDDMockito.willThrow (org.mockito.BDDMockito.willThrow)2 Mockito.mock (org.mockito.Mockito.mock)2 Mockito.verify (org.mockito.Mockito.verify)2 IntegrationResourceHolder (org.springframework.integration.transaction.IntegrationResourceHolder)2