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);
}
}
}
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;
}
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;
}
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;
}
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);
}
Aggregations