Search in sources :

Example 1 with TransactionFactory

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);
}
Also used : TransactionException(org.mule.runtime.api.tx.TransactionException) TransactionFactory(org.mule.runtime.core.api.transaction.TransactionFactory)

Example 2 with TransactionFactory

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);
}
Also used : TypedTransactionFactory(org.mule.runtime.core.api.transaction.TypedTransactionFactory) TransactionFactory(org.mule.runtime.core.api.transaction.TransactionFactory) Lock(java.util.concurrent.locks.Lock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock)

Example 3 with TransactionFactory

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);
}
Also used : TransactionFactory(org.mule.runtime.core.api.transaction.TransactionFactory) Test(org.junit.Test)

Example 4 with TransactionFactory

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;
}
Also used : TransactionFactory(org.mule.runtime.core.api.transaction.TransactionFactory) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException)

Example 5 with 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);
        }
    }
}
Also used : Transaction(org.mule.runtime.core.api.transaction.Transaction) TransactionFactory(org.mule.runtime.core.api.transaction.TransactionFactory) ExternalTransactionAwareTransactionFactory(org.mule.runtime.core.api.transaction.ExternalTransactionAwareTransactionFactory) ExternalTransactionAwareTransactionFactory(org.mule.runtime.core.api.transaction.ExternalTransactionAwareTransactionFactory)

Aggregations

TransactionFactory (org.mule.runtime.core.api.transaction.TransactionFactory)5 Lock (java.util.concurrent.locks.Lock)1 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)1 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)1 Test (org.junit.Test)1 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)1 TransactionException (org.mule.runtime.api.tx.TransactionException)1 ExternalTransactionAwareTransactionFactory (org.mule.runtime.core.api.transaction.ExternalTransactionAwareTransactionFactory)1 Transaction (org.mule.runtime.core.api.transaction.Transaction)1 TypedTransactionFactory (org.mule.runtime.core.api.transaction.TypedTransactionFactory)1