Search in sources :

Example 1 with NestedTransactionNotSupportedException

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

the class WebSphereUowTransactionManager method execute.

@Override
public <T> T execute(TransactionDefinition definition, TransactionCallback<T> callback) throws TransactionException {
    if (definition == null) {
        // Use defaults if no transaction definition given.
        definition = new DefaultTransactionDefinition();
    }
    if (definition.getTimeout() < TransactionDefinition.TIMEOUT_DEFAULT) {
        throw new InvalidTimeoutException("Invalid transaction timeout", definition.getTimeout());
    }
    int pb = definition.getPropagationBehavior();
    boolean existingTx = (this.uowManager.getUOWStatus() != UOWSynchronizationRegistry.UOW_STATUS_NONE && this.uowManager.getUOWType() != UOWSynchronizationRegistry.UOW_TYPE_LOCAL_TRANSACTION);
    int uowType = UOWSynchronizationRegistry.UOW_TYPE_GLOBAL_TRANSACTION;
    boolean joinTx = false;
    boolean newSynch = false;
    if (existingTx) {
        if (pb == TransactionDefinition.PROPAGATION_NEVER) {
            throw new IllegalTransactionStateException("Transaction propagation 'never' but existing transaction found");
        }
        if (pb == TransactionDefinition.PROPAGATION_NESTED) {
            throw new NestedTransactionNotSupportedException("Transaction propagation 'nested' not supported for WebSphere UOW transactions");
        }
        if (pb == TransactionDefinition.PROPAGATION_SUPPORTS || pb == TransactionDefinition.PROPAGATION_REQUIRED || pb == TransactionDefinition.PROPAGATION_MANDATORY) {
            joinTx = true;
            newSynch = (getTransactionSynchronization() != SYNCHRONIZATION_NEVER);
        } else if (pb == TransactionDefinition.PROPAGATION_NOT_SUPPORTED) {
            uowType = UOWSynchronizationRegistry.UOW_TYPE_LOCAL_TRANSACTION;
            newSynch = (getTransactionSynchronization() == SYNCHRONIZATION_ALWAYS);
        } else {
            newSynch = (getTransactionSynchronization() != SYNCHRONIZATION_NEVER);
        }
    } else {
        if (pb == TransactionDefinition.PROPAGATION_MANDATORY) {
            throw new IllegalTransactionStateException("Transaction propagation 'mandatory' but no existing transaction found");
        }
        if (pb == TransactionDefinition.PROPAGATION_SUPPORTS || pb == TransactionDefinition.PROPAGATION_NOT_SUPPORTED || pb == TransactionDefinition.PROPAGATION_NEVER) {
            uowType = UOWSynchronizationRegistry.UOW_TYPE_LOCAL_TRANSACTION;
            newSynch = (getTransactionSynchronization() == SYNCHRONIZATION_ALWAYS);
        } else {
            newSynch = (getTransactionSynchronization() != SYNCHRONIZATION_NEVER);
        }
    }
    boolean debug = logger.isDebugEnabled();
    if (debug) {
        logger.debug("Creating new transaction with name [" + definition.getName() + "]: " + definition);
    }
    SuspendedResourcesHolder suspendedResources = (!joinTx ? suspend(null) : null);
    try {
        if (definition.getTimeout() > TransactionDefinition.TIMEOUT_DEFAULT) {
            this.uowManager.setUOWTimeout(uowType, definition.getTimeout());
        }
        if (debug) {
            logger.debug("Invoking WebSphere UOW action: type=" + uowType + ", join=" + joinTx);
        }
        UOWActionAdapter<T> action = new UOWActionAdapter<>(definition, callback, (uowType == UOWManager.UOW_TYPE_GLOBAL_TRANSACTION), !joinTx, newSynch, debug);
        this.uowManager.runUnderUOW(uowType, joinTx, action);
        if (debug) {
            logger.debug("Returned from WebSphere UOW action: type=" + uowType + ", join=" + joinTx);
        }
        return action.getResult();
    } catch (UOWException ex) {
        throw new TransactionSystemException("UOWManager transaction processing failed", ex);
    } catch (UOWActionException ex) {
        throw new TransactionSystemException("UOWManager threw unexpected UOWActionException", ex);
    } finally {
        if (suspendedResources != null) {
            resume(null, suspendedResources);
        }
    }
}
Also used : UOWActionException(com.ibm.wsspi.uow.UOWActionException) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) IllegalTransactionStateException(org.springframework.transaction.IllegalTransactionStateException) TransactionSystemException(org.springframework.transaction.TransactionSystemException) InvalidTimeoutException(org.springframework.transaction.InvalidTimeoutException) UOWException(com.ibm.wsspi.uow.UOWException) NestedTransactionNotSupportedException(org.springframework.transaction.NestedTransactionNotSupportedException)

Example 2 with NestedTransactionNotSupportedException

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

the class AbstractPlatformTransactionManager method handleExistingTransaction.

/**
	 * Create a TransactionStatus for an existing transaction.
	 */
