Search in sources :

Example 6 with XaResource

use of org.neo4j.kernel.impl.transaction.xaframework.XaResource in project neo4j-mobile-android by neo4j-contrib.

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 (Xid xid : xids) {
                if (!recoveredXidsList.contains(xid)) {
                    log.fine("Tx-seq[" + seq + "][" + xid + "] not found in recovered xid list, " + "assuming already committed");
                    continue;
                }
                recoveredXidsList.remove(xid);
                Resource resource = new Resource(xid.getBranchQualifier());
                if (!resourceMap.containsKey(resource)) {
                    final TransactionFailureException ex = new TransactionFailureException("Couldn't find XAResource for " + xid);
                    throw logAndReturn("TM: recovery error", ex);
                }
                log.fine("Commiting tx seq[" + seq + "][" + xid + "] ... ");
                msgLog.logMessage("TM: Committing tx " + xid, true);
                resourceMap.get(resource).commit(xid, 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)) {
                final TransactionFailureException ex = new TransactionFailureException("Couldn't find XAResource for " + xid);
                throw logAndReturn("TM: recovery error", ex);
            }
            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);
        }
        // doesn't get lost.
        for (XAResource participant : MapUtil.reverse(resourceMap).keySet()) {
            xaResourceToDataSource(participant).rotateLogicalLog();
        }
    } catch (IOException e) {
        throw logAndReturn("TM: recovery failed", new TransactionFailureException("Recovery failed.", e));
    } catch (XAException e) {
        throw logAndReturn("TM: recovery failed", 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) IOException(java.io.IOException) LinkedList(java.util.LinkedList) Xid(javax.transaction.xa.Xid) XAResource(javax.transaction.xa.XAResource) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException)

Aggregations

XAResource (javax.transaction.xa.XAResource)6 TransactionFailureException (org.neo4j.graphdb.TransactionFailureException)6 XaResource (org.neo4j.kernel.impl.transaction.xaframework.XaResource)6 HashMap (java.util.HashMap)4 LinkedList (java.util.LinkedList)4 XAException (javax.transaction.xa.XAException)4 Xid (javax.transaction.xa.Xid)4 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 XaDataSource (org.neo4j.kernel.impl.transaction.xaframework.XaDataSource)2 IOException (java.io.IOException)1