Search in sources :

Example 56 with Xid

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

the class TransactionImpl method isOnePhase.

private boolean isOnePhase() {
    if (resourceList.size() == 0) {
        log.severe("Detected zero resources in resourceList");
        return true;
    }
    // check for more than one unique xid
    Iterator<ResourceElement> itr = resourceList.iterator();
    Xid xid = itr.next().getXid();
    while (itr.hasNext()) {
        if (!xid.equals(itr.next().getXid())) {
            return false;
        }
    }
    return true;
}
Also used : Xid(javax.transaction.xa.Xid)

Example 57 with Xid

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

the class TransactionImpl method doCommit.

void doCommit() throws XAException, SystemException {
    boolean onePhase = isOnePhase();
    boolean readOnly = true;
    if (!onePhase) {
        // prepare
        status = Status.STATUS_PREPARING;
        LinkedList<Xid> preparedXids = new LinkedList<Xid>();
        Iterator<ResourceElement> itr = resourceList.iterator();
        while (itr.hasNext()) {
            ResourceElement re = itr.next();
            if (!preparedXids.contains(re.getXid())) {
                preparedXids.add(re.getXid());
                int vote = re.getResource().prepare(re.getXid());
                if (vote == XAResource.XA_OK) {
                    readOnly = false;
                } else if (vote == XAResource.XA_RDONLY) {
                    re.setStatus(RS_READONLY);
                } else {
                    // rollback tx
                    status = Status.STATUS_MARKED_ROLLBACK;
                    return;
                }
            } else {
                // set it to readonly, only need to commit once
                re.setStatus(RS_READONLY);
            }
        }
        status = Status.STATUS_PREPARED;
    }
    // commit
    if (!onePhase && readOnly) {
        status = Status.STATUS_COMMITTED;
        return;
    }
    if (!onePhase) {
        try {
            txManager.getTxLog().markAsCommitting(getGlobalId());
        } 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);
        }
    }
    status = Status.STATUS_COMMITTING;
    Iterator<ResourceElement> itr = resourceList.iterator();
    while (itr.hasNext()) {
        ResourceElement re = itr.next();
        if (re.getStatus() != RS_READONLY) {
            re.getResource().commit(re.getXid(), onePhase);
        }
    }
    status = Status.STATUS_COMMITTED;
}
Also used : Xid(javax.transaction.xa.Xid) SystemException(javax.transaction.SystemException) IOException(java.io.IOException) LinkedList(java.util.LinkedList)

Example 58 with Xid

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

Example 59 with Xid

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

the class TestXa method testApplyLogicalLog.

@Test
public void testApplyLogicalLog() throws Exception {
    ds.keepLogicalLogs(true);
    Xid xid = new XidImpl(new byte[1], new byte[1]);
    XAResource xaRes = xaCon.getXaResource();
    xaRes.start(xid, XAResource.TMNOFLAGS);
    long node1 = ds.nextId(Node.class);
    xaCon.getNodeConsumer().createNode(node1);
    long node2 = ds.nextId(Node.class);
    xaCon.getNodeConsumer().createNode(node2);
    long n1prop1 = ds.nextId(PropertyStore.class);
    xaCon.getNodeConsumer().addProperty(node1, n1prop1, index("prop1"), "string1");
    xaCon.getNodeConsumer().getProperties(node1, false);
    int relType1 = (int) ds.nextId(RelationshipType.class);
    xaCon.getRelationshipTypeConsumer().addRelationshipType(relType1, "relationshiptype1");
    long rel1 = ds.nextId(Relationship.class);
    xaCon.getRelationshipConsumer().createRelationship(rel1, node1, node2, relType1);
    long r1prop1 = ds.nextId(PropertyStore.class);
    xaCon.getRelationshipConsumer().addProperty(rel1, r1prop1, index("prop1"), "string1");
    xaCon.getNodeConsumer().changeProperty(node1, n1prop1, "string2");
    xaCon.getRelationshipConsumer().changeProperty(rel1, r1prop1, "string2");
    xaCon.getNodeConsumer().removeProperty(node1, n1prop1);
    xaCon.getRelationshipConsumer().removeProperty(rel1, r1prop1);
    xaCon.getRelationshipConsumer().deleteRelationship(rel1);
    xaCon.getNodeConsumer().deleteNode(node1);
    xaCon.getNodeConsumer().deleteNode(node2);
    xaRes.end(xid, XAResource.TMSUCCESS);
    xaRes.commit(xid, true);
    long currentVersion = ds.getCurrentLogVersion();
    ds.keepLogicalLogs(true);
    ds.rotateLogicalLog();
    ds.rotateLogicalLog();
    ds.rotateLogicalLog();
    ds.setCurrentLogVersion(currentVersion);
    ds.setLastCommittedTxId(0);
    ds.makeBackupSlave();
    ds.applyLog(ds.getLogicalLog(currentVersion));
    ds.applyLog(ds.getLogicalLog(currentVersion + 1));
    ds.applyLog(ds.getLogicalLog(currentVersion + 2));
    ds.keepLogicalLogs(false);
}
Also used : Xid(javax.transaction.xa.Xid) XAResource(javax.transaction.xa.XAResource) XidImpl(org.neo4j.kernel.impl.transaction.XidImpl) RelationshipType(org.neo4j.graphdb.RelationshipType) Test(org.junit.Test)

