Example usage for org.springframework.transaction TransactionSystemException initApplicationException

List of usage examples for org.springframework.transaction TransactionSystemException initApplicationException

Introduction

In this page you can find the example usage for org.springframework.transaction TransactionSystemException initApplicationException.

Prototype

public void initApplicationException(Throwable ex) 

Source Link

Document

Set an application exception that was thrown before this transaction exception, preserving the original exception despite the overriding TransactionSystemException.

Usage

From source file:com.googlecode.starflow.engine.transaction.SpringTransactionPolicy.java

@Override
public void rollbackOnException(TransactionStatus txStatus, Throwable ex) throws TransactionException {
    logger.debug("Initiating transaction rollback on application exception", ex);
    try {/*w w  w.  j  ava 2 s .co m*/
        if (transactionManager != null && txStatus != null) {
            this.transactionManager.rollback(txStatus);
        }
    } catch (TransactionSystemException ex2) {
        logger.error("Application exception overridden by rollback exception", ex);
        ex2.initApplicationException(ex);
        throw ex2;
    } catch (RuntimeException ex2) {
        logger.error("Application exception overridden by rollback exception", ex);
        throw ex2;
    } catch (Error err) {
        logger.error("Application exception overridden by rollback error", ex);
        throw err;
    }
}

From source file:org.dcache.chimera.JdbcFs.java

/**
 * Perform a rollback, handling rollback exceptions properly.
 * @param status object representing the transaction
 * @param ex the thrown application exception or error
 * @throws TransactionException in case of a rollback error
 *//*from  www  .  j  a v  a2 s .  c  om*/
private void rollbackOnException(TransactionStatus status, Throwable ex) throws TransactionException {
    _log.debug("Initiating transaction rollback on application exception", ex);
    try {
        _tx.rollback(status);
    } catch (TransactionSystemException e) {
        _log.error("Application exception overridden by rollback exception", ex);
        e.initApplicationException(ex);
        throw e;
    } catch (RuntimeException e) {
        _log.error("Application exception overridden by rollback exception", ex);
        throw e;
    } catch (Error err) {
        _log.error("Application exception overridden by rollback error", ex);
        throw err;
    }
}

From source file:org.jamwiki.db.DatabaseConnection.java

/**
 * Perform a rollback, handling rollback exceptions properly.
 * @param status object representing the transaction
 * @param ex the thrown application exception or error
 * @throws TransactionException in case of a rollback error
 *///from w w  w . j  av a  2s.  c o m
protected static void rollbackOnException(TransactionStatus status, Throwable ex) throws TransactionException {
    logger.debug("Initiating transaction rollback on application exception", ex);
    if (status == null) {
        logger.info("TransactionStatus is null, unable to rollback");
        return;
    }
    try {
        transactionManager.rollback(status);
    } catch (TransactionSystemException ex2) {
        logger.error("Application exception overridden by rollback exception", ex);
        ex2.initApplicationException(ex);
        throw ex2;
    } catch (RuntimeException ex2) {
        logger.error("Application exception overridden by rollback exception", ex);
        throw ex2;
    } catch (Error err) {
        logger.error("Application exception overridden by rollback error", ex);
        throw err;
    }
}

From source file:org.springframework.transaction.interceptor.TransactionAspectSupport.java

/**
 * General delegate for around-advice-based subclasses, delegating to several other template
 * methods on this class. Able to handle {@link CallbackPreferringPlatformTransactionManager}
 * as well as regular {@link PlatformTransactionManager} implementations.
 * @param method the Method being invoked
 * @param targetClass the target class that we're invoking the method on
 * @param invocation the callback to use for proceeding with the target invocation
 * @return the return value of the method, if any
 * @throws Throwable propagated from the target invocation
 *///from  w  w w  .  j av  a2 s.  c o  m
