Example usage for org.springframework.transaction TransactionSystemException TransactionSystemException

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

Introduction

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

Prototype

public TransactionSystemException(String msg) 

Source Link

Document

Constructor for TransactionSystemException.

Usage

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

@Override
protected DefaultTransactionStatus newTransactionStatus(TransactionDefinition definition, Object transaction,
        boolean newTransaction, boolean newSynchronization, boolean debug, Object suspendedResources) {
    // Validate isolation level and time-out settings 
    validateIsolationLevel(definition.getIsolationLevel());

    boolean actualNewSynchronization = newSynchronization
            && !TransactionSynchronizationManager.isSynchronizationActive();

    GlobalTransaction gtx = (GlobalTransaction) transaction;
    Set<GlobalTransactionState> gtxStateSet = getGlobalTransactionStates(gtx);

    GlobalTransaction newGtx;//from  w w  w.j  a va2  s .c  o  m

    switch (definition.getPropagationBehavior()) {
    case TransactionAttribute.PROPAGATION_NESTED:
        if (gtxStateSet.contains(GlobalTransactionState.GlobalTransactionInstance)) {
            if (!isNestedTransactionAllowed()) {
                throw new TransactionSystemException(String.format(
                        "Unexpected system state: the value of nestedTransactionAllowed boolean "
                                + "member field is false. It should have been checked early as true within "
                                + "handleExistingTransaction method, in order to provide the nested "
                                + "propagation behavior over the transaction (what has already begun "
                                + "GlobalTransaction instance (ID:%1$s)) by nested begin and commit/rollback calls.",
                        (gtxStateSet.contains(GlobalTransactionState.GlobalTransactionInstance) ? gtx.getId()
                                : "null")));
            }
            if (logger.isInfoEnabled()) {
                logger.info(String.format(
                        "Going to provide nested propagation behavior by nested begin and "
                                + "commit/rollback calls on new GlobalTransaction instance over the "
                                + "transaction what has already begun GlobalTransaction instance (ID:%1$s).",
                        gtx.getId()));
            }
        }
        // Enter to below logic (for TransactionAttribute.PROPAGATION_REQUIRES_NEW case)
    case TransactionAttribute.PROPAGATION_REQUIRES_NEW:
        // Sanity check on system state
        if (!newTransaction) {
            throw new IllegalTransactionStateException(String.format(
                    "Unexpected system state: the value of newTransaction boolean member field "
                            + "is false for %1$s propagation behavior (%2$d). Exptected it to be true.",
                    getPropagationBehaviorStr(definition.getPropagationBehavior()),
                    definition.getPropagationBehavior()));
        }

        // Sanity check on current GlobalTransaction instances
        if (gtxStateSet.contains(GlobalTransactionState.GlobalTransactionInstance)) {
            validateCurrentActiveGlobalTransactionToParticipate(gtx, definition);
        }

        // begin new GlobalTransaction
        newGtx = beginGlogalTransaction(gtx, gtxStateSet, definition);

        return new DefaultTransactionStatus(newGtx, newTransaction, actualNewSynchronization,
                definition.isReadOnly(), debug, suspendedResources);
    case TransactionAttribute.PROPAGATION_REQUIRED:
        if (newTransaction) {
            // Sanity check on current GlobalTransaction instances
            if (gtxStateSet.contains(GlobalTransactionState.GlobalTransactionInstance)) {
                throw new IllegalTransactionStateException(String.format(
                        "Unexpected system state: the value of newTransaction boolean member "
                                + "field is true what direct to create new transaction for %1$s "
                                + "propagation behavior (%2$d) though this transaction has already begun "
                                + "the GlobalTransaction instance (ID:%3$s). Exptected it to be false and "
                                + "participate to the existing transaction.",
                        getPropagationBehaviorStr(definition.getPropagationBehavior()),
                        definition.getPropagationBehavior(), gtx.getId()));
            }

            // begin new GlobalTransaction
            newGtx = beginGlogalTransaction(gtx, gtxStateSet, definition);

            return new DefaultTransactionStatus(newGtx, newTransaction, actualNewSynchronization,
                    definition.isReadOnly(), debug, suspendedResources);
        } else {
            // Sanity check on current GlobalTransaction instances
            validateCurrentActiveGlobalTransactionToParticipate(gtx, definition);

            return new DefaultTransactionStatus(gtx, newTransaction, actualNewSynchronization,
                    definition.isReadOnly(), debug, suspendedResources);
        }
    case TransactionAttribute.PROPAGATION_NEVER:
        if (newTransaction && !gtxStateSet.contains(GlobalTransactionState.GlobalTransactionInstance)) {
            return new DefaultTransactionStatus(null, newTransaction, actualNewSynchronization,
                    definition.isReadOnly(), debug, suspendedResources);
        } else {
            throw new IllegalTransactionStateException(String.format(
                    "Unexpected system state: for %1$s propagation behavior (%2$d), the value of "
                            + "newTransaction boolean member field is expected to be true (actual: %3$b) "
                            + "and transaction Object argument is expected to hold current active "
                            + "GlobalTransaction instance (actual: %4$s %5$s one (ID:%6$s)).",
                    getPropagationBehaviorStr(definition.getPropagationBehavior()),
                    definition.getPropagationBehavior(), newTransaction,
                    (gtxStateSet.contains(GlobalTransactionState.CurrentGlobalTransaction) ? "current"
                            : "not-current"),
                    (gtxStateSet.contains(GlobalTransactionState.ActiveGlobalTransaction) ? "active"
                            : "inactive"),
                    (gtxStateSet.contains(GlobalTransactionState.GlobalTransactionInstance) ? gtx.getId()
                            : "null")));
        }
    case TransactionAttribute.PROPAGATION_NOT_SUPPORTED:
        if (gtxStateSet.contains(GlobalTransactionState.GlobalTransactionInstance)) {
            throw new IllegalTransactionStateException(String.format(
                    "Unexpected system state: for %1$s propagation behavior (%2$d), the "
                            + "transaction Object argument is expected to hold null value (actual: "
                            + "%3$s %4$s GlobalTransaction instance (ID:%5$s)).",
                    getPropagationBehaviorStr(definition.getPropagationBehavior()),
                    definition.getPropagationBehavior(),
                    (gtxStateSet.contains(GlobalTransactionState.CurrentGlobalTransaction) ? "current"
                            : "not-current"),
                    (gtxStateSet.contains(GlobalTransactionState.ActiveGlobalTransaction) ? "active"
                            : "inactive"),
                    gtx.getId()));
        }

        // Create DeafultTransactionState with null transaction
        return new DefaultTransactionStatus(null, newTransaction, actualNewSynchronization,
                definition.isReadOnly(), debug, suspendedResources);
    case TransactionAttribute.PROPAGATION_SUPPORTS:
        if (newTransaction) {
            if (gtxStateSet.contains(GlobalTransactionState.GlobalTransactionInstance)) {
                throw new IllegalTransactionStateException(String.format(
                        "Unexpected system state: for %1$s propagation behavior (%2$d), when the "
                                + "newTransaction is true, the transaction Object argument is expected to "
                                + "hold null value (actual: %3$s %4$s GlobalTransaction instance (ID:%5$s)).",
                        getPropagationBehaviorStr(definition.getPropagationBehavior()),
                        definition.getPropagationBehavior(),
                        (gtxStateSet.contains(GlobalTransactionState.CurrentGlobalTransaction) ? "current"
                                : "not-current"),
                        (gtxStateSet.contains(GlobalTransactionState.ActiveGlobalTransaction) ? "active"
                                : "inactive"),
                        gtx.getId()));
            }
            return new DefaultTransactionStatus(null, newTransaction, actualNewSynchronization,
                    definition.isReadOnly(), debug, suspendedResources);
        } else {
            if (!gtxStateSet.contains(GlobalTransactionState.ActiveGlobalTransaction)
                    || !gtxStateSet.contains(GlobalTransactionState.CurrentGlobalTransaction)) {
                throw new IllegalTransactionStateException(String.format(
                        "Unexpected system state: for %1$s propagation behavior (%2$d), when the "
                                + "newTransaction is false, the transaction Object argument is expected to "
                                + "hold current active GlobalTransaction instance (actual: %3$s %4$s "
                                + "GlobalTransaction instance (ID:%5$s)).",
                        getPropagationBehaviorStr(definition.getPropagationBehavior()),
                        definition.getPropagationBehavior(),
                        (gtxStateSet.contains(GlobalTransactionState.CurrentGlobalTransaction) ? "current"
                                : "not-current"),
                        (gtxStateSet.contains(GlobalTransactionState.ActiveGlobalTransaction) ? "active"
                                : "inactive"),
                        (gtxStateSet.contains(GlobalTransactionState.GlobalTransactionInstance) ? gtx.getId()
                                : "null")));
            }
            return new DefaultTransactionStatus(gtx, newTransaction, actualNewSynchronization,
                    definition.isReadOnly(), debug, suspendedResources);
        }

    case TransactionAttribute.PROPAGATION_MANDATORY:
        if (newTransaction || !gtxStateSet.contains(GlobalTransactionState.ActiveGlobalTransaction)
                || !gtxStateSet.contains(GlobalTransactionState.CurrentGlobalTransaction)) {
            throw new IllegalTransactionStateException(String.format(
                    "Unexpected system state: for %1$s propagation behavior (%2$d), the "
                            + "newTransaction is expected to be false (actual: %3$b) and the transaction "
                            + "Object argument is expected to hold current active GlobalTransaction "
                            + "instance (actual: %3$s %4$s GlobalTransaction instance (ID:%5$s)).",
                    getPropagationBehaviorStr(definition.getPropagationBehavior()),
                    definition.getPropagationBehavior(),
                    (gtxStateSet.contains(GlobalTransactionState.CurrentGlobalTransaction) ? "current"
                            : "not-current"),
                    (gtxStateSet.contains(GlobalTransactionState.ActiveGlobalTransaction) ? "active"
                            : "inactive"),
                    (gtxStateSet.contains(GlobalTransactionState.GlobalTransactionInstance) ? gtx.getId()
                            : "null")));
        }

        return new DefaultTransactionStatus(gtx, newTransaction, actualNewSynchronization,
                definition.isReadOnly(), debug, suspendedResources);

    default:
        throw new IllegalTransactionStateException(String.format(
                "Unknown propagation behavior (%1$d) is specified. Supported propagation "
                        + "behaviors is either PROPAGATION_MANDATORY (%2$d), PROPAGATION_NESTED (%3$d), "
                        + "PROPAGATION_NEVER (%4$d), PROPAGATION_NOT_SUPPORTED (%5$d), "
                        + "PROPAGATION_REQUIRED (%6$d), PROPAGATION_REQUIRES_NEW (%7$d), "
                        + "or PROPAGATION_SUPPORTS (%8$d).",
                definition.getPropagationBehavior(), TransactionAttribute.PROPAGATION_MANDATORY,
                TransactionAttribute.PROPAGATION_NESTED, TransactionAttribute.PROPAGATION_NEVER,
                TransactionAttribute.PROPAGATION_NOT_SUPPORTED, TransactionAttribute.PROPAGATION_REQUIRED,
                TransactionAttribute.PROPAGATION_REQUIRES_NEW, TransactionAttribute.PROPAGATION_SUPPORTS));

    } // switch
}

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

