Example usage for javax.transaction Status STATUS_COMMITTING

List of usage examples for javax.transaction Status STATUS_COMMITTING

Introduction

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

Prototype

int STATUS_COMMITTING

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

Click Source Link

Document

A transaction is associated with the target object and it is in the process of committing.

Usage

From source file:org.alfresco.util.transaction.SpringAwareUserTransaction.java

public synchronized void setRollbackOnly() throws IllegalStateException, SystemException {
    // just a check
    TransactionInfo txnInfo = getTransactionInfo();

    int status = getStatus();
    // check the status
    if (status == Status.STATUS_MARKED_ROLLBACK) {
        // this is acceptable
    } else if (status == Status.STATUS_NO_TRANSACTION) {
        throw new IllegalStateException("The transaction has not been started yet");
    } else if (status == Status.STATUS_ROLLING_BACK || status == Status.STATUS_ROLLEDBACK) {
        throw new IllegalStateException("The transaction has already been rolled back");
    } else if (status == Status.STATUS_COMMITTING || status == Status.STATUS_COMMITTED) {
        throw new IllegalStateException("The transaction has already been committed");
    } else if (status != Status.STATUS_ACTIVE) {
        throw new IllegalStateException("The transaction is not active: " + status);
    }//  w ww  .j a  v a  2 s  . com

    // mark for rollback
    txnInfo.getTransactionStatus().setRollbackOnly();
    // make sure that we record the fact that we have been marked for rollback
    internalStatus = Status.STATUS_MARKED_ROLLBACK;
    // done
    if (logger.isDebugEnabled()) {
        logger.debug("Set transaction status to rollback only: " + this);
    }
}

From source file:org.alfresco.util.transaction.SpringAwareUserTransaction.java

/**
 * @throws IllegalStateException if a transaction was not started
 *//* ww  w  .  ja v  a2  s.co m*/
public synchronized void commit() throws RollbackException, HeuristicMixedException, HeuristicRollbackException,
        SecurityException, IllegalStateException, SystemException {
    // perform checks
    TransactionInfo txnInfo = getTransactionInfo();

    int status = getStatus();
    // check the status
    if (status == Status.STATUS_NO_TRANSACTION) {
        throw new IllegalStateException("The transaction has not yet begun");
    } else if (status == Status.STATUS_ROLLING_BACK || status == Status.STATUS_ROLLEDBACK) {
        throw new RollbackException("The transaction has already been rolled back");
    } else if (status == Status.STATUS_MARKED_ROLLBACK) {
        throw new RollbackException("The transaction has already been marked for rollback");
    } else if (status == Status.STATUS_COMMITTING || status == Status.STATUS_COMMITTED) {
        throw new IllegalStateException("The transaction has already been committed");
    } else if (status != Status.STATUS_ACTIVE || txnInfo == null) {
        throw new IllegalStateException("No user transaction is active");
    }

    if (!finalized) {
        try {
            // the status seems correct - we can try a commit
            commitTransactionAfterReturning(txnInfo);
        } catch (Throwable e) {
            if (logger.isDebugEnabled()) {
                logger.debug("Transaction didn't commit", e);
            }
            // commit failed
            internalStatus = Status.STATUS_ROLLEDBACK;
            RollbackException re = new RollbackException("Transaction didn't commit: " + e.getMessage());
            // Stick the originating reason for failure into the exception.
            re.initCause(e);
            throw re;
        } finally {
            // make sure that we clean up the stack
            cleanupTransactionInfo(txnInfo);
            finalized = true;
            // clean up leaked transaction logging
            isBeginMatched = true;
            beginCallStack = null;
        }
    }

    // regardless of whether the transaction was finally committed or not, the status
    // as far as UserTransaction is concerned should be 'committed'

    // keep track that this UserTransaction was explicitly committed
    internalStatus = Status.STATUS_COMMITTED;

    // done
    if (logger.isDebugEnabled()) {
        logger.debug("Committed user transaction: " + this);
    }
}

From source file:org.alfresco.util.transaction.SpringAwareUserTransaction.java

public synchronized void rollback() throws IllegalStateException, SecurityException, SystemException {
    // perform checks
    TransactionInfo txnInfo = getTransactionInfo();

    int status = getStatus();
    // check the status
    if (status == Status.STATUS_ROLLING_BACK || status == Status.STATUS_ROLLEDBACK) {
        throw new IllegalStateException("The transaction has already been rolled back");
    } else if (status == Status.STATUS_COMMITTING || status == Status.STATUS_COMMITTED) {
        throw new IllegalStateException("The transaction has already been committed");
    } else if (txnInfo == null) {
        throw new IllegalStateException("No user transaction is active");
    }//from   w w  w .j  av a 2 s  .com

    if (!finalized) {
        try {
            // force a rollback by generating an exception that will trigger a rollback
            completeTransactionAfterThrowing(txnInfo, new Exception());
        } finally {
            // make sure that we clean up the stack
            cleanupTransactionInfo(txnInfo);
            finalized = true;
            // clean up leaked transaction logging
            isBeginMatched = true;
            beginCallStack = null;
        }
    }

    // the internal status notes that we were specifically rolled back 
    internalStatus = Status.STATUS_ROLLEDBACK;

    // done
    if (logger.isDebugEnabled()) {
        logger.debug("Rolled back user transaction: " + this);
    }
}

