Example usage for javax.transaction Status STATUS_COMMITTED

List of usage examples for javax.transaction Status STATUS_COMMITTED

Introduction

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

Prototype

int STATUS_COMMITTED

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

Click Source Link

Document

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

Usage

From source file:org.apache.openjpa.kernel.BrokerImpl.java

/**
 * End the current transaction, making appropriate state transitions.
 *//* w  ww  .  ja  va 2  s  .  co m*/
protected void endTransaction(int status) {
    // if a data store transaction was in progress, do the
    // appropriate transaction change
    boolean rollback = status != Status.STATUS_COMMITTED;
    List<Exception> exceps = null;

    try {
        exceps = add(exceps, endStoreManagerTransaction(rollback));
    } catch (RuntimeException re) {
        rollback = true;
        exceps = add(exceps, re);
    }

    // go back to default none lock level
    _fc.setReadLockLevel(LOCK_NONE);
    _fc.setWriteLockLevel(LOCK_NONE);
    _fc.setLockTimeout(-1);

    Collection transStates;
    if (hasTransactionalObjects())
        transStates = _transCache;
    else
        transStates = Collections.EMPTY_SET;

    // fire after rollback/commit event
    Collection mobjs = null;
    if (_transEventManager.hasEndListeners()) {
        mobjs = new ManagedObjectCollection(transStates);
        int eventType = (rollback) ? TransactionEvent.AFTER_ROLLBACK : TransactionEvent.AFTER_COMMIT;
        fireTransactionEvent(
                new TransactionEvent(this, eventType, mobjs, _persistedClss, _updatedClss, _deletedClss));
    }

    // null transactional caches now so that all the removeFromTransaction
    // calls as we transition each object don't have to do any work; don't
    // clear trans cache object because we still need the transStates
    // reference to it below
    _transCache = null;
    if (_persistedClss != null)
        _persistedClss = null;
    if (_updatedClss != null)
        _updatedClss = null;
    if (_deletedClss != null)
        _deletedClss = null;

    // new cache would get cleared anyway during transitions, but doing so
    // immediately saves us some lookups
    _cache.clearNew();

    // tell all derefed instances they're no longer derefed; we can't
    // rely on rollback and commit calls below cause some instances might
    // not be transactional
    if (_derefCache != null && !_derefCache.isEmpty()) {
        for (Iterator<StateManagerImpl> itr = _derefCache.iterator(); itr.hasNext();)
            itr.next().setDereferencedDependent(false, false);
        _derefCache = null;
    }

    // perform commit or rollback state transitions on each instance
    StateManagerImpl sm;
    for (Iterator itr = transStates.iterator(); itr.hasNext();) {
        sm = (StateManagerImpl) itr.next();
        try {
            if (rollback) {
                // tell objects that may have been derefed then flushed
                // (and therefore deleted) to un-deref
                sm.setDereferencedDependent(false, false);
                sm.rollback();
            } else {
                if (sm.getPCState() == PCState.PNEWDELETED || sm.getPCState() == PCState.PDELETED) {
                    fireLifecycleEvent(sm.getPersistenceCapable(), null, sm.getMetaData(),
                            LifecycleEvent.AFTER_DELETE_PERFORMED);
                }
                sm.commit();
            }
        } catch (RuntimeException re) {
            exceps = add(exceps, re);
        }
    }

    // notify the lock manager to clean up and release remaining locks
    _lm.endTransaction();

    // clear old savepoints in reverse
    OpenJPASavepoint save;
    while (_savepoints != null && _savepoints.size() > 0) {
        save = (OpenJPASavepoint) _savepoints.remove(_savepoints.size() - 1);
        save.release(false);
    }
    _savepoints = null;
    _savepointCache = null;

    // fire after state change event
    if (_transEventManager.hasEndListeners())
        fireTransactionEvent(
                new TransactionEvent(this, TransactionEvent.AFTER_STATE_TRANSITIONS, mobjs, null, null, null));

    // now clear trans cache; keep cleared version rather than
    // null to avoid having to re-create the set later; more efficient
    if (transStates != Collections.EMPTY_SET) {
        _transCache = (TransactionalCache) transStates;
        _transCache.clear();
    }

    throwNestedExceptions(exceps, true);
}

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();/* ww  w. j  a va2s  .  c  om*/
        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.");
    }
}

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

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

    TransactionImpl transactionImpl = (TransactionImpl) transaction;
    switch (transaction.getTransactionStatus()) {
    case Status.STATUS_PREPARED:
    case Status.STATUS_COMMITTING:
        break;/*  w  w w.  j av  a  2s .co  m*/
    case Status.STATUS_COMMITTED:
    case Status.STATUS_ROLLEDBACK:
        break;
    case Status.STATUS_ACTIVE:
    case Status.STATUS_MARKED_ROLLBACK:
    case Status.STATUS_PREPARING:
    case Status.STATUS_UNKNOWN:
    case Status.STATUS_ROLLING_BACK:
    default:
        transactionImpl.recoveryRollback();
        transactionImpl.forgetQuietly();
    }
}

