Search in sources :

Example 11 with XAException

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

the class TxManager method rollback.

public void rollback() throws IllegalStateException, SystemException {
    if (!tmOk) {
        throw new SystemException("TM has encountered some problem, " + "please perform neccesary action (tx recovery/restart)");
    }
    Thread thread = Thread.currentThread();
    TransactionImpl tx = txThreadMap.get(thread);
    if (tx == null) {
        throw new IllegalStateException("Not in transaction");
    }
    boolean hasAnyLocks = false;
    try {
        hasAnyLocks = finishHook.hasAnyLocks(tx);
        if (tx.getStatus() == Status.STATUS_ACTIVE || tx.getStatus() == Status.STATUS_MARKED_ROLLBACK || tx.getStatus() == Status.STATUS_PREPARING) {
            tx.setStatus(Status.STATUS_MARKED_ROLLBACK);
            tx.doBeforeCompletion();
            // delist resources?
            try {
                rolledBackTxCount.incrementAndGet();
                tx.doRollback();
            } catch (XAException e) {
                e.printStackTrace();
                log.severe("Unable to rollback marked or active transaction. " + "Some resources may be commited others not. " + "Neo4j kernel should be SHUTDOWN for " + "resource maintance and transaction recovery ---->");
                setTmNotOk();
                throw new SystemException("Unable to rollback " + " ---> error code for rollback: " + e.errorCode);
            }
            tx.doAfterCompletion();
            txThreadMap.remove(thread);
            try {
                if (tx.isGlobalStartRecordWritten()) {
                    getTxLog().txDone(tx.getGlobalId());
                }
            } catch (IOException e) {
                e.printStackTrace();
                log.severe("Error writing transaction log");
                setTmNotOk();
                throw new SystemException("TM encountered a problem, " + " error writing transaction log," + e);
            }
            tx.setStatus(Status.STATUS_NO_TRANSACTION);
        } else {
            throw new IllegalStateException("Tx status is: " + getTxStatusAsString(tx.getStatus()));
        }
    } finally {
        if (hasAnyLocks) {
            finishHook.finishTransaction(tx.getEventIdentifier());
        }
    }
}
Also used : XAException(javax.transaction.xa.XAException) SystemException(javax.transaction.SystemException) IOException(java.io.IOException)

Example 12 with XAException

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

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) {
                    log.log(Level.SEVERE, "Error writing transaction log", e);
                    txManager.setTmNotOk(e);
                    throw Exceptions.withCause(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) {
                addResourceToList(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);
                addResourceToList(xid, xaRes);
                xaRes.start(xid, XAResource.TMNOFLAGS);
                try {
                    txManager.getTxLog().addBranch(globalId, branchId);
                } catch (IOException e) {
                    log.log(Level.SEVERE, "Error writing transaction log", e);
                    txManager.setTmNotOk(e);
                    throw Exceptions.withCause(new SystemException("TM encountered a problem, " + " error writing transaction log"), e);
                }
            }
            return true;
        } catch (XAException e) {
            log.log(Level.SEVERE, "Unable to enlist resource[" + xaRes + "]", e);
            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)

Example 13 with XAException

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

the class TxManager method commit.

private void commit(Thread thread, TransactionImpl tx) throws SystemException, HeuristicMixedException, HeuristicRollbackException {
    // mark as commit in log done TxImpl.doCommit()
    Throwable commitFailureCause = null;
    int xaErrorCode = -1;
    if (tx.getResourceCount() == 0) {
        tx.setStatus(Status.STATUS_COMMITTED);
    } else {
        try {
            tx.doCommit();
        } catch (XAException e) {
            xaErrorCode = e.errorCode;
            log.log(Level.SEVERE, "Commit failed, status=" + getTxStatusAsString(tx.getStatus()) + ", errorCode=" + xaErrorCode, e);
            if (tx.getStatus() == Status.STATUS_COMMITTED) {
                // this should never be
                setTmNotOk(e);
                throw logAndReturn("TM error tx commit", new TransactionFailureException("commit threw exception but status is committed?", e));
            }
        } catch (Throwable t) {
            log.log(Level.SEVERE, "Commit failed", t);
            commitFailureCause = t;
        }
    }
    if (tx.getStatus() != Status.STATUS_COMMITTED) {
        try {
            tx.doRollback();
        } catch (Throwable e) {
            log.log(Level.SEVERE, "Unable to rollback transaction. " + "Some resources may be commited others not. " + "Neo4j kernel should be SHUTDOWN for " + "resource maintance and transaction recovery ---->", e);
            setTmNotOk(e);
            String commitError;
            if (commitFailureCause != null) {
                commitError = "error in commit: " + commitFailureCause;
            } else {
                commitError = "error code in commit: " + xaErrorCode;
            }
            String rollbackErrorCode = "Uknown error code";
            if (e instanceof XAException) {
                rollbackErrorCode = Integer.toString(((XAException) e).errorCode);
            }
            throw logAndReturn("TM error tx commit", Exceptions.withCause(new HeuristicMixedException("Unable to rollback ---> " + commitError + " ---> error code for rollback: " + rollbackErrorCode), e));
        }
        tx.doAfterCompletion();
        txThreadMap.remove(thread);
        try {
            if (tx.isGlobalStartRecordWritten()) {
                getTxLog().txDone(tx.getGlobalId());
            }
        } catch (IOException e) {
            log.log(Level.SEVERE, "Error writing transaction log", e);
            setTmNotOk(e);
            throw logAndReturn("TM error tx commit", Exceptions.withCause(new SystemException("TM encountered a problem, " + " error writing transaction log"), e));
        }
        tx.setStatus(Status.STATUS_NO_TRANSACTION);
        if (commitFailureCause == null) {
            throw logAndReturn("TM error tx commit", new HeuristicRollbackException("Failed to commit, transaction rolledback ---> " + "error code was: " + xaErrorCode));
        } else {
            throw logAndReturn("TM error tx commit", Exceptions.withCause(new HeuristicRollbackException("Failed to commit, transaction rolledback ---> " + commitFailureCause), commitFailureCause));
        }
    }
    tx.doAfterCompletion();
    txThreadMap.remove(thread);
    try {
        if (tx.isGlobalStartRecordWritten()) {
            getTxLog().txDone(tx.getGlobalId());
        }
    } catch (IOException e) {
        log.log(Level.SEVERE, "Error writing transaction log", e);
        setTmNotOk(e);
        throw logAndReturn("TM error tx commit", Exceptions.withCause(new SystemException("TM encountered a problem, " + " error writing transaction log"), e));
    }
    tx.setStatus(Status.STATUS_NO_TRANSACTION);
}
Also used : TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) XAException(javax.transaction.xa.XAException) SystemException(javax.transaction.SystemException) HeuristicMixedException(javax.transaction.HeuristicMixedException) IOException(java.io.IOException)