From source file:org.alfresco.repo.search.impl.lucene.AbstractLuceneIndexerImpl.java

/**
 * Utility method to report errors about invalid state.
 * // w ww .  j  a v a  2 s . com
 * @return - an error based on status
 */
private String buildErrorString() {
    StringBuilder buffer = new StringBuilder(128);
    buffer.append("The indexer is unable to accept more work: ");
    switch (getStatus().getStatus()) {
    case Status.STATUS_COMMITTED:
        buffer.append("The indexer has been committed");
        break;
    case Status.STATUS_COMMITTING:
        buffer.append("The indexer is committing");
        break;
    case Status.STATUS_MARKED_ROLLBACK:
        buffer.append("The indexer is marked for rollback");
        break;
    case Status.STATUS_PREPARED:
        buffer.append("The indexer is prepared to commit");
        break;
    case Status.STATUS_PREPARING:
        buffer.append("The indexer is preparing to commit");
        break;
    case Status.STATUS_ROLLEDBACK:
        buffer.append("The indexer has been rolled back");
        break;
    case Status.STATUS_ROLLING_BACK:
        buffer.append("The indexer is rolling back");
        break;
    case Status.STATUS_UNKNOWN:
        buffer.append("The indexer is in an unknown state");
        break;
    default:
        break;
    }
    return buffer.toString();
}

From source file:org.alfresco.repo.search.impl.lucene.AbstractLuceneIndexerImpl.java

/**
 * Commit this index//from ww w .j ava  2s. c  o m
 * 
 * @throws LuceneIndexException
 */
public void commit() throws LuceneIndexException {
    if (s_logger.isDebugEnabled()) {
        s_logger.debug(Thread.currentThread().getName() + " Starting Commit");
    }
    switch (getStatus().getStatus()) {
    case Status.STATUS_COMMITTING:
        throw new LuceneIndexException("Unable to commit: Transaction is committing");
    case Status.STATUS_COMMITTED:
        throw new LuceneIndexException("Unable to commit: Transaction is commited ");
    case Status.STATUS_ROLLING_BACK:
        throw new LuceneIndexException("Unable to commit: Transaction is rolling back");
    case Status.STATUS_ROLLEDBACK:
        throw new LuceneIndexException("Unable to commit: Transaction is aleady rolled back");
    case Status.STATUS_MARKED_ROLLBACK:
        throw new LuceneIndexException("Unable to commit: Transaction is marked for roll back");
    case Status.STATUS_PREPARING:
        throw new LuceneIndexException("Unable to commit: Transaction is preparing");
    case Status.STATUS_ACTIVE:
        // special case - commit from active
        prepare();
        // drop through to do the commit;
    default:
        if (getStatus().getStatus() != Status.STATUS_PREPARED) {
            throw new LuceneIndexException("Index must be prepared to commit");
        }
        try {
            setStatus(TransactionStatus.COMMITTING);
            if (isModified()) {
                doCommit();
            }
            setStatus(TransactionStatus.COMMITTED);
        } catch (LuceneIndexException e) {
            // If anything goes wrong we try and do a roll back
            rollback();
            if (s_logger.isDebugEnabled()) {
                s_logger.debug(Thread.currentThread().getName() + " Commit Failed", e);
            }
            throw new LuceneIndexException("Commit failed", e);
        } catch (Throwable t) {
            // If anything goes wrong we try and do a roll back
            rollback();
            if (s_logger.isDebugEnabled()) {
                s_logger.debug(Thread.currentThread().getName() + " Commit Failed", t);
            }
            throw new LuceneIndexException("Commit failed", t);
        } finally {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug(Thread.currentThread().getName() + " Ending Commit");
            }

            // Make sure we tidy up
            // deleteDelta();
        }
        break;
    }
}

From source file:org.alfresco.repo.search.impl.lucene.AbstractLuceneIndexerImpl.java

/**
 * Prepare to commit At the moment this makes sure we have all the locks TODO: This is not doing proper
 * serialisation against the index as would a data base transaction.
 * //from  w ww.  j a v  a 2 s . c o m
 * @return the tx state
 * @throws LuceneIndexException
 */
