Search in sources :

Example 51 with Xid

use of javax.transaction.xa.Xid in project graphdb by neo4j-attic.

the class XaLogicalLog method applyStartEntry.

private void applyStartEntry(LogEntry.Start entry) throws IOException {
    int identifier = entry.getIdentifier();
    if (identifier >= nextIdentifier) {
        nextIdentifier = (identifier + 1);
    }
    // re-create the transaction
    Xid xid = entry.getXid();
    xidIdentMap.put(identifier, entry);
    XaTransaction xaTx = xaTf.create(identifier);
    xaTx.setRecovered();
    recoveredTxMap.put(identifier, xaTx);
    xaRm.injectStart(xid, xaTx);
    // force to make sure done record is there if 2PC tx and global log
    // marks tx as committed
    fileChannel.force(false);
}
Also used : Xid(javax.transaction.xa.Xid)

Example 52 with Xid

use of javax.transaction.xa.Xid in project graphdb by neo4j-attic.

the class XaResourceManager method fail.

synchronized void fail(XAResource xaResource, Xid xid) throws XAException {
    if (xidMap.get(xid) == null) {
        throw new XAException("Unknown xid[" + xid + "]");
    }
    Xid xidEntry = xaResourceMap.remove(xaResource);
    if (xidEntry == null) {
        throw new XAException("Resource[" + xaResource + "] not enlisted");
    }
    XidStatus status = xidMap.get(xid);
    status.getTransactionStatus().markAsRollback();
}
Also used : Xid(javax.transaction.xa.Xid) XAException(javax.transaction.xa.XAException)

Example 53 with Xid

use of javax.transaction.xa.Xid in project neo4j-mobile-android by neo4j-contrib.

the class XaLogicalLog method applyPrepareEntry.

private void applyPrepareEntry(LogEntry.Prepare prepareEntry) throws IOException {
    // get the tx identifier
    int identifier = prepareEntry.getIdentifier();
    LogEntry.Start entry = xidIdentMap.get(identifier);
    if (entry == null) {
        throw new IOException("Unknown xid for identifier " + identifier);
    }
    Xid xid = entry.getXid();
    if (xaRm.injectPrepare(xid)) {
        // read only we can remove
        xidIdentMap.remove(identifier);
        recoveredTxMap.remove(identifier);
    }
}
Also used : Xid(javax.transaction.xa.Xid) IOException(java.io.IOException)

Example 54 with Xid

use of javax.transaction.xa.Xid in project neo4j-mobile-android by neo4j-contrib.

the class XaLogicalLog method applyTransactionWithoutTxId.

public synchronized void applyTransactionWithoutTxId(ReadableByteChannel byteChannel, long nextTxId, int masterId) throws IOException {
    if (nextTxId != (xaTf.getLastCommittedTx() + 1)) {
        throw new IllegalStateException("Tried to apply tx " + nextTxId + " but expected transaction " + (xaTf.getCurrentVersion() + 1));
    }
    logRecoveryMessage("applyTxWithoutTxId log version: " + logVersion + ", committing tx=" + nextTxId + ") @ pos " + writeBuffer.getFileChannelPosition());
    long logEntriesFound = 0;
    scanIsComplete = false;
    LogApplier logApplier = new LogApplier(byteChannel);
    int xidIdent = getNextIdentifier();
    long startEntryPosition = writeBuffer.getFileChannelPosition();
    while (logApplier.readAndWriteAndApplyEntry(xidIdent)) {
        logEntriesFound++;
    }
    byteChannel.close();
    LogEntry.Start startEntry = logApplier.startEntry;
    if (startEntry == null) {
        throw new IOException("Unable to find start entry");
    }
    startEntry.setStartPosition(startEntryPosition);
    //        System.out.println( "applyTxWithoutTxId#before 1PC @ pos: " + writeBuffer.getFileChannelPosition() );
    LogEntry.OnePhaseCommit commit = new LogEntry.OnePhaseCommit(xidIdent, nextTxId, masterId);
    LogIoUtils.writeLogEntry(commit, writeBuffer);
    // need to manually force since xaRm.commit will not do it (transaction marked as recovered)
    writeBuffer.force();
    Xid xid = startEntry.getXid();
    try {
        XaTransaction xaTx = xaRm.getXaTransaction(xid);
        xaTx.setCommitTxId(nextTxId);
        xaRm.commit(xid, true);
        LogEntry doneEntry = new LogEntry.Done(startEntry.getIdentifier());
        LogIoUtils.writeLogEntry(doneEntry, writeBuffer);
        xidIdentMap.remove(startEntry.getIdentifier());
        recoveredTxMap.remove(startEntry.getIdentifier());
        cacheTxStartPosition(nextTxId, masterId, startEntry);
    } catch (XAException e) {
        throw new IOException(e);
    }
    //        LogEntry.Done done = new LogEntry.Done( entry.getIdentifier() );
    //        LogIoUtils.writeLogEntry( done, writeBuffer );
    // xaTf.setLastCommittedTx( nextTxId ); // done in doCommit
    scanIsComplete = true;
    //        log.info( "Tx[" + nextTxId + "] " + " applied successfully." );
    logRecoveryMessage("Applied external tx and generated tx id=" + nextTxId);
    checkLogRotation();
//        System.out.println( "applyTxWithoutTxId#end @ pos: " + writeBuffer.getFileChannelPosition() );
}
Also used : XAException(javax.transaction.xa.XAException) IOException(java.io.IOException) Xid(javax.transaction.xa.Xid)

