Example usage for javax.transaction Status STATUS_ACTIVE

List of usage examples for javax.transaction Status STATUS_ACTIVE

Introduction

In this page you can find the example usage for javax.transaction Status STATUS_ACTIVE.

Prototype

int STATUS_ACTIVE

To view the source code for javax.transaction Status STATUS_ACTIVE.

Click Source Link

Document

A transaction is associated with the target object and it is in the active state.

Usage

From source file:org.dhatim.db.JndiDataSourceTest.java

@Test
public void test_jta_existing_transaction_exception() throws Exception {

    when(connection.getAutoCommit()).thenReturn(false);
    when(transaction.getStatus()).thenReturn(Status.STATUS_ACTIVE);

    executeSmooksWithException("jta_exception", "test_jta_existing_transaction_exception", REPORT);

    verify(dataSource, atLeastOnce()).getConnection();
    verify(transaction).setRollbackOnly();
    verify(connection).close();/*  w  w  w . ja  va2s .  com*/

    verify(transaction, never()).begin();
    verify(transaction, never()).commit();
    verify(transaction, never()).rollback();
    verify(connection, never()).setAutoCommit(false);
    verify(connection, never()).commit();
    verify(connection, never()).rollback();

}

From source file:org.dhatim.db.JndiDataSourceTest.java

@Test
public void test_jta_set_autocommit_not_allowed() throws Exception {
    when(transaction.getStatus()).thenReturn(Status.STATUS_ACTIVE);

    executeSmooks("jta_set_autocommit_not_allowed", "test_jta_set_autocommit_not_allowed", REPORT);

    verify(dataSource, atLeastOnce()).getConnection();
    verify(connection).close();//  ww  w  .ja  v  a 2  s.c o  m

    verify(connection, never()).setAutoCommit(false);
    verify(connection, never()).getAutoCommit();
}

From source file:org.eclipse.ecr.runtime.transaction.TransactionHelper.java

/**
 * Checks if the current User Transaction is active.
 *///from   w w w  .ja v  a2s .co m
public static boolean isTransactionActive() {
    try {
        return lookupUserTransaction().getStatus() == Status.STATUS_ACTIVE;
    } catch (Exception e) {
        return false;
    }
}

From source file:org.eclipse.ecr.runtime.transaction.TransactionHelper.java

/**
 * Checks if the current User Transaction is active or marked rollback only.
 *///from   w  w w.  jav a 2 s.c  o  m
public static boolean isTransactionActiveOrMarkedRollback() {
    try {
        int status = lookupUserTransaction().getStatus();
        return status == Status.STATUS_ACTIVE || status == Status.STATUS_MARKED_ROLLBACK;
    } catch (Exception e) {
        return false;
    }
}

From source file:org.eclipse.ecr.runtime.transaction.TransactionHelper.java

/**
 * Commits or rolls back the User Transaction depending on the transaction
 * status./* w  w  w. j  a  v  a 2s. co  m*/
 *
 * @throws SystemException
 * @throws HeuristicRollbackException
 * @throws HeuristicMixedException
 * @throws RollbackException
 * @throws IllegalStateException
 * @throws SecurityException
 */
public static void commitOrRollbackTransaction() {
    UserTransaction ut;
    try {
        ut = lookupUserTransaction();
    } catch (NamingException e) {
        log.warn("No user transaction", e);
        return;
    }
    try {
        int status = ut.getStatus();
        if (status == Status.STATUS_ACTIVE) {
            if (log.isDebugEnabled()) {
                log.debug("Commiting transaction");
            }
            ut.commit();
        } else if (status == Status.STATUS_MARKED_ROLLBACK) {
            if (log.isDebugEnabled()) {
                log.debug("Cannot commit transaction because it is marked rollback only");
            }
            ut.rollback();
        }
    } catch (Exception e) {
        String msg = "Unable to commit/rollback  " + ut;
        if (e instanceof RollbackException
                && "Unable to commit: transaction marked for rollback".equals(e.getMessage())) {
            // don't log as error, this happens if there's a
            // ConcurrentModificationException at transaction end inside VCS
            log.debug(msg, e);
        } else {
            log.error(msg, e);
        }
        throw new TransactionRuntimeException(msg, e);
    }
}

