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.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 a va2s . c o 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.TransactionRecoveryImpl.java

private void fireCompensableStartRecovery() {
    final TransactionRepository transactionRepository = this.beanFactory.getCompensableRepository();
    CompensableLogger compensableLogger = this.beanFactory.getCompensableLogger();

    compensableLogger.recover(new TransactionRecoveryCallback() {
        public void recover(TransactionArchive archive) {
            this.recover((org.bytesoft.compensable.archive.TransactionArchive) archive);
        }/*  ww w  .  j  a  v  a 2s  . c o m*/

        public void recover(org.bytesoft.compensable.archive.TransactionArchive archive) {
            XidFactory transactionXidFactory = beanFactory.getTransactionXidFactory();

            CompensableTransactionImpl transaction = reconstructTransaction(archive);
            TransactionContext transactionContext = transaction.getTransactionContext();

            TransactionXid compensableXid = transactionContext.getXid();
            if (transactionContext.isCompensable()) {
                switch (transaction.getTransactionStatus()) {
                case Status.STATUS_ACTIVE:
                case Status.STATUS_MARKED_ROLLBACK:
                case Status.STATUS_PREPARING:
                case Status.STATUS_UNKNOWN:
                    recoverStatusIfNecessary(transaction);
                    break;
                default: // ignore
                }
            } else {
                TransactionXid transactionXid = transactionXidFactory
                        .createGlobalXid(compensableXid.getGlobalTransactionId());
                Transaction tx = recovered.get(transactionXid);
                if (tx != null) {
                    tx.setTransactionalExtra(transaction);
                    transaction.setTransactionalExtra(tx);
                }
            } // end-if (transactionContext.isCompensable())

            transactionRepository.putTransaction(compensableXid, transaction);
            transactionRepository.putErrorTransaction(compensableXid, transaction);
        }
    });
}

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
            }//  w  w w.jav  a2  s .  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();//w ww .  j a va  2  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}// w ww  . j av  a 2 s  .  c  o  m
 * @see org.castor.persist.TransactionContext#prepare()
 */
public final synchronized boolean prepare() throws TransactionAbortedException {
    ArrayList<Object> todo = new ArrayList<Object>();
    ArrayList<Object> done = new ArrayList<Object>();

    if (_status == Status.STATUS_MARKED_ROLLBACK) {
        throw new TransactionAbortedException("persist.markedRollback");
    }
    if (_status != Status.STATUS_ACTIVE) {
        throw new IllegalStateException(Messages.message("persist.noTransaction"));
    }

    try {
        // No objects in this transaction -- this is a read only transaction
        // Put it into the try block to close connections
        if (_tracker.readWriteSize() == 0) {
            _status = Status.STATUS_PREPARED;
            return false;
        }

        Collection readWriteObjects = _tracker.getReadWriteObjects();
        while (readWriteObjects.size() != done.size()) {
            todo.clear();
            Iterator rwIterator = readWriteObjects.iterator();
            while (rwIterator.hasNext()) {
                Object object = rwIterator.next();
                if (!done.contains(object)) {
                    todo.add(object);
                }
            }

            Iterator<Object> todoIterator = todo.iterator();
            while (todoIterator.hasNext()) {
                Object object = todoIterator.next();
                // Anything not marked 'deleted' or 'creating' is ready to
                // consider for store.
                if ((!_tracker.isDeleted(object)) && (!_tracker.isCreating(object))) {
                    LockEngine engine = _tracker.getMolderForObject(object).getLockEngine();
                    //_tracker.getMolderForObject(object);
                    OID oid = _tracker.getOIDForObject(object);

                    OID newoid = engine.preStore(this, oid, object, _lockTimeout);
                    if (newoid != null) {
                        _tracker.trackOIDChange(object, engine, oid, newoid);
                        _tracker.markUpdateCacheNeeded(object);
                    }
                }
                done.add(object);
            }
        }

        // preStore will actually walk all existing object and it might
        // marked
        // some object to be created (and to be removed).
        walkObjectsToBeCreated();

        // Now mark anything ready for create to create them.
        prepareForCreate();

        _status = Status.STATUS_PREPARING;
        prepareForDelete();
        _status = Status.STATUS_PREPARED;

        return true;
    } catch (Exception except) {
        _status = Status.STATUS_MARKED_ROLLBACK;
        if (except instanceof TransactionAbortedException) {
            throw (TransactionAbortedException) except;
        }
        // Any error is reported as transaction aborted
        throw new TransactionAbortedException(Messages.format("persist.nested", except), except);
    }
}

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