@Override
protected void doRollback(DefaultTransactionStatus defaultTransactionStatus) throws TransactionException {
    // Sanity check on precondition -----------------------------------------------------------
    /* When logic flow got here, it should be not the case of participating existing transaction that  
     * means the target method is the outer most transactional method what began a transaction process 
     * at the first, and that also means newTransaction boolean member field of DefaultTransactionStatus 
     * object should be true.//from   ww  w  . j  ava  2s .  com
     */
    if (!defaultTransactionStatus.isNewTransaction()) {
        // Could be caused by unaware code change on org.springframework.transaction.support.AbstractPlatformTransactionManager
        throw new TransactionSystemException(String.format(
                "Unexpected system state: attempting to roll back from participation of an "
                        + "existing transaction (of which GlobalTransacion Id is %1$s).",
                ((defaultTransactionStatus.getTransaction() instanceof GlobalTransaction)
                        ? ((GlobalTransaction) defaultTransactionStatus.getTransaction()).getId()
                        : "null")));
    }
    // ----------------------------------------------------------------------------------------

    // Check on GlobalTransaction instance ----------------------------------------------------
    GlobalTransaction gtx = (GlobalTransaction) defaultTransactionStatus.getTransaction();
    Set<GlobalTransactionState> gtxStateSet = getGlobalTransactionStates(gtx);
    if (!gtxStateSet.contains(GlobalTransactionState.GlobalTransactionInstance)) {
        throw new TransactionSystemException(
                "Inconsistent status: defaultTransactionStatus argument is not holding "
                        + "GlobalTransaction instance.");
    }

    String gtxIdStr = gtx.getId();
    Map<String, Slim3GlobalTransactionObject> slim3GtxObjMap = slim3GtxObjMapThreadLocal.get();
    Slim3GlobalTransactionObject slim3GtxObj = slim3GtxObjMap.get(gtxIdStr);
    if (slim3GtxObj == null) {
        if (logger.isErrorEnabled()) {
            logger.error(String.format("Encountered inconsistent status: GlobalTransaction instance (ID:%1$s) "
                    + "held by defaultTransactionStatus argument is not among ones managed " + "by %2$s.",
                    gtxIdStr, this.getClass().getName()));
        }
    }

    if (!gtxStateSet.contains(GlobalTransactionState.ActiveGlobalTransaction)) {
        /* Reminder: gtx here can be inactive by the ConcurrentModificationException case. 
         * However, it's not 100% certain that, in ConcurrentModificationException case, 
         * gtx here will be always inactive.
         */

        slim3GtxObjMapThreadLocal.get().remove(gtxIdStr);

        if (logger.isWarnEnabled()) {
            logger.warn(String.format("Skipping to perform roll back on GlobalTransaction (ID:%1$s) since "
                    + "it has been already in inactive state.", gtxIdStr));
        }

        return;
    }

    if (!(gtxStateSet.contains(GlobalTransactionState.CurrentGlobalTransaction))) {
        /* Changed from throwing TransactionSystemException to logging incident in order to 
         * avoid leaving GlobalTransaction instance active as much as possible by proceeding further.
         */
        GlobalTransaction currentGtx = Datastore.getCurrentGlobalTransaction();

        if (logger.isErrorEnabled()) {
            logger.error(String.format(
                    "Encountered inconsistent status: GlobalTransaction object (ID:%1$s) held "
                            + "by defaultTransactionStatus argument is not current active GlobalTransaction "
                            + "instance (ID:%2$s).",
                    gtxIdStr, ((currentGtx instanceof GlobalTransaction) ? currentGtx.getId() : "null")));
        }
    }
    // ----------------------------------------------------------------------------------------

    try {
        if (logger.isInfoEnabled()) {
            logger.info(String.format("Rolling back on Slim3 GlobalTransaction (ID:%1$s)", gtxIdStr));
        }

        /* Note: There are the cases that GlobalTransaction instance has already been rolled back by 
         * Slim3 behind scene of Spring transaction framework when commit failed and GlobalTransaction 
         * instance remains active state. Even the control flow reached in those cases, there should be 
         * no harm being made by rolling back here again. 
         */
        gtx.rollback();
    } catch (Throwable throwable) {
        String message = String.format("Rolling back on Slim3 GlobalTransaction (ID:%1$s) failed.", gtxIdStr);
        throw new TransactionSystemException(message, throwable);
    } finally {
        slim3GtxObjMapThreadLocal.get().remove(gtxIdStr);
        /* Not necessary to call defaultTransactionStatus.setCompleted method in this block because 
         * it will be called later in cleanupAfterCompletion method.
                 defaultTransactionStatus.setCompleted();
         */
        if (gtx.isActive()) {
            if (logger.isErrorEnabled()) {
                logger.error(String.format("Slim3 GlobalTransaction (ID:%1$s) remains active "
                        + "after the attempt of rolling back.", gtxIdStr));
            }
        }
    }
}