From source file:org.bytesoft.bytetcc.CompensableTransactionImpl.java

public synchronized void commit() throws RollbackException, HeuristicMixedException, HeuristicRollbackException,
        SecurityException, IllegalStateException, SystemException {

    CompensableLogger compensableLogger = this.beanFactory.getCompensableLogger();

    this.transactionContext.setCompensating(true);
    this.transactionStatus = Status.STATUS_COMMITTING;
    compensableLogger.updateTransaction(this.getTransactionArchive());

    SystemException systemEx = null;
    try {//w  w  w .  j a v a2  s  .c  o  m
        this.fireNativeParticipantConfirm();
    } catch (SystemException ex) {
        systemEx = ex;

        logger.info("{}| confirm native branchs failed!",
                ByteUtils.byteArrayToString(this.transactionContext.getXid().getGlobalTransactionId()), ex);
    } catch (RuntimeException ex) {
        systemEx = new SystemException(ex.getMessage());

        logger.info("{}| confirm native branchs failed!",
                ByteUtils.byteArrayToString(this.transactionContext.getXid().getGlobalTransactionId()), ex);
    }

    try {
        this.fireRemoteParticipantConfirm();
    } catch (HeuristicMixedException ex) {
        logger.info("{}| confirm remote branchs failed!",
                ByteUtils.byteArrayToString(this.transactionContext.getXid().getGlobalTransactionId()), ex);
        throw ex;
    } catch (HeuristicRollbackException ex) {
        logger.info("{}| confirm remote branchs failed!",
                ByteUtils.byteArrayToString(this.transactionContext.getXid().getGlobalTransactionId()), ex);
        throw ex;
    } catch (SystemException ex) {
        logger.info("{}| confirm remote branchs failed!",
                ByteUtils.byteArrayToString(this.transactionContext.getXid().getGlobalTransactionId()), ex);
        throw ex;
    } catch (RuntimeException ex) {
        logger.info("{}| confirm remote branchs failed!",
                ByteUtils.byteArrayToString(this.transactionContext.getXid().getGlobalTransactionId()), ex);
        throw ex;
    }

    if (systemEx != null) {
        throw systemEx;
    } else {
        this.transactionStatus = Status.STATUS_COMMITTED;
        compensableLogger.updateTransaction(this.getTransactionArchive());
        logger.info("{}| compensable transaction committed!",
                ByteUtils.byteArrayToString(transactionContext.getXid().getGlobalTransactionId()));
    }

}

From source file:org.bytesoft.bytetcc.CompensableTransactionImpl.java

public synchronized void recoveryCommit() throws CommitRequiredException, SystemException {
    CompensableLogger compensableLogger = this.beanFactory.getCompensableLogger();

    this.transactionContext.setCompensating(true);
    this.transactionStatus = Status.STATUS_COMMITTING;
    compensableLogger.updateTransaction(this.getTransactionArchive());

    SystemException systemEx = null;
    try {// w  ww  . j  a va  2  s  . c om
        this.fireNativeParticipantRecoveryConfirm();
    } catch (SystemException ex) {
        systemEx = ex;

        logger.info("{}| recovery-confirm native branchs failed!",
                ByteUtils.byteArrayToString(this.transactionContext.getXid().getGlobalTransactionId()), ex);
    } catch (RuntimeException ex) {
        systemEx = new SystemException(ex.getMessage());

        logger.info("{}| recovery-confirm native branchs failed!",
                ByteUtils.byteArrayToString(this.transactionContext.getXid().getGlobalTransactionId()), ex);
    }

    try {
        this.fireRemoteParticipantRecoveryConfirm();
    } catch (HeuristicMixedException ex) {
        logger.info("{}| recovery-confirm remote branchs failed!",
                ByteUtils.byteArrayToString(this.transactionContext.getXid().getGlobalTransactionId()), ex);
        throw new SystemException(ex.getMessage());
    } catch (HeuristicRollbackException ex) {
        logger.info("{}| recovery-confirm remote branchs failed!",
                ByteUtils.byteArrayToString(this.transactionContext.getXid().getGlobalTransactionId()), ex);
        throw new SystemException(ex.getMessage());
    } catch (SystemException ex) {
        logger.info("{}| recovery-confirm remote branchs failed!",
                ByteUtils.byteArrayToString(this.transactionContext.getXid().getGlobalTransactionId()), ex);
        throw ex;
    } catch (RuntimeException ex) {
        logger.info("{}| recovery-confirm remote branchs failed!",
                ByteUtils.byteArrayToString(this.transactionContext.getXid().getGlobalTransactionId()), ex);
        throw new SystemException(ex.getMessage());
    }

    if (systemEx != null) {
        throw systemEx;
    } else {
        this.transactionStatus = Status.STATUS_COMMITTED;
        compensableLogger.updateTransaction(this.getTransactionArchive());
        logger.info("{}| compensable transaction recovery committed!",
                ByteUtils.byteArrayToString(transactionContext.getXid().getGlobalTransactionId()));
    }

}

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

