Example usage for javax.transaction.xa XAResource XA_RDONLY

List of usage examples for javax.transaction.xa XAResource XA_RDONLY

Introduction

In this page you can find the example usage for javax.transaction.xa XAResource XA_RDONLY.

Prototype

int XA_RDONLY

To view the source code for javax.transaction.xa XAResource XA_RDONLY.

Click Source Link

Document

The transaction branch has been read-only and has been committed.

Usage

From source file:com.taobao.metamorphosis.server.transaction.XATransaction.java

@Override
public int prepare() throws XAException, IOException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("XA Transaction prepare: " + this.xid);
    }/*  w ww .  j  a v  a  2  s  . c  om*/

    switch (this.getState()) {
    case START_STATE:
        // No work done.. no commit/rollback needed.
        this.setStateFinished();
        return XAResource.XA_RDONLY;
    case IN_USE_STATE:
        // We would record prepare here.
        this.doPrePrepare();
        this.setState(Transaction.PREPARED_STATE);
        this.transactionStore.prepare(this.getTransactionId());
        return XAResource.XA_OK;
    default:
        this.illegalStateTransition("prepare");
        return XAResource.XA_RDONLY;
    }
}

From source file:com.alibaba.napoli.metamorphosis.client.transaction.TransactionContext.java

@Override
public int prepare(final Xid xid) throws XAException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Prepare: " + xid);
    }/*from w ww  . j a va  2  s. co  m*/

    XATransactionId x;

    // ???endprepare?associatedXid?null
    if (xid == null || this.equals(this.associatedXid, xid)) {
        throw new XAException(XAException.XAER_PROTO);
    } else {
        x = new XATransactionId(xid);
    }
    MetaStatLog.addStat(null, StatConstants.TX_PREPARE);

    final TransactionInfo info = new TransactionInfo(x, this.sessionId,
            TransactionInfo.TransactionType.PREPARE);

    final BooleanCommand response = this.syncSendXATxCommand(info);
    final int result = Integer.parseInt(response.getErrorMsg());
    if (XAResource.XA_RDONLY == result) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("XA_RDONLY from prepare: " + xid);
        }
    }
    return result;

}

From source file:it.doqui.index.ecmengine.business.personalization.multirepository.index.lucene.RepositoryAwareAbstractLuceneIndexerAndSearcherFactory.java

public int prepare(Xid xid) throws XAException {
    // TODO: Track state OK, ReadOnly, Exception (=> rolled back?)
    Map<String, LuceneIndexer> indexers = activeIndexersInGlobalTx.get(xid);

    if (indexers == null) {
        if (suspendedIndexersInGlobalTx.containsKey(xid)) {
            throw new XAException("Trying to commit indexes for a suspended transaction.");
        } else {// w  w w.j  a v  a2  s. c  om
            // nothing to do
            return XAResource.XA_OK;
        }
    }

    boolean isPrepared = true;
    boolean isModified = false;

    for (LuceneIndexer indexer : indexers.values()) {
        try {
            isModified |= indexer.isModified();
            indexer.prepare();
        } catch (IndexerException e) {
            isPrepared = false;
        }
    }

    if (isPrepared) {
        return (isModified) ? XAResource.XA_OK : XAResource.XA_RDONLY;
    } else {
        throw new XAException("Failed to prepare: requires rollback");
    }
}

From source file:it.doqui.index.ecmengine.business.personalization.multirepository.index.lucene.RepositoryAwareAbstractLuceneIndexerAndSearcherFactory.java

/**
 * Prepare the transaction TODO: Store prepare results
 *
 * @return - the tx code/*from  w w w  .jav a2 s .co m*/
 */
public int prepare() throws IndexerException {
    boolean isPrepared = true;
    boolean isModified = false;
    Map<String, LuceneIndexer> indexers = threadLocalIndexers.get();
    if (indexers != null) {
        for (LuceneIndexer indexer : indexers.values()) {
            try {
                isModified |= indexer.isModified();
                indexer.prepare();
            } catch (IndexerException e) {
                isPrepared = false;
                throw new IndexerException("Failed to prepare: requires rollback", e);
            }
        }
    }
    if (isPrepared) {
        if (isModified) {
            return XAResource.XA_OK;
        } else {
            return XAResource.XA_RDONLY;
        }
    } else {
        throw new IndexerException("Failed to prepare: requires rollback");
    }
}

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

public int prepare(Xid xid) throws XAException {
    // TODO: Track state OK, ReadOnly, Exception (=> rolled back?)
    Map<StoreRef, LuceneIndexer> indexers = activeIndexersInGlobalTx.get(xid);
    if (indexers == null) {
        if (suspendedIndexersInGlobalTx.containsKey(xid)) {
            throw new XAException("Trying to commit indexes for a suspended transaction.");
        } else {//from w  ww . j av a2 s  .  c o m
            // nothing to do
            return XAResource.XA_OK;
        }
    }
    boolean isPrepared = true;
    boolean isModified = false;
    for (LuceneIndexer indexer : indexers.values()) {
        try {
            isModified |= indexer.isModified();
            indexer.prepare();
        } catch (IndexerException e) {
            isPrepared = false;
        }
    }
    if (isPrepared) {
        if (isModified) {
            return XAResource.XA_OK;
        } else {
            return XAResource.XA_RDONLY;
        }
    } else {
        throw new XAException("Failed to prepare: requires rollback");
    }
}

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

