Example usage for org.springframework.transaction.support TransactionSynchronization STATUS_UNKNOWN

List of usage examples for org.springframework.transaction.support TransactionSynchronization STATUS_UNKNOWN

Introduction

In this page you can find the example usage for org.springframework.transaction.support TransactionSynchronization STATUS_UNKNOWN.

Prototype

int STATUS_UNKNOWN

To view the source code for org.springframework.transaction.support TransactionSynchronization STATUS_UNKNOWN.

Click Source Link

Document

Completion status in case of heuristic mixed completion or system errors.

Usage

From source file:org.springframework.transaction.jta.SpringJtaSynchronizationAdapter.java

/**
 * JTA {@code afterCompletion} callback: invoked after commit/rollback.
 * <p>Needs to invoke the Spring synchronization's {@code beforeCompletion}
 * at this late stage in case of a rollback, since there is no corresponding
 * callback with JTA./*  ww  w  . j a  v  a2s  .  co m*/
 * @see org.springframework.transaction.support.TransactionSynchronization#beforeCompletion
 * @see org.springframework.transaction.support.TransactionSynchronization#afterCompletion
 */
@Override
public void afterCompletion(int status) {
    if (!this.beforeCompletionCalled) {
        // beforeCompletion not called before (probably because of JTA rollback).
        // Perform the cleanup here.
        this.springSynchronization.beforeCompletion();
    }
    // Call afterCompletion with the appropriate status indication.
    switch (status) {
    case Status.STATUS_COMMITTED:
        this.springSynchronization.afterCompletion(TransactionSynchronization.STATUS_COMMITTED);
        break;
    case Status.STATUS_ROLLEDBACK:
        this.springSynchronization.afterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK);
        break;
    default:
        this.springSynchronization.afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
    }
}

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

/**
 * Process an actual commit./*from  www.  jav  a 2s. c o m*/
 * Rollback-only flags have already been checked and applied.
 * @param status object representing the transaction
 * @throws TransactionException in case of commit failure
 */
private void processCommit(DefaultTransactionStatus status) throws TransactionException {
    try {
        boolean beforeCompletionInvoked = false;

        try {
            boolean unexpectedRollback = false;
            prepareForCommit(status);
            triggerBeforeCommit(status);
            triggerBeforeCompletion(status);
            beforeCompletionInvoked = true;

            if (status.hasSavepoint()) {
                if (status.isDebug()) {
                    logger.debug("Releasing transaction savepoint");
                }
                unexpectedRollback = status.isGlobalRollbackOnly();
                status.releaseHeldSavepoint();
            } else if (status.isNewTransaction()) {
                if (status.isDebug()) {
                    logger.debug("Initiating transaction commit");
                }
                unexpectedRollback = status.isGlobalRollbackOnly();
                doCommit(status);
            } else if (isFailEarlyOnGlobalRollbackOnly()) {
                unexpectedRollback = status.isGlobalRollbackOnly();
            }

            // Throw UnexpectedRollbackException if we have a global rollback-only
            // marker but still didn't get a corresponding exception from commit.
            if (unexpectedRollback) {
                throw new UnexpectedRollbackException(
                        "Transaction silently rolled back because it has been marked as rollback-only");
            }
        } catch (UnexpectedRollbackException ex) {
            // can only be caused by doCommit
            triggerAfterCompletion(status, TransactionSynchronization.STATUS_ROLLED_BACK);
            throw ex;
        } catch (TransactionException ex) {
            // can only be caused by doCommit
            if (isRollbackOnCommitFailure()) {
                doRollbackOnCommitException(status, ex);
            } else {
                triggerAfterCompletion(status, TransactionSynchronization.STATUS_UNKNOWN);
            }
            throw ex;
        } catch (RuntimeException | Error ex) {
            if (!beforeCompletionInvoked) {
                triggerBeforeCompletion(status);
            }
            doRollbackOnCommitException(status, ex);
            throw ex;
        }

        // Trigger afterCommit callbacks, with an exception thrown there
        // propagated to callers but the transaction still considered as committed.
        try {
            triggerAfterCommit(status);
        } finally {
            triggerAfterCompletion(status, TransactionSynchronization.STATUS_COMMITTED);
        }

    } finally {
        cleanupAfterCompletion(status);
    }
}

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

/**
 * Process an actual rollback.//from   w  w w . j a  v a  2 s . c om
 * The completed flag has already been checked.
 * @param status object representing the transaction
 * @throws TransactionException in case of rollback failure
 */