private void recoverStatusIfNecessary(Transaction transaction) {
    CompensableTransactionImpl compensable = (CompensableTransactionImpl) transaction;
    List<CompensableArchive> archiveList = compensable.getCompensableArchiveList();

    XAResourceDeserializer resourceDeserializer = this.beanFactory.getResourceDeserializer();
    CompensableLogger compensableLogger = this.beanFactory.getCompensableLogger();

    Map<TransactionBranchKey, Boolean> triedMap = new HashMap<TransactionBranchKey, Boolean>();
    for (int i = 0; i < archiveList.size(); i++) {
        CompensableArchive archive = archiveList.get(i);

        if (archive.isTried()) {
            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.setTransactionStatus(Status.STATUS_PREPARED);
                transaction.getTransactionContext().setCompensating(true);
                compensableLogger.updateTransaction(compensable.getTransactionArchive());
                break;
            case Status.STATUS_PREPARED:
            case Status.STATUS_COMMITTING:
            case Status.STATUS_COMMITTED:
            case Status.STATUS_ROLLEDBACK:
            default:
                // ignore
            }/*  www.j a  v a 2s. c  om*/
        } else {
            Xid transactionXid = archive.getTransactionXid();
            String resourceKey = archive.getTransactionResourceKey();

            TransactionBranchKey recordKey = new TransactionBranchKey();
            recordKey.xid = transactionXid;
            recordKey.resource = resourceKey;

            if (StringUtils.isBlank(resourceKey)) {
                logger.warn(
                        "There is no valid resource participated in the trying branch transaction, the status of the branch transaction is unknown!");
            } else if (triedMap.containsKey(recordKey)) {
                Boolean tried = triedMap.get(recordKey);
                if (Boolean.TRUE.equals(tried)) {
                    transaction.setTransactionStatus(Status.STATUS_COMMITTING); // TODO
                } else {
                    transaction.setTransactionStatus(Status.STATUS_MARKED_ROLLBACK);
                }
                transaction.getTransactionContext().setCompensating(true);
                compensableLogger.updateTransaction(compensable.getTransactionArchive());
            } else {
                XAResource xares = resourceDeserializer.deserialize(resourceKey);
                if (RecoveredResource.class.isInstance(xares)) {
                    RecoveredResource resource = (RecoveredResource) xares;
                    try {
                        resource.recoverable(archive.getTransactionXid());
                        archive.setTried(true);
                        triedMap.put(recordKey, Boolean.TRUE);

                        transaction.setTransactionStatus(Status.STATUS_COMMITTING); // TODO
                        transaction.getTransactionContext().setCompensating(true);
                        compensableLogger.updateTransaction(compensable.getTransactionArchive());
                    } catch (XAException xaex) {
                        switch (xaex.errorCode) {
                        case XAException.XAER_NOTA:
                            triedMap.put(recordKey, Boolean.FALSE);

                            transaction.setTransactionStatus(Status.STATUS_MARKED_ROLLBACK);
                            transaction.getTransactionContext().setCompensating(true);
                            compensableLogger.updateTransaction(compensable.getTransactionArchive());
                            break;
                        case XAException.XAER_RMERR:
                            logger.warn(
                                    "The database table 'bytejta' cannot found, the status of the trying branch transaction is unknown!");
                            break;
                        case XAException.XAER_RMFAIL:
                            logger.error("Error occurred while recovering the branch transaction service: {}",
                                    ByteUtils.byteArrayToString(transactionXid.getGlobalTransactionId()), xaex);
                            break;
                        default:
                            logger.error(
                                    "Illegal state, the status of the trying branch transaction is unknown!");
                        }
                    }
                } else {
                    logger.error("Illegal resources, the status of the trying branch transaction is unknown!");
                }
            }
        } // end-else-if (archive.isTried())
    } // end-for
}

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

