Search in sources :

Example 51 with XAException

use of javax.transaction.xa.XAException 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 52 with XAException

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

the class XaLogicalLog method applyTransaction.

public synchronized void applyTransaction(ReadableByteChannel byteChannel) throws IOException {
    //        System.out.println( "applyFullTx#start @ pos: " + writeBuffer.getFileChannelPosition() );
    long logEntriesFound = 0;
    scanIsComplete = false;
    LogApplier logApplier = new LogApplier(byteChannel);
    int xidIdent = getNextIdentifier();
    long startEntryPosition = writeBuffer.getFileChannelPosition();
    boolean successfullyApplied = false;
    try {
        while (logApplier.readAndWriteAndApplyEntry(xidIdent)) {
            logEntriesFound++;
        }
        successfullyApplied = true;
    } finally {
        if (!successfullyApplied && logApplier.startEntry != null) {
            // Unmap this identifier if tx not applied correctly
            xidIdentMap.remove(xidIdent);
            try {
                xaRm.forget(logApplier.startEntry.getXid());
            } catch (XAException e) {
                throw new IOException(e);
            }
        }
    }
    byteChannel.close();
    scanIsComplete = true;
    LogEntry.Start startEntry = logApplier.startEntry;
    if (startEntry == null) {
        throw new IOException("Unable to find start entry");
    }
    startEntry.setStartPosition(startEntryPosition);
    cacheTxStartPosition(logApplier.commitEntry.getTxId(), logApplier.commitEntry.getMasterId(), startEntry);
    //        System.out.println( "applyFullTx#end @ pos: " + writeBuffer.getFileChannelPosition() );
    checkLogRotation();
}
Also used : XAException(javax.transaction.xa.XAException) IOException(java.io.IOException)

Example 53 with XAException

use of javax.transaction.xa.XAException 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)

Example 54 with XAException

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

the class ReadOnlyTxManager method rollbackCommit.

private void rollbackCommit(Thread thread, ReadOnlyTransactionImpl tx) throws HeuristicMixedException, RollbackException {
    try {
        tx.doRollback();
    } catch (XAException e) {
        e.printStackTrace();
        log.severe("Unable to rollback marked transaction. " + "Some resources may be commited others not. " + "Neo4j kernel should be SHUTDOWN for " + "resource maintance and transaction recovery ---->");
        throw new HeuristicMixedException("Unable to rollback " + " ---> error code for rollback: " + e.errorCode);
    }
    tx.doAfterCompletion();
    txThreadMap.remove(thread);
    tx.setStatus(Status.STATUS_NO_TRANSACTION);
    throw new RollbackException("Failed to commit, transaction rolledback");
}
Also used : XAException(javax.transaction.xa.XAException) HeuristicMixedException(javax.transaction.HeuristicMixedException) RollbackException(javax.transaction.RollbackException)

Example 55 with XAException

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

the class TransactionImpl method enlistResource.

public synchronized boolean enlistResource(XAResource xaRes) throws RollbackException, IllegalStateException, SystemException {
    if (xaRes == null) {
        throw new IllegalArgumentException("Null xa resource");
    }
    if (status == Status.STATUS_ACTIVE || status == Status.STATUS_PREPARING) {
        try {
            if (resourceList.size() == 0) {
                if (!globalStartRecordWritten) {
                    txManager.writeStartRecord(globalId);
                    globalStartRecordWritten = true;
                }
                // 
                byte[] branchId = txManager.getBranchId(xaRes);
                Xid xid = new XidImpl(globalId, branchId);
                resourceList.add(new ResourceElement(xid, xaRes));
                xaRes.start(xid, XAResource.TMNOFLAGS);
                try {
                    txManager.getTxLog().addBranch(globalId, branchId);
                } catch (IOException e) {
                    e.printStackTrace();
                    log.severe("Error writing transaction log");
                    txManager.setTmNotOk();
                    throw new SystemException("TM encountered a problem, " + " error writing transaction log," + e);
                }
                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);
                try {
                    txManager.getTxLog().addBranch(globalId, branchId);
                } catch (IOException e) {
                    e.printStackTrace();
                    log.severe("Error writing transaction log");
                    txManager.setTmNotOk();
                    throw new SystemException("TM encountered a problem, " + " error writing transaction log," + e);
                }
            }
            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) SystemException(javax.transaction.SystemException) IOException(java.io.IOException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) RollbackException(javax.transaction.RollbackException)

Aggregations

XAException (javax.transaction.xa.XAException)68 IOException (java.io.IOException)20 Xid (javax.transaction.xa.Xid)19 SystemException (javax.transaction.SystemException)14 TransactionContext (com.hazelcast.transaction.TransactionContext)12 RollbackException (javax.transaction.RollbackException)8 XAResource (javax.transaction.xa.XAResource)8 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)7 HeuristicMixedException (javax.transaction.HeuristicMixedException)6 Test (org.junit.Test)6 TransactionFailureException (org.neo4j.graphdb.TransactionFailureException)6 ParallelTest (com.hazelcast.test.annotation.ParallelTest)5 QuickTest (com.hazelcast.test.annotation.QuickTest)5 HashMap (java.util.HashMap)5 XaResource (org.neo4j.kernel.impl.transaction.xaframework.XaResource)4 ArrayList (java.util.ArrayList)3 InvalidRecordException (org.neo4j.kernel.impl.nioneo.store.InvalidRecordException)3 NodeRecord (org.neo4j.kernel.impl.nioneo.store.NodeRecord)3 PropertyIndexRecord (org.neo4j.kernel.impl.nioneo.store.PropertyIndexRecord)3 PropertyRecord (org.neo4j.kernel.impl.nioneo.store.PropertyRecord)3