From source file:org.efaps.db.Context.java

/**
 * Is the status of transaction manager active?
 *
 * @return <i>true</i> if transaction manager is active, otherwise
 *         <i>false</i>/*from  w w  w .  j a va  2  s  .c o  m*/
 * @throws EFapsException if the status of the transaction manager could not
 *             be evaluated
 * @see #TRANSMANAG
 */
public static boolean isTMActive() throws EFapsException {
    try {
        return Context.TRANSMANAG.getStatus() == Status.STATUS_ACTIVE;
    } catch (final SystemException e) {
        throw new EFapsException(Context.class, "isTMActive.SystemException", e);
    }
}

From source file:org.enhydra.jdbc.pool.StandardXAPoolDataSource.java

/**
 * This method tests if a connection is valid or not. It overrides the
 * method in StandardPoolDataSource to take into account global transactions:
 * if global transaction is in progress - suspend it so that
 * connection testing happens ouside of transaction.
 * If connection testing fails - it will not affect transaction
 * and next good connection can join the transaction
 *//* w w w.  j a  v  a2  s.  c o  m*/
public boolean testThisObject(Object o) {
    Connection ret = null;
    log.debug("StandardXAPoolDataSource:testThisObject verify the current object");
    Transaction suspended = null;
    try {
        Transaction tx = transactionManager == null ? null : transactionManager.getTransaction();
        boolean isActive = tx == null ? false : tx.getStatus() == Status.STATUS_ACTIVE;
        if (isActive) {
            suspended = transactionManager.suspend();
        }

        PooledConnection con = (PooledConnection) o;
        ret = con.getConnection();

        Statement s = ret.createStatement();
        s.execute(jdbcTestStmt);
        s.close();
        try {
            ret.close();
        } catch (Exception e) {
            log.error("StandardXAPoolDataSource:checkThisObject can't closed the connection: " + e);
        }
        return true;
    } catch (SQLException e) {
        log.error(
                "StandardXAPoolDataSource:checkThisObject Error java.sql.SQLException in StandardXAPoolDataSource:testThisObject");
        return false;
    } catch (SystemException e) {
        log.error(
                "StandardXAPoolDataSource:checkThisObject Error java.sql.SystemException in StandardXAPoolDataSource:testThisObject");
        return false;
    } finally {
        if (suspended != null) {
            try {
                transactionManager.resume(suspended);
            } catch (Exception ex) {
                log.error(
                        "StandardXAPoolDataSource:checkThisObject Error Exception in StandardXAPoolDataSource:testThisObject");
                return false;
            }
        }
    }
}

From source file:org.etk.entity.engine.plugins.transaction.TransactionUtil.java

/**
 * Begins a transaction in the current thread IF transactions are available;
 * only tries if the current transaction status is ACTIVE, if not active it
 * returns false. If and on only if it begins a transaction it will return
 * true. In other words, if a transaction is already in place it will return
 * false and do nothing./*from  w  ww  . j a  v  a 2  s .c o m*/
 */