From source file:jp.terasoluna.fw.batch.util.BatchUtilTest.java

/**
 * ???//w  w w  .  ja  v  a2 s.  c  o m
 * 
 * <pre>
 * ??
 * {@code TLogger}?{@code commons.logging.Log}??{@code BatchUtil}?????
 * ?
 * ??{@code error()}??????????
 * </pre>
 */
public void testSwitchLogger02() {
    // 
    TransactionDefinition def = BatchUtil.getTransactionDefinition();
    Map<String, PlatformTransactionManager> tranMap = new HashMap<String, PlatformTransactionManager>();
    PlatformTransactionManager tran = new PlatformTransactionManagerStub();
    Log mockCommonsLog = LogFactory.getLog(BatchUtilTest.class);
    TLogger mockLog = (TLogger) spy(log);
    mockCommonsLog = spy(mockCommonsLog);

    tran = spy(tran);
    tranMap.put("tran", tran);
    when(tran.getTransaction(def)).thenThrow(new TransactionSystemException("test exception"));

    // 
    try {
        BatchUtil.startTransactions(def, tranMap, mockLog);
        fail();
    } catch (TransactionException e) {
        assertTrue(e instanceof TransactionException);
    }
    try {
        BatchUtil.startTransactions(def, tranMap, mockCommonsLog);
        fail();
    } catch (TransactionException e) {
        assertEquals("test exception", e.getMessage());
    }

    // ?
    verify(mockCommonsLog).error(any());
    verify(mockLog).error(any(String.class), any(), any(String.class));

}

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