private void processRollback(DefaultTransactionStatus status, boolean unexpected) {
    try {
        boolean unexpectedRollback = unexpected;

        try {
            triggerBeforeCompletion(status);

            if (status.hasSavepoint()) {
                if (status.isDebug()) {
                    logger.debug("Rolling back transaction to savepoint");
                }
                status.rollbackToHeldSavepoint();
            } else if (status.isNewTransaction()) {
                if (status.isDebug()) {
                    logger.debug("Initiating transaction rollback");
                }
                doRollback(status);
            } else {
                // Participating in larger transaction
                if (status.hasTransaction()) {
                    if (status.isLocalRollbackOnly() || isGlobalRollbackOnParticipationFailure()) {
                        if (status.isDebug()) {
                            logger.debug(
                                    "Participating transaction failed - marking existing transaction as rollback-only");
                        }
                        doSetRollbackOnly(status);
                    } else {
                        if (status.isDebug()) {
                            logger.debug(
                                    "Participating transaction failed - letting transaction originator decide on rollback");
                        }
                    }
                } else {
                    logger.debug("Should roll back transaction but cannot - no transaction available");
                }
                // Unexpected rollback only matters here if we're asked to fail early
                if (!isFailEarlyOnGlobalRollbackOnly()) {
                    unexpectedRollback = false;
                }
            }
        } catch (RuntimeException | Error ex) {
            triggerAfterCompletion(status, TransactionSynchronization.STATUS_UNKNOWN);
            throw ex;
        }

        triggerAfterCompletion(status, TransactionSynchronization.STATUS_ROLLED_BACK);

        // Raise UnexpectedRollbackException if we had a global rollback-only marker
        if (unexpectedRollback) {
            throw new UnexpectedRollbackException(
                    "Transaction rolled back because it has been marked as rollback-only");
        }
    } finally {
        cleanupAfterCompletion(status);
    }
}

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

/**
 * Invoke {@code doRollback}, handling rollback exceptions properly.
 * @param status object representing the transaction
 * @param ex the thrown application exception or error
 * @throws TransactionException in case of rollback failure
 * @see #doRollback/*  w  w w.ja v a 2s .com*/
 */
private void doRollbackOnCommitException(DefaultTransactionStatus status, Throwable ex)
        throws TransactionException {
    try {
        if (status.isNewTransaction()) {
            if (status.isDebug()) {
                logger.debug("Initiating transaction rollback after commit exception", ex);
            }
            doRollback(status);
        } else if (status.hasTransaction() && isGlobalRollbackOnParticipationFailure()) {
            if (status.isDebug()) {
                logger.debug("Marking existing transaction as rollback-only after commit exception", ex);
            }
            doSetRollbackOnly(status);
        }
    } catch (RuntimeException | Error rbex) {
        logger.error("Commit exception overridden by rollback exception", ex);
        triggerAfterCompletion(status, TransactionSynchronization.STATUS_UNKNOWN);
        throw rbex;
    }
    triggerAfterCompletion(status, TransactionSynchronization.STATUS_ROLLED_BACK);
}

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

/**
 * Register the given list of transaction synchronizations with the existing transaction.
 * <p>Invoked when the control of the Spring transaction manager and thus all Spring
 * transaction synchronizations end, without the transaction being completed yet. This
 * is for example the case when participating in an existing JTA or EJB CMT transaction.
 * <p>The default implementation simply invokes the {@code afterCompletion} methods
 * immediately, passing in "STATUS_UNKNOWN". This is the best we can do if there's no
 * chance to determine the actual outcome of the outer transaction.
 * @param transaction transaction object returned by {@code doGetTransaction}
 * @param synchronizations List of TransactionSynchronization objects
 * @throws TransactionException in case of system errors
 * @see #invokeAfterCompletion(java.util.List, int)
 * @see TransactionSynchronization#afterCompletion(int)
 * @see TransactionSynchronization#STATUS_UNKNOWN
 *//*from w w w. j a  v  a2  s  .c  o  m*/
protected void registerAfterCompletionWithExistingTransaction(Object transaction,
        List<TransactionSynchronization> synchronizations) throws TransactionException {

    logger.debug("Cannot register Spring after-completion synchronization with existing transaction - "
            + "processing Spring after-completion callbacks immediately, with outcome status 'unknown'");
    invokeAfterCompletion(synchronizations, TransactionSynchronization.STATUS_UNKNOWN);
}