/**
 * Prepare the transaction TODO: Store prepare results
 * /* www .  j a v  a 2s  .  co  m*/
 * @return - the tx code
 */
@SuppressWarnings("unchecked")
public int prepare() throws IndexerException {
    boolean isPrepared = true;
    boolean isModified = false;
    Map<StoreRef, LuceneIndexer> indexers = (Map<StoreRef, LuceneIndexer>) AlfrescoTransactionSupport
            .getResource(indexersKey);
    if (indexers != null) {
        for (LuceneIndexer indexer : indexers.values()) {
            try {
                isModified |= indexer.isModified();
                indexer.prepare();
            } catch (IndexerException e) {
                isPrepared = false;
                throw new IndexerException("Failed to prepare: requires rollback", e);
            }
        }
    }
    if (isPrepared) {
        if (isModified) {
            return XAResource.XA_OK;
        } else {
            return XAResource.XA_RDONLY;
        }
    } else {
        throw new IndexerException("Failed to prepare: requires rollback");
    }
}

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.
 * /*w  w w  .  j  a v a 2  s. c  om*/
 * @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.hadoop.hbase.client.transactional.JtaXAResource.java

public int prepare(final Xid xid) throws XAException {
    LOG.trace("prepare [" + xid.toString() + "] ");
    TransactionState state = xidToTransactionState.get(xid);
    int status;/*from  w  ww .  j a  va2s. c o m*/
    try {
        status = this.transactionManager.prepareCommit(state);
    } catch (CommitUnsuccessfulException e) {
        XAException xae = new XAException(XAException.XA_HEURRB);
        xae.initCause(e);
        throw xae;
    } catch (IOException e) {
        XAException xae = new XAException(XAException.XAER_RMERR);
        xae.initCause(e);
        throw xae;
    }

    switch (status) {
    case TransactionalReturn.COMMIT_OK:
        return XAResource.XA_OK;
    case TransactionalReturn.COMMIT_OK_READ_ONLY:
        return XAResource.XA_RDONLY;
    default:
        throw new XAException(XAException.XA_RBPROTO);
    }
}

From source file:org.bytesoft.bytejta.supports.resource.LocalXAResourceDescriptor.java

public int prepare(Xid arg0) throws XAException {
    if (this.delegate == null) {
        return XAResource.XA_RDONLY;
    }//  ww  w  . ja v  a 2 s . c  o  m
    return delegate.prepare(arg0);
}

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

private TransactionImpl reconstructTransaction(TransactionArchive archive) throws IllegalStateException {
    XidFactory xidFactory = this.beanFactory.getXidFactory();
    TransactionContext transactionContext = new TransactionContext();
    TransactionXid xid = (TransactionXid) archive.getXid();
    transactionContext.setXid(xidFactory.createGlobalXid(xid.getGlobalTransactionId()));
    transactionContext.setRecoveried(true);
    transactionContext.setCoordinator(archive.isCoordinator());
    transactionContext.setPropagatedBy(archive.getPropagatedBy());

    TransactionImpl transaction = new TransactionImpl(transactionContext);
    transaction.setBeanFactory(this.beanFactory);
    transaction.setTransactionStatus(archive.getStatus());

    List<XAResourceArchive> nativeResources = archive.getNativeResources();
    transaction.getNativeParticipantList().addAll(nativeResources);

    transaction.setParticipant(archive.getOptimizedResource());

    List<XAResourceArchive> remoteResources = archive.getRemoteResources();
    transaction.getRemoteParticipantList().addAll(remoteResources);

    List<XAResourceArchive> participants = transaction.getParticipantList();
    Map<String, XAResourceArchive> applicationMap = transaction.getApplicationMap();
    Map<String, XAResourceArchive> participantMap = transaction.getParticipantMap();
    if (archive.getOptimizedResource() != null) {
        participants.add(archive.getOptimizedResource());
    }/*from  w w w  .  j  a  va2  s.  co m*/
    participants.addAll(nativeResources);
    participants.addAll(remoteResources);

    for (int i = 0; i < participants.size(); i++) {
        XAResourceArchive element = participants.get(i);
        XAResourceDescriptor descriptor = element.getDescriptor();
        String identifier = StringUtils.trimToEmpty(descriptor.getIdentifier());

        if (RemoteResourceDescriptor.class.isInstance(descriptor)) {
            RemoteResourceDescriptor resourceDescriptor = (RemoteResourceDescriptor) descriptor;
            RemoteCoordinator remoteCoordinator = resourceDescriptor.getDelegate();
            applicationMap.put(remoteCoordinator.getApplication(), element);
        } // end-if (RemoteResourceDescriptor.class.isInstance(descriptor))

        participantMap.put(identifier, element);
    }

    transaction.recoverTransactionStrategy(archive.getTransactionStrategyType());

    if (archive.getVote() == XAResource.XA_RDONLY) {
        throw new IllegalStateException("Transaction has already been completed!");
    }

    return transaction;
}