Example 60 with Xid

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

the class TestXa method testBrokenNodeCommand.

@Test
public void testBrokenNodeCommand() throws Exception {
    Xid xid = new XidImpl(new byte[4], new byte[4]);
    XAResource xaRes = xaCon.getXaResource();
    xaRes.start(xid, XAResource.TMNOFLAGS);
    long node1 = ds.nextId(Node.class);
    xaCon.getNodeConsumer().createNode(node1);
    xaRes.end(xid, XAResource.TMSUCCESS);
    xaRes.prepare(xid);
    xaCon.clearAllTransactions();
    copyLogicalLog(path());
    xaCon.clearAllTransactions();
    ds.close();
    deleteLogicalLogIfExist();
    renameCopiedLogicalLog(path());
    truncateLogicalLog(40);
    ds = newNeoStore();
    //        ds = new NeoStoreXaDataSource( file( "neo" ), file( "nioneo_logical.log" ),
    //            lockManager, lockReleaser );
    xaCon = (NeoStoreXaConnection) ds.getXaConnection();
    xaRes = xaCon.getXaResource();
    assertEquals(0, xaRes.recover(XAResource.TMNOFLAGS).length);
    xaCon.clearAllTransactions();
}
Also used : Xid(javax.transaction.xa.Xid) XAResource(javax.transaction.xa.XAResource) XidImpl(org.neo4j.kernel.impl.transaction.XidImpl) Test(org.junit.Test)

Aggregations

Xid (javax.transaction.xa.Xid)82 Test (org.junit.Test)35 XAException (javax.transaction.xa.XAException)20 IOException (java.io.IOException)16 XAResource (javax.transaction.xa.XAResource)14 UnitTest (nl.topicus.jdbc.test.category.UnitTest)11 XidImpl (org.neo4j.kernel.impl.transaction.XidImpl)11 LinkedList (java.util.LinkedList)10 InOrder (org.mockito.InOrder)6 HashMap (java.util.HashMap)5 RecoveredXid (nl.topicus.jdbc.xa.RecoveredXid)5 RelationshipType (org.neo4j.graphdb.RelationshipType)5 HazelcastXAResource (com.hazelcast.transaction.HazelcastXAResource)4 ArrayList (java.util.ArrayList)4 RollbackException (javax.transaction.RollbackException)4 SystemException (javax.transaction.SystemException)4 TransactionFailureException (org.neo4j.graphdb.TransactionFailureException)4 XaResource (org.neo4j.kernel.impl.transaction.xaframework.XaResource)4 TransactionContext (com.hazelcast.transaction.TransactionContext)3 SerializableXID (com.hazelcast.transaction.impl.xa.SerializableXID)3