use of org.mule.runtime.core.api.transaction.TransactionFactory in project mule by mulesoft.
the class DelegateTransaction method bindResource.
@Override
public void bindResource(Object key, Object resource) throws TransactionException {
if (!(this.delegate instanceof NullTransaction)) {
throw new TransactionException(CoreMessages.createStaticMessage("Single resource transaction has already a resource bound"));
}
TransactionFactory transactionFactory = muleContext.getTransactionFactoryManager().getTransactionFactoryFor(key.getClass());
this.unbindTransaction();
int timeout = delegate.getTimeout();
this.delegate = transactionFactory.beginTransaction(muleContext);
delegate.setTimeout(timeout);
delegate.bindResource(key, resource);
}
use of org.mule.runtime.core.api.transaction.TransactionFactory in project mule by mulesoft.
the class TransactionFactoryLocator method lookUpTransactionFactory.
/**
* Given a {@link TransactionType} will look through SPI a {@link TransactionFactory} able to handle that kind of
* of transaction.
*
* @param type The {@link TransactionType} that the {@link TransactionFactory} should handle.
* @return An {@link Optional} {@link TransactionFactory}
*/
public Optional<TransactionFactory> lookUpTransactionFactory(TransactionType type) {
if (!initialized) {
Lock writeLock = lock.writeLock();
try {
writeLock.lock();
if (!initialized) {
factories.putAll(getAvailableFactories());
initialized = true;
}
} finally {
writeLock.unlock();
}
}
Lock readLock = lock.readLock();
TransactionFactory value;
try {
readLock.lock();
value = factories.get(type);
} finally {
readLock.unlock();
}
return ofNullable(value);
}
use of org.mule.runtime.core.api.transaction.TransactionFactory in project mule by mulesoft.
the class SimpleRegistryBootstrapTestCase method existingNotOptionalTransaction.
@Test
public void existingNotOptionalTransaction() throws Exception {
createTestRegistryBootstrap(APP);
TransactionFactory transactionFactoryFor = muleContext.getTransactionFactoryManager().getTransactionFactoryFor(FakeTransactionResource.class);
Assert.assertNotNull(transactionFactoryFor);
}
use of org.mule.runtime.core.api.transaction.TransactionFactory in project mule by mulesoft.
the class SingleResourceTransactionFactoryManager method getTransactionFactoryFor.
public TransactionFactory getTransactionFactoryFor(Class type) {
TransactionFactory transactionFactory = transactionFactoriesCache.get(type);
if (transactionFactory == null) {
for (Class transactionResourceType : transactionFactories.keySet()) {
if (transactionResourceType.isAssignableFrom(type)) {
transactionFactory = transactionFactories.get(transactionResourceType);
this.transactionFactoriesCache.put(type, transactionFactory);
break;
}
}
}
if (transactionFactory == null) {
throw new MuleRuntimeException(CoreMessages.createStaticMessage(String.format("No %s for transactional resource %s", TransactionFactory.class.getName(), type.getName())));
}
return transactionFactory;
}
use of org.mule.runtime.core.api.transaction.TransactionFactory in project mule by mulesoft.
the class ExternalTransactionInterceptor method execute.
@Override
public T execute(ExecutionCallback<T> callback, ExecutionContext executionContext) throws Exception {
Transaction joinedExternal = null;
Transaction tx = TransactionCoordination.getInstance().getTransaction();
try {
if (tx == null && muleContext != null && transactionConfig != null && transactionConfig.isInteractWithExternal()) {
TransactionFactory tmFactory = transactionConfig.getFactory();
if (tmFactory instanceof ExternalTransactionAwareTransactionFactory) {
ExternalTransactionAwareTransactionFactory externalTransactionFactory = (ExternalTransactionAwareTransactionFactory) tmFactory;
joinedExternal = externalTransactionFactory.joinExternalTransaction(muleContext);
}
}
return next.execute(callback, executionContext);
} finally {
if (joinedExternal != null) {
TransactionCoordination.getInstance().unbindTransaction(joinedExternal);
}
}
}
Aggregations