public synchronized 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  w w w.j ava2  s.  c  om
        transaction.recoveryForget();
        break;
    case Status.STATUS_PREPARED:
    case Status.STATUS_COMMITTING:
        transaction.recoveryCommit();
        transaction.recoveryForget();
        break;
    case Status.STATUS_COMMITTED:
    case Status.STATUS_ROLLEDBACK:
    default:
        // ignore
    }

}

From source file:org.castor.persist.AbstractTransactionContext.java

/**
 * {@inheritDoc}//from  www .ja v  a2s .  co m
 * @see org.castor.persist.TransactionContext#commit()
 */
public final synchronized void commit() throws TransactionAbortedException {
    // Never commit transaction that has been marked for rollback
    if (_status == Status.STATUS_MARKED_ROLLBACK) {
        throw new TransactionAbortedException("persist.markedRollback");
    }
    if (_status != Status.STATUS_PREPARED) {
        throw new IllegalStateException(Messages.message("persist.missingPrepare"));
    }

    try {
        _status = Status.STATUS_COMMITTING;

        // Go through all the connections opened in this transaction,
        // commit and close them one by one.
        commitConnections();
    } catch (Exception except) {
        // Any error that happens, we're going to rollback the transaction.
        _status = Status.STATUS_MARKED_ROLLBACK;
        throw new TransactionAbortedException(Messages.format("persist.nested", except), except);
    }

    // Assuming all went well in the connection department,
    // no deadlocks, etc. clean all the transaction locks with
    // regards to the persistence engine.
    Collection readWriteObjects = _tracker.getReadWriteObjects();
    Iterator it = readWriteObjects.iterator();
    while (it.hasNext()) {
        Object toCommit = it.next();

        ClassMolder molder = _tracker.getMolderForObject(toCommit);
        LockEngine engine = molder.getLockEngine();
        OID oid = _tracker.getOIDForObject(toCommit);

        if (_tracker.isDeleted(toCommit)) {
            // Object has been deleted inside transaction,
            // engine must forget about it.
            engine.forgetObject(this, oid);
        } else {
            // Object has been created/accessed inside the
            // transaction, release its lock.
            if (_tracker.isUpdateCacheNeeded(toCommit)) {
                engine.updateCache(this, oid, toCommit);
            }

            engine.releaseLock(this, oid);
        }

        // Call our release callback on all processed objects.
        if (_callback != null) {
            _callback.releasing(toCommit, true);
        } else if (molder.getCallback() != null) {
            molder.getCallback().releasing(toCommit, true);
        }
    }

    // Call txcommited() before objects are removed to allow
    // TxSynchronizable to iterate through the objects.
    txcommitted();

    // Forget about all the objects in this transaction,
    // and mark it as completed.
    _tracker.clear();
    _status = Status.STATUS_COMMITTED;
}

From source file:org.compass.core.transaction.AbstractJTATransaction.java

public boolean wasCommitted() throws TransactionException {

    if (!isBegun() || commitFailed)
        return false;

    final int status;
    try {//from w ww  .  jav  a 2s.  c om
        status = ut.getStatus();
    } catch (SystemException se) {
        throw new TransactionException("Could not determine transaction status: ", se);
    }
    if (status == Status.STATUS_UNKNOWN) {
        throw new TransactionException("Could not determine transaction status");
    } else {
        return status == Status.STATUS_COMMITTED;
    }
}

From source file:org.eclipse.ecr.core.api.TransactionalCoreSessionWrapper.java

@Override
public void afterCompletion(int status) {
    threadBound.remove();//w w  w  .j  a  v a2 s.  c  o m
    boolean committed;
    if (status == Status.STATUS_COMMITTED) {
        committed = true;
    } else if (status == Status.STATUS_ROLLEDBACK) {
        committed = false;
    } else {
        log.error("Unexpected status after completion: " + status);
        return;
    }
    session.afterCompletion(committed);
}