Search in sources :

Example 26 with TransactionFailureException

use of org.neo4j.graphdb.TransactionFailureException in project graphdb by neo4j-attic.

the class TestUpgradeStore method makeSureStoreCantBeUpgradedIfNotExplicitlyToldTo2.

@Test
public void makeSureStoreCantBeUpgradedIfNotExplicitlyToldTo2() throws Exception {
    String path = path(12);
    new EmbeddedGraphDatabase(path).shutdown();
    setOlderNeoStoreVersion(path);
    try {
        new EmbeddedGraphDatabase(path, stringMap(ALLOW_STORE_UPGRADE, "false"));
        fail("Shouldn't be able to upgrade if not told to");
    } catch (TransactionFailureException e) {
        if (!(e.getCause() instanceof IllegalStoreVersionException)) {
            throw e;
        }
    }
}
Also used : EmbeddedGraphDatabase(org.neo4j.kernel.EmbeddedGraphDatabase) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) Test(org.junit.Test)

Example 27 with TransactionFailureException

use of org.neo4j.graphdb.TransactionFailureException in project graphdb by neo4j-attic.

the class TxManager method recover.

private void recover(Iterator<List<TxLog.Record>> danglingRecordList) {
    msgLog.logMessage("TM non resolved transactions found in " + txLog.getName(), true);
    try {
        // contains NonCompletedTransaction that needs to be committed
        List<NonCompletedTransaction> commitList = new ArrayList<NonCompletedTransaction>();
        // contains Xids that should be rolledback
        List<Xid> rollbackList = new LinkedList<Xid>();
        // key = Resource(branchId) value = XAResource
        Map<Resource, XAResource> resourceMap = new HashMap<Resource, XAResource>();
        buildRecoveryInfo(commitList, rollbackList, resourceMap, danglingRecordList);
        // invoke recover on all xa resources found
        Iterator<Resource> resourceItr = resourceMap.keySet().iterator();
        List<Xid> recoveredXidsList = new LinkedList<Xid>();
        while (resourceItr.hasNext()) {
            XAResource xaRes = resourceMap.get(resourceItr.next());
            Xid[] xids = xaRes.recover(XAResource.TMNOFLAGS);
            for (int i = 0; i < xids.length; i++) {
                if (XidImpl.isThisTm(xids[i].getGlobalTransactionId())) {
                    // linear search
                    if (rollbackList.contains(xids[i])) {
                        log.fine("Found pre commit " + xids[i] + " rolling back ... ");
                        msgLog.logMessage("TM: Found pre commit " + xids[i] + " rolling back ... ", true);
                        rollbackList.remove(xids[i]);
                        xaRes.rollback(xids[i]);
                    } else {
                        recoveredXidsList.add(xids[i]);
                    }
                } else {
                    log.warning("Unknown xid: " + xids[i]);
                }
            }
        }
        // sort the commit list after sequence number
        Collections.sort(commitList, new Comparator<NonCompletedTransaction>() {

            public int compare(NonCompletedTransaction r1, NonCompletedTransaction r2) {
                return r1.getSequenceNumber() - r2.getSequenceNumber();
            }
        });
        // go through and commit
        Iterator<NonCompletedTransaction> commitItr = commitList.iterator();
        while (commitItr.hasNext()) {
            NonCompletedTransaction nct = commitItr.next();
            int seq = nct.getSequenceNumber();
            Xid[] xids = nct.getXids();
            log.fine("Marked as commit tx-seq[" + seq + "] branch length: " + xids.length);
            for (int i = 0; i < xids.length; i++) {
                if (!recoveredXidsList.contains(xids[i])) {
                    log.fine("Tx-seq[" + seq + "][" + xids[i] + "] not found in recovered xid list, " + "assuming already committed");
                    continue;
                }
                recoveredXidsList.remove(xids[i]);
                Resource resource = new Resource(xids[i].getBranchQualifier());
                if (!resourceMap.containsKey(resource)) {
                    throw new TransactionFailureException("Couldn't find XAResource for " + xids[i]);
                }
                log.fine("Commiting tx seq[" + seq + "][" + xids[i] + "] ... ");
                msgLog.logMessage("TM: Committing tx " + xids[i], true);
                resourceMap.get(resource).commit(xids[i], false);
            }
        }
        // rollback the rest
        Iterator<Xid> rollbackItr = recoveredXidsList.iterator();
        while (rollbackItr.hasNext()) {
            Xid xid = rollbackItr.next();
            Resource resource = new Resource(xid.getBranchQualifier());
            if (!resourceMap.containsKey(resource)) {
                throw new TransactionFailureException("Couldn't find XAResource for " + xid);
            }
            log.fine("Rollback " + xid + " ... ");
            msgLog.logMessage("TM: no match found for " + xid + " removing", true);
            resourceMap.get(resource).rollback(xid);
        }
        if (rollbackList.size() > 0) {
            log.fine("TxLog contained unresolved " + "xids that needed rollback. They couldn't be matched to " + "any of the XAResources recover list. " + "Assuming " + rollbackList.size() + " transactions already rolled back.");
            msgLog.logMessage("TM: no match found for in total " + rollbackList.size() + " transaction that should have been rolled back", true);
        }
    } catch (XAException e) {
        throw new TransactionFailureException("Recovery failed." + e);
    }
}
Also used : XAException(javax.transaction.xa.XAException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) XAResource(javax.transaction.xa.XAResource) XaResource(org.neo4j.kernel.impl.transaction.xaframework.XaResource) LinkedList(java.util.LinkedList) Xid(javax.transaction.xa.Xid) XAResource(javax.transaction.xa.XAResource) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException)

