Example usage for javax.transaction Status STATUS_PREPARED

List of usage examples for javax.transaction Status STATUS_PREPARED

Introduction

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

Prototype

int STATUS_PREPARED

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

Click Source Link

Document

A transaction is associated with the target object and it has been prepared.

Usage

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

/**
 * Utility method to report errors about invalid state.
 * // w  w  w.j  a  va  2s .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   w  w  w.  j  a  va 2  s  . 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  w  w.  ja v  a  2  s .co 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.apache.ofbiz.entity.transaction.TransactionUtil.java

public static String getTransactionStateString(int state) {
    /*//from w  w  w  .  ja  va 2 s.  co 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.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:// w  w  w. jav  a2 s .c  o  m
        return "NO STATUS FOUND";
    }
}

From source file:org.apache.ojb.odmg.TransactionImpl.java

/**
 * Determine whether the transaction is open or not. A transaction is open if
 * a call has been made to <code>begin</code> , but a subsequent call to
 * either <code>commit</code> or <code>abort</code> has not been made.
 * @return    True if the transaction is open, otherwise false.
 *///from www. j a va2s . c  o m
public boolean isOpen() {
    return (getStatus() == Status.STATUS_ACTIVE || getStatus() == Status.STATUS_MARKED_ROLLBACK
            || getStatus() == Status.STATUS_PREPARED || getStatus() == Status.STATUS_PREPARING
            || getStatus() == Status.STATUS_COMMITTING);
}

From source file:org.apache.ojb.odmg.TransactionImpl.java

protected void checkForCommit() {
    // Never commit transaction that has been marked for rollback
    if (txStatus == Status.STATUS_MARKED_ROLLBACK)
        throw new TransactionAbortedExceptionOJB("Illegal tx-status: tx is already markedRollback");
    // Don't commit if not prepared
    if (txStatus != Status.STATUS_PREPARED)
        throw new IllegalStateException("Illegal tx-status: Do prepare commit before commit");
}

From source file:org.apache.ojb.odmg.TransactionImpl.java

/**
 * Prepare does the actual work of moving the changes at the object level
 * into storage (the underlying rdbms for instance). prepare Can be called multiple times, and
 * does not release locks.//  w  w  w  . j a  v a  2s. com
 *
 * @throws TransactionAbortedException if the transaction has been aborted
 * for any reason.
 * @throws  IllegalStateException Method called if transaction is
 *  not in the proper state to perform this operation
 * @throws TransactionNotInProgressException if the transaction is closed.
 */
protected void prepareCommit() throws TransactionAbortedException, LockNotGrantedException {
    if (txStatus == Status.STATUS_MARKED_ROLLBACK) {
        throw new TransactionAbortedExceptionOJB("Prepare Transaction: tx already marked for rollback");
    }
    if (txStatus != Status.STATUS_ACTIVE) {
        throw new IllegalStateException("Prepare Transaction: tx status is not 'active', status is "
                + TxUtil.getStatusString(txStatus));
    }
    try {
        txStatus = Status.STATUS_PREPARING;
        doWriteObjects(false);
        txStatus = Status.STATUS_PREPARED;
    } catch (RuntimeException e) {
        log.error("Could not prepare for commit", e);
        txStatus = Status.STATUS_MARKED_ROLLBACK;
        throw e;
    }
}

From source file:org.apache.ojb.odmg.TransactionImpl.java

/**
 * Abort and close the transaction. Calling abort abandons all persistent
 * object modifications and releases the associated locks. Aborting a
 * transaction does not restore the state of modified transient objects
 *//*from ww w.j  av  a2  s  . c o m*/
public void abort() {
    /*
    do nothing if already rolledback
    */
    if (txStatus == Status.STATUS_NO_TRANSACTION || txStatus == Status.STATUS_UNKNOWN
            || txStatus == Status.STATUS_ROLLEDBACK) {
        log.info("Nothing to abort, tx is not active - status is " + TxUtil.getStatusString(txStatus));
        return;
    }
    // check status of tx
    if (txStatus != Status.STATUS_ACTIVE && txStatus != Status.STATUS_PREPARED
            && txStatus != Status.STATUS_MARKED_ROLLBACK) {
        throw new IllegalStateException(
                "Illegal state for abort call, state was '" + TxUtil.getStatusString(txStatus) + "'");
    }
    if (log.isEnabledFor(Logger.INFO)) {
        log.info("Abort transaction was called on tx " + this);
    }
    try {
        try {
            doAbort();
        } catch (Exception e) {
            log.error("Error while abort transaction, will be skipped", e);
        }

        // used in managed environments, ignored in non-managed
        this.implementation.getTxManager().abortExternalTx(this);

        try {
            if (hasBroker() && getBroker().isInTransaction()) {
                getBroker().abortTransaction();
            }
        } catch (Exception e) {
            log.error("Error while do abort used broker instance, will be skipped", e);
        }
    } finally {
        txStatus = Status.STATUS_ROLLEDBACK;
        // cleanup things, e.g. release all locks
        doClose();
    }
}

From source file:org.bytesoft.bytejta.TransactionRecoveryImpl.java

private void recoverCoordinator(Transaction transaction)
        throws CommitRequiredException, RollbackRequiredException, SystemException {

    switch (transaction.getTransactionStatus()) {
    case Status.STATUS_ACTIVE:
    case Status.STATUS_MARKED_ROLLBACK:
    case Status.STATUS_PREPARING:
    case Status.STATUS_ROLLING_BACK:
    case Status.STATUS_UNKNOWN:
        transaction.recoveryRollback();/*from   www .j  ava 2 s . com*/
        transaction.forgetQuietly();
        break;
    case Status.STATUS_PREPARED:
    case Status.STATUS_COMMITTING:
        transaction.recoveryCommit();
        transaction.forgetQuietly();
        break;
    case Status.STATUS_COMMITTED:
    case Status.STATUS_ROLLEDBACK:
        transaction.forgetQuietly();
        break;
    default:
        logger.debug("Current transaction has already been completed.");
    }
}