private TransactionStatus handleExistingTransaction(TransactionDefinition definition, Object transaction, boolean debugEnabled) throws TransactionException {
    if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NEVER) {
        throw new IllegalTransactionStateException("Existing transaction found for transaction marked with propagation 'never'");
    }
    if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NOT_SUPPORTED) {
        if (debugEnabled) {
            logger.debug("Suspending current transaction");
        }
        Object suspendedResources = suspend(transaction);
        boolean newSynchronization = (getTransactionSynchronization() == SYNCHRONIZATION_ALWAYS);
        return prepareTransactionStatus(definition, null, false, newSynchronization, debugEnabled, suspendedResources);
    }
    if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRES_NEW) {
        if (debugEnabled) {
            logger.debug("Suspending current transaction, creating new transaction with name [" + definition.getName() + "]");
        }
        SuspendedResourcesHolder suspendedResources = suspend(transaction);
        try {
            boolean newSynchronization = (getTransactionSynchronization() != SYNCHRONIZATION_NEVER);
            DefaultTransactionStatus status = newTransactionStatus(definition, transaction, true, newSynchronization, debugEnabled, suspendedResources);
            doBegin(transaction, definition);
            prepareSynchronization(status, definition);
            return status;
        } catch (RuntimeException | Error beginEx) {
            resumeAfterBeginException(transaction, suspendedResources, beginEx);
            throw beginEx;
        }
    }
    if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NESTED) {
        if (!isNestedTransactionAllowed()) {
            throw new NestedTransactionNotSupportedException("Transaction manager does not allow nested transactions by default - " + "specify 'nestedTransactionAllowed' property with value 'true'");
        }
        if (debugEnabled) {
            logger.debug("Creating nested transaction with name [" + definition.getName() + "]");
        }
        if (useSavepointForNestedTransaction()) {
            // Create savepoint within existing Spring-managed transaction,
            // through the SavepointManager API implemented by TransactionStatus.
            // Usually uses JDBC 3.0 savepoints. Never activates Spring synchronization.
            DefaultTransactionStatus status = prepareTransactionStatus(definition, transaction, false, false, debugEnabled, null);
            status.createAndHoldSavepoint();
            return status;
        } else {
            // Nested transaction through nested begin and commit/rollback calls.
            // Usually only for JTA: Spring synchronization might get activated here
            // in case of a pre-existing JTA transaction.
            boolean newSynchronization = (getTransactionSynchronization() != SYNCHRONIZATION_NEVER);
            DefaultTransactionStatus status = newTransactionStatus(definition, transaction, true, newSynchronization, debugEnabled, null);
            doBegin(transaction, definition);
            prepareSynchronization(status, definition);
            return status;
        }
    }
    // Assumably PROPAGATION_SUPPORTS or PROPAGATION_REQUIRED.
    if (debugEnabled) {
        logger.debug("Participating in existing transaction");
    }
    if (isValidateExistingTransaction()) {
        if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) {
            Integer currentIsolationLevel = TransactionSynchronizationManager.getCurrentTransactionIsolationLevel();
            if (currentIsolationLevel == null || currentIsolationLevel != definition.getIsolationLevel()) {
                Constants isoConstants = DefaultTransactionDefinition.constants;
                throw new IllegalTransactionStateException("Participating transaction with definition [" + definition + "] specifies isolation level which is incompatible with existing transaction: " + (currentIsolationLevel != null ? isoConstants.toCode(currentIsolationLevel, DefaultTransactionDefinition.PREFIX_ISOLATION) : "(unknown)"));
            }
        }
        if (!definition.isReadOnly()) {
            if (TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {
                throw new IllegalTransactionStateException("Participating transaction with definition [" + definition + "] is not marked as read-only but existing transaction is");
            }
        }
    }
    boolean newSynchronization = (getTransactionSynchronization() != SYNCHRONIZATION_NEVER);
    return prepareTransactionStatus(definition, transaction, false, newSynchronization, debugEnabled, null);
}
Also used : NestedTransactionNotSupportedException(org.springframework.transaction.NestedTransactionNotSupportedException) IllegalTransactionStateException(org.springframework.transaction.IllegalTransactionStateException) Constants(org.springframework.core.Constants)

Example 3 with NestedTransactionNotSupportedException

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

the class WebSphereUowTransactionManagerTests method propagationNestedFailsInCaseOfExistingTransaction.

@Test
public void propagationNestedFailsInCaseOfExistingTransaction() {
    MockUOWManager manager = new MockUOWManager();
    manager.setUOWStatus(UOWManager.UOW_STATUS_ACTIVE);
    WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager(manager);
    DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
    definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
    try {
        ptm.execute(definition, new TransactionCallback<String>() {

            @Override
            public String doInTransaction(TransactionStatus status) {
                return "result";
            }
        });
        fail("Should have thrown NestedTransactionNotSupportedException");
    } catch (NestedTransactionNotSupportedException ex) {
    // expected
    }
}
Also used : DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) NestedTransactionNotSupportedException(org.springframework.transaction.NestedTransactionNotSupportedException) TransactionStatus(org.springframework.transaction.TransactionStatus) Test(org.junit.Test)

Aggregations

NestedTransactionNotSupportedException (org.springframework.transaction.NestedTransactionNotSupportedException)3 IllegalTransactionStateException (org.springframework.transaction.IllegalTransactionStateException)2 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)2 UOWActionException (com.ibm.wsspi.uow.UOWActionException)1 UOWException (com.ibm.wsspi.uow.UOWException)1 Test (org.junit.Test)1 Constants (org.springframework.core.Constants)1 InvalidTimeoutException (org.springframework.transaction.InvalidTimeoutException)1 TransactionStatus (org.springframework.transaction.TransactionStatus)1 TransactionSystemException (org.springframework.transaction.TransactionSystemException)1