/**
 * This constructor retrieves the WebSphere TransactionManager factory class,
 * so we can get access to the JTA TransactionManager.
 *//*from   w  w w  .  j av  a2s . co m*/
public WebSphereTransactionManagerFactoryBean() throws TransactionSystemException {
    Class clazz;
    try {
        logger.debug("Trying WebSphere 5.1+: " + FACTORY_CLASS_5_1);
        clazz = Class.forName(FACTORY_CLASS_5_1);
        logger.info("Found WebSphere 5.1+: " + FACTORY_CLASS_5_1);
    } catch (ClassNotFoundException ex) {
        logger.debug("Could not find WebSphere 5.1/6.0 TransactionManager factory class", ex);
        try {
            logger.debug("Trying WebSphere 5.0: " + FACTORY_CLASS_5_0);
            clazz = Class.forName(FACTORY_CLASS_5_0);
            logger.info("Found WebSphere 5.0: " + FACTORY_CLASS_5_0);
        } catch (ClassNotFoundException ex2) {
            logger.debug("Could not find WebSphere 5.0 TransactionManager factory class", ex2);
            try {
                logger.debug("Trying WebSphere 4: " + FACTORY_CLASS_4);
                clazz = Class.forName(FACTORY_CLASS_4);
                logger.info("Found WebSphere 4: " + FACTORY_CLASS_4);
            } catch (ClassNotFoundException ex3) {
                logger.debug("Could not find WebSphere 4 TransactionManager factory class", ex3);
                throw new TransactionSystemException(
                        "Could not find any WebSphere TransactionManager factory class, "
                                + "neither for WebSphere version 5.1+ nor 5.0 nor 4");
            }
        }
    }

    try {
        Method method = clazz.getMethod("getTransactionManager", (Class[]) null);
        this.transactionManager = (TransactionManager) method.invoke(null, (Object[]) null);
    } catch (InvocationTargetException ex) {
        throw new TransactionSystemException(
                "WebSphere's TransactionManagerFactory.getTransactionManager method failed",
                ex.getTargetException());
    } catch (Exception ex) {
        throw new TransactionSystemException(
                "Could not access WebSphere's TransactionManagerFactory.getTransactionManager method", ex);
    }
}