Example usage for org.springframework.transaction.support DefaultTransactionStatus hasTransaction

List of usage examples for org.springframework.transaction.support DefaultTransactionStatus hasTransaction

Introduction

In this page you can find the example usage for org.springframework.transaction.support DefaultTransactionStatus hasTransaction.

Prototype

public boolean hasTransaction() 

Source Link

Document

Return whether there is an actual transaction active.

Usage

From source file:com.newmainsoftech.spray.slingong.datastore.Slim3PlatformTransactionManagerTest.java

/**
 * @param transactionCommited: true when committed. false for not committed, but it's not for verifying 
 * roll back./*from w w  w .  j av  a 2  s  .c  om*/
 */
protected void verifyCommitProcess(boolean transactionCommited) {
    /* Mockito cannot spy on "final" method because it cannot mock "final" method: 
     * @see http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html#13      
          mockedSlim3TxMangerInOrder.verify( slim3PlatformTransactionManager, Mockito.times( 1))
          .commit( transactionStatusArgumentCaptor.capture());
    */
    // protected boolean shouldCommitOnGlobalRollbackOnly()
    // private void processCommit(DefaultTransactionStatus status)
    // protected void prepareForCommit(DefaultTransactionStatus status)
    // protected final void triggerBeforeCommit(DefaultTransactionStatus status)
    // protected final void triggerBeforeCompletion(DefaultTransactionStatus status)
    // If it's not new transaction (means not outer most transaction), then call public final boolean isFailEarlyOnGlobalRollbackOnly()

    ArgumentCaptor<DefaultTransactionStatus> defaultTransactionStatusArgumentCaptor = ArgumentCaptor
            .forClass(DefaultTransactionStatus.class);
    if (transactionCommited) {
        mockedSlim3TxMangerInOrder.verify(slim3PlatformTransactionManager, Mockito.times(1))
                .doCommit(defaultTransactionStatusArgumentCaptor.capture());
        DefaultTransactionStatus defaultTransactionStatus = (DefaultTransactionStatus) (defaultTransactionStatusArgumentCaptor
                .getValue());
        Assert.assertTrue(defaultTransactionStatus.isCompleted()); // should be true since actually commit has already finished
        Assert.assertTrue(defaultTransactionStatus.isNewTransaction());
        Assert.assertTrue(defaultTransactionStatus.hasTransaction());
        Assert.assertTrue(defaultTransactionStatus.getTransaction() instanceof GlobalTransaction);

        ArgumentCaptor<GlobalTransaction> globalTransactionArgumentCaptor = ArgumentCaptor
                .forClass(GlobalTransaction.class);
        mockedSlim3TxMangerInOrder.verify(slim3PlatformTransactionManager, Mockito.times(1))
                .getGlobalTransactionStates(globalTransactionArgumentCaptor.capture());
        GlobalTransaction gtx = globalTransactionArgumentCaptor.getValue();
        Assert.assertTrue(gtx instanceof GlobalTransaction);
    } else {
        mockedSlim3TxMangerInOrder.verify(slim3PlatformTransactionManager, Mockito.never())
                .doCommit(defaultTransactionStatusArgumentCaptor.capture());
    }

    // private void triggerAfterCommit(DefaultTransactionStatus status)
    // private void triggerAfterCompletion(DefaultTransactionStatus status, int completionStatus)
    // private void cleanupAfterCompletion(DefaultTransactionStatus status)
    // protected void doCleanupAfterCompletion(Object transaction)
}

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

/**
 * Initialize transaction synchronization as appropriate.
 *//*from   www .j  a  va2s. co  m*/
protected void prepareSynchronization(DefaultTransactionStatus status, TransactionDefinition definition) {
    if (status.isNewSynchronization()) {
        TransactionSynchronizationManager.setActualTransactionActive(status.hasTransaction());
        TransactionSynchronizationManager.setCurrentTransactionIsolationLevel(
                definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT
                        ? definition.getIsolationLevel()
                        : null);
        TransactionSynchronizationManager.setCurrentTransactionReadOnly(definition.isReadOnly());
        TransactionSynchronizationManager.setCurrentTransactionName(definition.getName());
        TransactionSynchronizationManager.initSynchronization();
    }
}

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

/**
 * Process an actual rollback.//from   www . j ava2  s  . c o  m
 * 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/*from   w  ww .j a  v a 2s  .  c om*/
 */
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

/**
 * Trigger {@code afterCompletion} callbacks.
 * @param status object representing the transaction
 * @param completionStatus completion status according to TransactionSynchronization constants
 *//*  w  ww. ja  v a2 s.  co  m*/
private void triggerAfterCompletion(DefaultTransactionStatus status, int completionStatus) {
    if (status.isNewSynchronization()) {
        List<TransactionSynchronization> synchronizations = TransactionSynchronizationManager
                .getSynchronizations();
        TransactionSynchronizationManager.clearSynchronization();
        if (!status.hasTransaction() || status.isNewTransaction()) {
            if (status.isDebug()) {
                logger.trace("Triggering afterCompletion synchronization");
            }
            // No transaction or new transaction for the current scope ->
            // invoke the afterCompletion callbacks immediately
            invokeAfterCompletion(synchronizations, completionStatus);
        } else if (!synchronizations.isEmpty()) {
            // Existing transaction that we participate in, controlled outside
            // of the scope of this Spring transaction manager -> try to register
            // an afterCompletion callback with the existing (JTA) transaction.
            registerAfterCompletionWithExistingTransaction(status.getTransaction(), synchronizations);
        }
    }
}

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

/**
 * Clean up after completion, clearing synchronization if necessary,
 * and invoking doCleanupAfterCompletion.
 * @param status object representing the transaction
 * @see #doCleanupAfterCompletion/*from  w  ww  . j  a  v a  2 s  .  co m*/
 */
private void cleanupAfterCompletion(DefaultTransactionStatus status) {
    status.setCompleted();
    if (status.isNewSynchronization()) {
        TransactionSynchronizationManager.clear();
    }
    if (status.isNewTransaction()) {
        doCleanupAfterCompletion(status.getTransaction());
    }
    if (status.getSuspendedResources() != null) {
        if (status.isDebug()) {
            logger.debug("Resuming suspended transaction after completion of inner transaction");
        }
        Object transaction = (status.hasTransaction() ? status.getTransaction() : null);
        resume(transaction, (SuspendedResourcesHolder) status.getSuspendedResources());
    }
}