public static boolean begin(int timeout) throws GenericTransactionException {
    UserTransaction ut = TransactionFactory.getUserTransaction();
    if (ut != null) {
        try {
            int currentStatus = ut.getStatus();
            if (logger.isDebugEnabled()) {
                logger.debug(
                        "[TransactionUtil.begin] current status : " + getTransactionStateString(currentStatus));
            }
            if (currentStatus == Status.STATUS_ACTIVE) {
                if (logger.isDebugEnabled()) {
                    logger.debug(
                            "[TransactionUtil.begin] active transaction in place, so no transaction begun");
                }
                return false;
            } else if (currentStatus == Status.STATUS_MARKED_ROLLBACK) {
                Exception e = getTransactionBeginStack();
                if (e != null) {
                    logger.warn(
                            "[TransactionUtil.begin] active transaction marked for rollback in place, so no transaction begun; this stack trace shows when the exception began: ",
                            e);
                } else {
                    logger.warn(
                            "[TransactionUtil.begin] active transaction marked for rollback in place, so no transaction begun");
                }

                RollbackOnlyCause roc = getSetRollbackOnlyCause();
                // do we have a cause? if so, throw special exception
                if (roc != null && !roc.isEmpty()) {
                    throw new GenericTransactionException(
                            "The current transaction is marked for rollback, not beginning a new transaction and aborting current operation; the rollbackOnly was caused by: "
                                    + roc.getCauseMessage(),
                            roc.getCauseThrowable());
                } else {
                    return false;
                }
            }

            internalBegin(ut, timeout);

            // reset the transaction stamps, just in case...
            clearTransactionStamps();
            // initialize the start stamp
            getTransactionStartStamp();
            // set the tx begin stack placeholder
            setTransactionBeginStack();

            // initialize the debug resource
            if (debugResources) {
                DebugXaResource dxa = new DebugXaResource();
                try {
                    dxa.enlist();
                } catch (XAException e) {
                    logger.error(e.getMessage(), e);
                }
            }

            return true;
        } catch (NotSupportedException e) {
            // This is Java 1.4 only, but useful for certain debuggins: Throwable t
            // = e.getCause() == null ? e : e.getCause();
            throw new GenericTransactionException(
                    "Not Supported error, could not begin transaction (probably a nesting problem)", e);
        } catch (SystemException e) {
            // This is Java 1.4 only, but useful for certain debuggins: Throwable t
            // = e.getCause() == null ? e : e.getCause();
            throw new GenericTransactionException("System error, could not begin transaction", e);
        }
    } else {
        if (logger.isInfoEnabled())
            logger.info("[TransactionUtil.begin] no user transaction, so no transaction begun");
        return false;
    }
}

From source file:org.etk.entity.engine.plugins.transaction.TransactionUtil.java

public static String getTransactionStateString(int state) {
    /*//from   ww w.j  a va 2s .c o  m
     * javax.transaction.Status STATUS_ACTIVE 0 STATUS_MARKED_ROLLBACK 1
     * STATUS_PREPARED 2 STATUS_COMMITTED 3 STATUS_ROLLEDBACK 4 STATUS_UNKNOWN 5
     * STATUS_NO_TRANSACTION 6 STATUS_PREPARING 7 STATUS_COMMITTING 8
     * STATUS_ROLLING_BACK 9
     */
    switch (state) {
    case Status.STATUS_ACTIVE:
        return "Transaction Active (" + state + ")";
    case Status.STATUS_COMMITTED:
        return "Transaction Committed (" + state + ")";
    case Status.STATUS_COMMITTING:
        return "Transaction Committing (" + state + ")";
    case Status.STATUS_MARKED_ROLLBACK:
        return "Transaction Marked Rollback (" + state + ")";
    case Status.STATUS_NO_TRANSACTION:
        return "No Transaction (" + state + ")";
    case Status.STATUS_PREPARED:
        return "Transaction Prepared (" + state + ")";
    case Status.STATUS_PREPARING:
        return "Transaction Preparing (" + state + ")";
    case Status.STATUS_ROLLEDBACK:
        return "Transaction Rolledback (" + state + ")";
    case Status.STATUS_ROLLING_BACK:
        return "Transaction Rolling Back (" + state + ")";
    case Status.STATUS_UNKNOWN:
        return "Transaction Status Unknown (" + state + ")";
    default:
        return "Not a valid state code (" + state + ")";
    }
}

From source file:org.firstopen.singularity.util.TransactionManager.java

public void commit() {

    try {/*from  w  ww .  j  a  v a  2s  .  c  o  m*/
        /*
         * close any DAO Sessions related to this 
         * transaction.
         */
        DAOUtilFactory.close();

        UserTransaction userTransaction = TransactionUtil.getUserTransaction();
        if (userTransaction.getStatus() == Status.STATUS_ACTIVE) {
            userTransaction.commit();
            log.debug("transaction commit completed!");
        }
    } catch (Exception e) {
        log.error("commit failed!");
        // rollback();
        throw new InfrastructureException(e);
    }

}