Search in sources :

Example 6 with XAException

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

the class XaLogicalLog method applyTwoPhaseCommitEntry.

private void applyTwoPhaseCommitEntry(LogEntry.TwoPhaseCommit commit) throws IOException {
    int identifier = commit.getIdentifier();
    long txId = commit.getTxId();
    LogEntry.Start startEntry = xidIdentMap.get(identifier);
    if (startEntry == null) {
        throw new IOException("Unknown xid for identifier " + identifier);
    }
    Xid xid = startEntry.getXid();
    if (xid == null) {
        throw new IOException("Xid null for identifier " + identifier);
    }
    try {
        XaTransaction xaTx = xaRm.getXaTransaction(xid);
        xaTx.setCommitTxId(txId);
        xaRm.injectTwoPhaseCommit(xid);
        registerRecoveredTransaction(txId);
    } catch (XAException e) {
        throw new IOException(e);
    }
}
Also used : Xid(javax.transaction.xa.Xid) XAException(javax.transaction.xa.XAException) IOException(java.io.IOException)

Example 7 with XAException

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

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 8 with XAException

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

the class TestXaFramework method testCreateXaResource.

@Test
public void testCreateXaResource() throws Exception {
    Map<Object, Object> config = new HashMap<Object, Object>();
    config.put("store_dir", "target/var");
    config.put(LogBufferFactory.class, CommonFactories.defaultLogBufferFactory(config));
    xaDsMgr.registerDataSource("dummy_datasource", new DummyXaDataSource(config), UTF8.encode("DDDDDD"));
    XaDataSource xaDs = xaDsMgr.getXaDataSource("dummy_datasource");
    DummyXaConnection xaC = null;
    try {
        xaC = (DummyXaConnection) xaDs.getXaConnection();
        try {
            xaC.doStuff1();
            fail("Non enlisted resource should throw exception");
        } catch (XAException e) {
        // good
        }
        Xid xid = new XidImpl(new byte[0], new byte[0]);
        xaC.getXaResource().start(xid, XAResource.TMNOFLAGS);
        try {
            xaC.doStuff1();
            xaC.doStuff2();
        } catch (XAException e) {
            fail("Enlisted resource should not throw exception");
        }
        xaC.getXaResource().end(xid, XAResource.TMSUCCESS);
        xaC.getXaResource().prepare(xid);
        xaC.getXaResource().commit(xid, false);
    } finally {
        xaDsMgr.unregisterDataSource("dummy_datasource");
        if (xaC != null) {
            xaC.destroy();
        }
    }
    // cleanup dummy resource log
    File dir = new File(".");
    File[] files = dir.listFiles(new FilenameFilter() {

        public boolean accept(File dir, String fileName) {
            return fileName.startsWith(resourceFile());
        }
    });
    for (int i = 0; i < files.length; i++) {
        files[i].delete();
    }
}
Also used : XAException(javax.transaction.xa.XAException) HashMap(java.util.HashMap) XaDataSource(org.neo4j.kernel.impl.transaction.xaframework.XaDataSource) FilenameFilter(java.io.FilenameFilter) Xid(javax.transaction.xa.Xid) File(java.io.File) Test(org.junit.Test)

Example 9 with XAException

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

the class ReadOnlyTxManager method rollback.

public void rollback() throws IllegalStateException, SystemException {
    Thread thread = Thread.currentThread();
    ReadOnlyTransactionImpl tx = txThreadMap.get(thread);
    if (tx == null) {
        throw new IllegalStateException("Not in transaction");
    }
    if (tx.getStatus() == Status.STATUS_ACTIVE || tx.getStatus() == Status.STATUS_MARKED_ROLLBACK || tx.getStatus() == Status.STATUS_PREPARING) {
        tx.doBeforeCompletion();
        try {
            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 ---->");
            throw new SystemException("Unable to rollback " + " ---> error code for rollback: " + e.errorCode);
        }
        tx.doAfterCompletion();
        txThreadMap.remove(thread);
        tx.setStatus(Status.STATUS_NO_TRANSACTION);
    } else {
        throw new IllegalStateException("Tx status is: " + getTxStatusAsString(tx.getStatus()));
    }
}
Also used : XAException(javax.transaction.xa.XAException) SystemException(javax.transaction.SystemException)

Example 10 with XAException

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

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