Example 28 with TransactionFailureException

use of org.neo4j.graphdb.TransactionFailureException in project graphdb by neo4j-attic.

the class TxManager method buildRecoveryInfo.

private void buildRecoveryInfo(List<NonCompletedTransaction> commitList, List<Xid> rollbackList, Map<Resource, XAResource> resourceMap, Iterator<List<TxLog.Record>> danglingRecordList) {
    while (danglingRecordList.hasNext()) {
        Iterator<TxLog.Record> dListItr = danglingRecordList.next().iterator();
        TxLog.Record startRecord = dListItr.next();
        if (startRecord.getType() != TxLog.TX_START) {
            throw new TransactionFailureException("First record not a start record, type=" + startRecord.getType());
        }
        // get branches & commit status
        HashSet<Resource> branchSet = new HashSet<Resource>();
        int markedCommit = -1;
        while (dListItr.hasNext()) {
            TxLog.Record record = dListItr.next();
            if (record.getType() == TxLog.BRANCH_ADD) {
                if (markedCommit != -1) {
                    throw new TransactionFailureException("Already marked commit " + startRecord);
                }
                branchSet.add(new Resource(record.getBranchId()));
            } else if (record.getType() == TxLog.MARK_COMMIT) {
                if (markedCommit != -1) {
                    throw new TransactionFailureException("Already marked commit " + startRecord);
                }
                markedCommit = record.getSequenceNumber();
            } else {
                throw new TransactionFailureException("Illegal record type[" + record.getType() + "]");
            }
        }
        Iterator<Resource> resourceItr = branchSet.iterator();
        List<Xid> xids = new LinkedList<Xid>();
        while (resourceItr.hasNext()) {
            Resource resource = resourceItr.next();
            if (!resourceMap.containsKey(resource)) {
                resourceMap.put(resource, getXaResource(resource.getResourceId()));
            }
            xids.add(new XidImpl(startRecord.getGlobalId(), resource.getResourceId()));
        }
        if (// this xid needs to be committed
        markedCommit != -1) {
            commitList.add(new NonCompletedTransaction(markedCommit, xids));
        } else {
            rollbackList.addAll(xids);
        }
    }
}
Also used : XAResource(javax.transaction.xa.XAResource) XaResource(org.neo4j.kernel.impl.transaction.xaframework.XaResource) LinkedList(java.util.LinkedList) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) Xid(javax.transaction.xa.Xid) HashSet(java.util.HashSet)