/**
 * {@inheritDoc}/*from ww  w. ja v a2 s.com*/
 * @see org.castor.persist.TransactionContext#rollback()
 */
public final synchronized void rollback() {
    if (_status != Status.STATUS_ACTIVE && _status != Status.STATUS_PREPARED
            && _status != Status.STATUS_MARKED_ROLLBACK) {
        throw new IllegalStateException(Messages.message("persist.noTransaction"));
    }

    // Go through all the connections opened in this transaction,
    // rollback and close them one by one.
    rollbackConnections();

    // un-delete object first
    _tracker.unmarkAllDeleted();

    // Clean the transaction locks with regards to the database engine.
    Collection readWriteObjects = _tracker.getReadWriteObjects();
    OID oid = null;
    try {
        Iterator it = readWriteObjects.iterator();
        // First revert all objects
        while (it.hasNext()) {
            Object object = it.next();
            LockEngine engine = _tracker.getMolderForObject(object).getLockEngine();
            oid = _tracker.getOIDForObject(object);
            if (!_tracker.isCreating(object)) {
                engine.revertObject(this, oid, object);
            }
        }

        // then forget object or release lock on them
        it = readWriteObjects.iterator();
        while (it.hasNext()) {
            Object object = it.next();
            ClassMolder molder = _tracker.getMolderForObject(object);
            LockEngine engine = molder.getLockEngine();
            oid = _tracker.getOIDForObject(object);

            if (!_tracker.isCreating(object)) {
                if (_tracker.isCreated(object)) {
                    // Object has been created in this transaction,
                    // it no longer exists, forget about it in the engine.
                    engine.forgetObject(this, oid);
                } else {
                    // Object has been queried (possibly) deleted in this
                    // transaction and release the lock.
                    engine.releaseLock(this, oid);
                }
            }

            if (_callback != null) {
                _callback.releasing(object, false);
            } else if (molder.getCallback() != null) {
                molder.getCallback().releasing(object, false);
            }
        }
    } catch (Exception except) {
        // Don't thow exceptions during a rollback. Just report them.
        LOG.error("Caught exception at rollback of object with OID " + oid, except);
    }

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

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

/**
 * {@inheritDoc}//from  w ww.  ja  va2  s .co m
 * @see org.castor.persist.TransactionContext#close()
 */
public final synchronized void close() throws TransactionAbortedException {
    if (_status != Status.STATUS_ACTIVE && _status != Status.STATUS_MARKED_ROLLBACK) {
        throw new IllegalStateException(Messages.message("persist.missingEnd"));
    }
    try {
        // Go through all the connections opened in this transaction,
        // close them one by one.
        closeConnections();

    } 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);
    }
}

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

/**
 * {@inheritDoc}//from ww  w  .j av a2s  .com
 * @see org.castor.persist.TransactionContext#isOpen()
 */
public final boolean isOpen() {
    return ((_status == Status.STATUS_ACTIVE) || (_status == Status.STATUS_MARKED_ROLLBACK));
}

From source file:org.commonjava.maven.galley.cache.infinispan.FastLocalCacheProvider.java

private boolean isTxActive(CacheInstance<String, String> cacheInstance) throws SystemException {
    final int[] ACTIVE_STATUS = new int[] { Status.STATUS_ACTIVE, Status.STATUS_COMMITTING,
            Status.STATUS_PREPARING, Status.STATUS_PREPARED, Status.STATUS_ROLLING_BACK };
    boolean isActive = false;
    for (int status : ACTIVE_STATUS) {
        if (cacheInstance.getTransactionStatus() == status) {
            isActive = true;//w  w w.  j  a  v  a 2  s. c  o m
            break;
        }
    }
    return isActive;
}

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

@Test
public void test_jta_existing_transaction() throws Exception {

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

    executeSmooks("jta", "test_jta_existing_transaction", REPORT);

    verify(dataSource, atLeastOnce()).getConnection();
    verify(connection).close();/*from  ww  w .  j a v a 2 s . co m*/

    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();

}