@Nullable
protected Object invokeWithinTransaction(Method method, @Nullable Class<?> targetClass,
        final InvocationCallback invocation) throws Throwable {

    // If the transaction attribute is null, the method is non-transactional.
    TransactionAttributeSource tas = getTransactionAttributeSource();
    final TransactionAttribute txAttr = (tas != null ? tas.getTransactionAttribute(method, targetClass) : null);
    final PlatformTransactionManager tm = determineTransactionManager(txAttr);
    final String joinpointIdentification = methodIdentification(method, targetClass, txAttr);

    if (txAttr == null || !(tm instanceof CallbackPreferringPlatformTransactionManager)) {
        // Standard transaction demarcation with getTransaction and commit/rollback calls.
        TransactionInfo txInfo = createTransactionIfNecessary(tm, txAttr, joinpointIdentification);
        Object retVal = null;
        try {
            // This is an around advice: Invoke the next interceptor in the chain.
            // This will normally result in a target object being invoked.
            retVal = invocation.proceedWithInvocation();
        } catch (Throwable ex) {
            // target invocation exception
            completeTransactionAfterThrowing(txInfo, ex);
            throw ex;
        } finally {
            cleanupTransactionInfo(txInfo);
        }
        commitTransactionAfterReturning(txInfo);
        return retVal;
    }

    else {
        final ThrowableHolder throwableHolder = new ThrowableHolder();

        // It's a CallbackPreferringPlatformTransactionManager: pass a TransactionCallback in.
        try {
            Object result = ((CallbackPreferringPlatformTransactionManager) tm).execute(txAttr, status -> {
                TransactionInfo txInfo = prepareTransactionInfo(tm, txAttr, joinpointIdentification, status);
                try {
                    return invocation.proceedWithInvocation();
                } catch (Throwable ex) {
                    if (txAttr.rollbackOn(ex)) {
                        // A RuntimeException: will lead to a rollback.
                        if (ex instanceof RuntimeException) {
                            throw (RuntimeException) ex;
                        } else {
                            throw new ThrowableHolderException(ex);
                        }
                    } else {
                        // A normal return value: will lead to a commit.
                        throwableHolder.throwable = ex;
                        return null;
                    }
                } finally {
                    cleanupTransactionInfo(txInfo);
                }
            });

            // Check result state: It might indicate a Throwable to rethrow.
            if (throwableHolder.throwable != null) {
                throw throwableHolder.throwable;
            }
            return result;
        } catch (ThrowableHolderException ex) {
            throw ex.getCause();
        } catch (TransactionSystemException ex2) {
            if (throwableHolder.throwable != null) {
                logger.error("Application exception overridden by commit exception", throwableHolder.throwable);
                ex2.initApplicationException(throwableHolder.throwable);
            }
            throw ex2;
        } catch (Throwable ex2) {
            if (throwableHolder.throwable != null) {
                logger.error("Application exception overridden by commit exception", throwableHolder.throwable);
            }
            throw ex2;
        }
    }
}

From source file:org.springframework.transaction.interceptor.TransactionAspectSupport.java

/**
 * Handle a throwable, completing the transaction.
 * We may commit or roll back, depending on the configuration.
 * @param txInfo information about the current transaction
 * @param ex throwable encountered/*from  w w w . j  ava  2  s.c o  m*/
 */
protected void completeTransactionAfterThrowing(@Nullable TransactionInfo txInfo, Throwable ex) {
    if (txInfo != null && txInfo.getTransactionStatus() != null) {
        if (logger.isTraceEnabled()) {
            logger.trace("Completing transaction for [" + txInfo.getJoinpointIdentification()
                    + "] after exception: " + ex);
        }
        if (txInfo.transactionAttribute != null && txInfo.transactionAttribute.rollbackOn(ex)) {
            try {
                txInfo.getTransactionManager().rollback(txInfo.getTransactionStatus());
            } catch (TransactionSystemException ex2) {
                logger.error("Application exception overridden by rollback exception", ex);
                ex2.initApplicationException(ex);
                throw ex2;
            } catch (RuntimeException | Error ex2) {
                logger.error("Application exception overridden by rollback exception", ex);
                throw ex2;
            }
        } else {
            // We don't roll back on this exception.
            // Will still roll back if TransactionStatus.isRollbackOnly() is true.
            try {
                txInfo.getTransactionManager().commit(txInfo.getTransactionStatus());
            } catch (TransactionSystemException ex2) {
                logger.error("Application exception overridden by commit exception", ex);
                ex2.initApplicationException(ex);
                throw ex2;
            } catch (RuntimeException | Error ex2) {
                logger.error("Application exception overridden by commit exception", ex);
                throw ex2;
            }
        }
    }
}

From source file:org.springframework.transaction.support.TransactionTemplate.java

/**
 * Perform a rollback, handling rollback exceptions properly.
 * @param status object representing the transaction
 * @param ex the thrown application exception or error
 * @throws TransactionException in case of a rollback error
 *///ww w.  ja v a  2  s. c  om
private void rollbackOnException(TransactionStatus status, Throwable ex) throws TransactionException {
    Assert.state(this.transactionManager != null, "No PlatformTransactionManager set");

    logger.debug("Initiating transaction rollback on application exception", ex);
    try {
        this.transactionManager.rollback(status);
    } catch (TransactionSystemException ex2) {
        logger.error("Application exception overridden by rollback exception", ex);
        ex2.initApplicationException(ex);
        throw ex2;
    } catch (RuntimeException | Error ex2) {
        logger.error("Application exception overridden by rollback exception", ex);
        throw ex2;
    }
}