Example 29 with TransactionFailureException

use of org.neo4j.graphdb.TransactionFailureException in project graphdb by neo4j-attic.

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;
            e.printStackTrace();
            log.severe("Commit failed, status=" + getTxStatusAsString(tx.getStatus()) + ", errorCode=" + xaErrorCode);
            if (tx.getStatus() == Status.STATUS_COMMITTED) {
                // this should never be
                setTmNotOk();
                throw new TransactionFailureException("commit threw exception but status is committed?", e);
            }
        } catch (Throwable t) {
            t.printStackTrace();
            commitFailureCause = t;
        }
    }
    if (tx.getStatus() != Status.STATUS_COMMITTED) {
        try {
            tx.doRollback();
        } catch (XAException e) {
            e.printStackTrace();
            log.severe("Unable to rollback transaction. " + "Some resources may be commited others not. " + "Neo4j kernel should be SHUTDOWN for " + "resource maintance and transaction recovery ---->");
            setTmNotOk();
            if (commitFailureCause != null) {
                commitFailureCause.printStackTrace();
            }
            throw new HeuristicMixedException("Unable to rollback ---> error code in commit: " + xaErrorCode + " ---> 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);
        if (commitFailureCause == null) {
            throw new HeuristicRollbackException("Failed to commit, transaction rolledback ---> " + "error code was: " + xaErrorCode);
        } else {
            throw new HeuristicRollbackException("Failed to commit, transaction rolledback ---> " + commitFailureCause);
        }
    }
    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);
}
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 30 with TransactionFailureException

use of org.neo4j.graphdb.TransactionFailureException in project graphdb by neo4j-attic.

the class PersistenceManager method delistResourcesForTransaction.

void delistResourcesForTransaction() throws NotInTransactionException {
    Transaction tx = this.getCurrentTransaction();
    if (tx == null) {
        throw new NotInTransactionException();
    }
    ResourceConnection con = txConnectionMap.get(tx);
    if (con != null) {
        try {
            tx.delistResource(con.getXAResource(), XAResource.TMSUCCESS);
        } catch (SystemException e) {
            throw new TransactionFailureException("Failed to delist resource '" + con + "' from current transaction.", e);
        }
    }
}
Also used : TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) NotInTransactionException(org.neo4j.graphdb.NotInTransactionException)

Aggregations

TransactionFailureException (org.neo4j.graphdb.TransactionFailureException)56 Transaction (org.neo4j.graphdb.Transaction)16 NotFoundException (org.neo4j.graphdb.NotFoundException)9 Test (org.junit.Test)7 ConstraintViolationException (org.neo4j.graphdb.ConstraintViolationException)7 Node (org.neo4j.graphdb.Node)7 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)7 LinkedList (java.util.LinkedList)6 SystemException (javax.transaction.SystemException)6 XAException (javax.transaction.xa.XAException)6 XAResource (javax.transaction.xa.XAResource)6 KernelException (org.neo4j.exceptions.KernelException)6 EntityNotFoundException (org.neo4j.internal.kernel.api.exceptions.EntityNotFoundException)6 InvalidTransactionTypeKernelException (org.neo4j.internal.kernel.api.exceptions.InvalidTransactionTypeKernelException)6 PropertyKeyIdNotFoundKernelException (org.neo4j.internal.kernel.api.exceptions.PropertyKeyIdNotFoundKernelException)6 IllegalTokenNameException (org.neo4j.internal.kernel.api.exceptions.schema.IllegalTokenNameException)6 TokenCapacityExceededKernelException (org.neo4j.internal.kernel.api.exceptions.schema.TokenCapacityExceededKernelException)6 XaDataSource (org.neo4j.kernel.impl.transaction.xaframework.XaDataSource)6 XaResource (org.neo4j.kernel.impl.transaction.xaframework.XaResource)6 IOException (java.io.IOException)5