public int prepare() throws LuceneIndexException {
    if (s_logger.isDebugEnabled()) {
        s_logger.debug(Thread.currentThread().getName() + " Starting Prepare");
    }
    switch (getStatus().getStatus()) {
    case Status.STATUS_COMMITTING:
        throw new IndexerException("Unable to prepare: Transaction is committing");
    case Status.STATUS_COMMITTED:
        throw new IndexerException("Unable to prepare: Transaction is commited ");
    case Status.STATUS_ROLLING_BACK:
        throw new IndexerException("Unable to prepare: Transaction is rolling back");
    case Status.STATUS_ROLLEDBACK:
        throw new IndexerException("Unable to prepare: Transaction is aleady rolled back");
    case Status.STATUS_MARKED_ROLLBACK:
        throw new IndexerException("Unable to prepare: Transaction is marked for roll back");
    case Status.STATUS_PREPARING:
        throw new IndexerException("Unable to prepare: Transaction is already preparing");
    case Status.STATUS_PREPARED:
        throw new IndexerException("Unable to prepare: Transaction is already prepared");
    default:
        try {
            setStatus(TransactionStatus.PREPARING);
            if (isModified()) {
                doPrepare();
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug(Thread.currentThread().getName() + " Waiting to Finish Preparing");
                }
            }
            setStatus(TransactionStatus.PREPARED);
            return isModified() ? XAResource.XA_OK : XAResource.XA_RDONLY;
        } catch (LuceneIndexException e) {
            setRollbackOnly();
            if (s_logger.isDebugEnabled()) {
                s_logger.debug(Thread.currentThread().getName() + " Prepare Failed", e);
            }
            throw new LuceneIndexException("Index failed to prepare", e);
        } catch (Throwable t) {
            // If anything goes wrong we try and do a roll back
            rollback();
            if (s_logger.isDebugEnabled()) {
                s_logger.debug(Thread.currentThread().getName() + " Prepare Failed", t);
            }
            throw new LuceneIndexException("Prepared failed", t);
        } finally {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug(Thread.currentThread().getName() + " Ending Prepare");
            }
        }
    }
}

From source file:org.alfresco.repo.search.impl.lucene.AbstractLuceneIndexerImpl.java

/**
 * Roll back the index changes (this just means they are never added)
 * /* w  w w .  ja v  a 2  s. c  om*/
 * @throws LuceneIndexException
 */
public void rollback() throws LuceneIndexException {
    switch (getStatus().getStatus()) {

    case Status.STATUS_COMMITTED:
        throw new IndexerException("Unable to roll back: Transaction is committed ");
    case Status.STATUS_ROLLING_BACK:
        throw new IndexerException("Unable to roll back: Transaction is rolling back");
    case Status.STATUS_ROLLEDBACK:
        throw new IndexerException("Unable to roll back: Transaction is already rolled back");
    case Status.STATUS_COMMITTING:
        // Can roll back during commit
    default:
        try {
            setStatus(TransactionStatus.ROLLINGBACK);
            doRollBack();
            setStatus(TransactionStatus.ROLLEDBACK);
        } catch (IOException e) {
            throw new LuceneIndexException("rollback failed ", e);
        }
        break;
    }
}

From source file:org.alfresco.repo.search.impl.lucene.AbstractLuceneIndexerImpl.java

/**
 * Mark this index for roll back only. This action can not be reversed. It will reject all other work and only allow
 * roll back.//  w  w w .j a v  a  2s  .c  o m
 */
public void setRollbackOnly() {
    switch (getStatus().getStatus()) {
    case Status.STATUS_COMMITTING:
        throw new IndexerException("Unable to mark for rollback: Transaction is committing");
    case Status.STATUS_COMMITTED:
        throw new IndexerException("Unable to mark for rollback: Transaction is committed");
    default:
        try {
            doSetRollbackOnly();
            setStatus(TransactionStatus.MARKED_ROLLBACK);
        } catch (IOException e) {
            throw new LuceneIndexException("Set rollback only failed ", e);
        }
        break;
    }
}

From source file:org.apache.ofbiz.entity.transaction.TransactionUtil.java

public static String getTransactionStateString(int state) {
    /*//  www .  j  a v  a  2 s .c  om
     * 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.apache.ojb.odmg.JTATxManager.java

private static String getStatusString(int status) {
    switch (status) {
    case Status.STATUS_ACTIVE:
        return "STATUS_ACTIVE";
    case Status.STATUS_COMMITTED:
        return "STATUS_COMMITTED";
    case Status.STATUS_COMMITTING:
        return "STATUS_COMMITTING";
    case Status.STATUS_MARKED_ROLLBACK:
        return "STATUS_MARKED_ROLLBACK";
    case Status.STATUS_NO_TRANSACTION:
        return "STATUS_NO_TRANSACTION";
    case Status.STATUS_PREPARED:
        return "STATUS_PREPARED";
    case Status.STATUS_PREPARING:
        return "STATUS_PREPARING";
    case Status.STATUS_ROLLEDBACK:
        return "STATUS_ROLLEDBACK";
    case Status.STATUS_ROLLING_BACK:
        return "STATUS_ROLLING_BACK";
    case Status.STATUS_UNKNOWN:
        return "STATUS_UNKNOWN";
    default:/*from w w  w .  ja  v a 2 s  .co  m*/
        return "NO STATUS FOUND";
    }
}