Example 55 with Xid

use of javax.transaction.xa.Xid in project graphdb by neo4j-attic.

the class ReadOnlyTransactionImpl method enlistResource.

public synchronized boolean enlistResource(XAResource xaRes) throws RollbackException, IllegalStateException {
    if (xaRes == null) {
        throw new IllegalArgumentException("Null xa resource");
    }
    if (status == Status.STATUS_ACTIVE || status == Status.STATUS_PREPARING) {
        try {
            if (resourceList.size() == 0) {
                // 
                byte[] branchId = txManager.getBranchId(xaRes);
                Xid xid = new XidImpl(globalId, branchId);
                resourceList.add(new ResourceElement(xid, xaRes));
                xaRes.start(xid, XAResource.TMNOFLAGS);
                return true;
            }
            Xid sameRmXid = null;
            Iterator<ResourceElement> itr = resourceList.iterator();
            while (itr.hasNext()) {
                ResourceElement re = itr.next();
                if (sameRmXid == null && re.getResource().isSameRM(xaRes)) {
                    sameRmXid = re.getXid();
                }
                if (xaRes == re.getResource()) {
                    if (re.getStatus() == RS_SUSPENDED) {
                        xaRes.start(re.getXid(), XAResource.TMRESUME);
                    } else {
                        // either enlisted or delisted
                        // is TMJOIN correct then?
                        xaRes.start(re.getXid(), XAResource.TMJOIN);
                    }
                    re.setStatus(RS_ENLISTED);
                    return true;
                }
            }
            if (// should we join?
            sameRmXid != null) {
                resourceList.add(new ResourceElement(sameRmXid, xaRes));
                xaRes.start(sameRmXid, XAResource.TMJOIN);
            } else // new branch
            {
                // ResourceElement re = resourceList.getFirst();
                byte[] branchId = txManager.getBranchId(xaRes);
                Xid xid = new XidImpl(globalId, branchId);
                resourceList.add(new ResourceElement(xid, xaRes));
                xaRes.start(xid, XAResource.TMNOFLAGS);
            }
            return true;
        } catch (XAException e) {
            e.printStackTrace();
            log.severe("Unable to enlist resource[" + xaRes + "]");
            status = Status.STATUS_MARKED_ROLLBACK;
            return false;
        }
    } else if (status == Status.STATUS_ROLLING_BACK || status == Status.STATUS_ROLLEDBACK || status == Status.STATUS_MARKED_ROLLBACK) {
        throw new RollbackException("Tx status is: " + txManager.getTxStatusAsString(status));
    }
    throw new IllegalStateException("Tx status is: " + txManager.getTxStatusAsString(status));
}
Also used : Xid(javax.transaction.xa.Xid) XAException(javax.transaction.xa.XAException) RollbackException(javax.transaction.RollbackException)

Aggregations

Xid (javax.transaction.xa.Xid)82 Test (org.junit.Test)35 XAException (javax.transaction.xa.XAException)20 IOException (java.io.IOException)16 XAResource (javax.transaction.xa.XAResource)14 UnitTest (nl.topicus.jdbc.test.category.UnitTest)11 XidImpl (org.neo4j.kernel.impl.transaction.XidImpl)11 LinkedList (java.util.LinkedList)10 InOrder (org.mockito.InOrder)6 HashMap (java.util.HashMap)5 RecoveredXid (nl.topicus.jdbc.xa.RecoveredXid)5 RelationshipType (org.neo4j.graphdb.RelationshipType)5 HazelcastXAResource (com.hazelcast.transaction.HazelcastXAResource)4 ArrayList (java.util.ArrayList)4 RollbackException (javax.transaction.RollbackException)4 SystemException (javax.transaction.SystemException)4 TransactionFailureException (org.neo4j.graphdb.TransactionFailureException)4 XaResource (org.neo4j.kernel.impl.transaction.xaframework.XaResource)4 TransactionContext (com.hazelcast.transaction.TransactionContext)3 SerializableXID (com.hazelcast.transaction.impl.xa.SerializableXID)3