Example 14 with XAException

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

the class TxManager method rollbackCommit.

private void rollbackCommit(Thread thread, TransactionImpl tx) throws HeuristicMixedException, RollbackException, SystemException {
    try {
        tx.doRollback();
    } catch (XAException e) {
        log.log(Level.SEVERE, "Unable to rollback marked transaction. " + "Some resources may be commited others not. " + "Neo4j kernel should be SHUTDOWN for " + "resource maintance and transaction recovery ---->", e);
        setTmNotOk(e);
        throw logAndReturn("TM error tx rollback commit", Exceptions.withCause(new HeuristicMixedException("Unable to rollback " + " ---> error code for rollback: " + e.errorCode), e));
    }
    tx.doAfterCompletion();
    txThreadMap.remove(thread);
    try {
        if (tx.isGlobalStartRecordWritten()) {
            getTxLog().txDone(tx.getGlobalId());
        }
    } catch (IOException e) {
        log.log(Level.SEVERE, "Error writing transaction log", e);
        setTmNotOk(e);
        throw logAndReturn("TM error tx rollback commit", Exceptions.withCause(new SystemException("TM encountered a problem, " + " error writing transaction log"), e));
    }
    tx.setStatus(Status.STATUS_NO_TRANSACTION);
    throw new RollbackException("Failed to commit, transaction rolledback");
}
Also used : XAException(javax.transaction.xa.XAException) SystemException(javax.transaction.SystemException) HeuristicMixedException(javax.transaction.HeuristicMixedException) IOException(java.io.IOException) RollbackException(javax.transaction.RollbackException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException)

Example 15 with XAException

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

the class TxManager method rollback.

public void rollback() throws IllegalStateException, SystemException {
    assertTmOk("tx rollback");
    Thread thread = Thread.currentThread();
    TransactionImpl tx = txThreadMap.get(thread);
    if (tx == null) {
        throw new IllegalStateException("Not in transaction");
    }
    boolean hasAnyLocks = false;
    try {
        hasAnyLocks = finishHook.hasAnyLocks(tx);
        if (tx.getStatus() == Status.STATUS_ACTIVE || tx.getStatus() == Status.STATUS_MARKED_ROLLBACK || tx.getStatus() == Status.STATUS_PREPARING) {
            tx.setStatus(Status.STATUS_MARKED_ROLLBACK);
            tx.doBeforeCompletion();
            // delist resources?
            try {
                rolledBackTxCount.incrementAndGet();
                tx.doRollback();
            } catch (XAException e) {
                log.log(Level.SEVERE, "Unable to rollback marked or active transaction. " + "Some resources may be commited others not. " + "Neo4j kernel should be SHUTDOWN for " + "resource maintance and transaction recovery ---->", e);
                setTmNotOk(e);
                throw logAndReturn("TM error tx rollback", Exceptions.withCause(new SystemException("Unable to rollback " + " ---> error code for rollback: " + e.errorCode), e));
            }
            tx.doAfterCompletion();
            txThreadMap.remove(thread);
            try {
                if (tx.isGlobalStartRecordWritten()) {
                    getTxLog().txDone(tx.getGlobalId());
                }
            } catch (IOException e) {
                log.log(Level.SEVERE, "Error writing transaction log", e);
                setTmNotOk(e);
                throw logAndReturn("TM error tx rollback", Exceptions.withCause(new SystemException("TM encountered a problem, " + " error writing transaction log"), e));
            }
            tx.setStatus(Status.STATUS_NO_TRANSACTION);
        } else {
            throw new IllegalStateException("Tx status is: " + getTxStatusAsString(tx.getStatus()));
        }
    } finally {
        if (hasAnyLocks) {
            finishHook.finishTransaction(tx.getEventIdentifier(), false);
        }
    }
}
Also used : XAException(javax.transaction.xa.XAException) SystemException(javax.transaction.SystemException) IOException(java.io.IOException)

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