Search in sources :

Example 11 with Xid

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

the class TestXa method testLogicalLogPrepared.

@Test
public void testLogicalLogPrepared() throws Exception {
    Xid xid = new XidImpl(new byte[2], new byte[2]);
    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");
    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");
    xaRes.end(xid, XAResource.TMSUCCESS);
    xaRes.prepare(xid);
    ds.rotateLogicalLog();
    copyLogicalLog(path());
    xaCon.clearAllTransactions();
    ds.close();
    deleteLogicalLogIfExist();
    renameCopiedLogicalLog(path());
    ds = newNeoStore();
    //        ds = new NeoStoreXaDataSource( file( "neo" ), file( "nioneo_logical.log" ),
    //            lockManager, lockReleaser );
    xaCon = (NeoStoreXaConnection) ds.getXaConnection();
    xaRes = xaCon.getXaResource();
    assertEquals(1, xaRes.recover(XAResource.TMNOFLAGS).length);
    xaRes.commit(xid, true);
    xaCon.clearAllTransactions();
}
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 12 with Xid

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

the class TestJtaCompliance method test2PhaseCommits1.

/**
     * o Tests two-phase commits with two different fake XAResource
     * implementations so a branch is created within the same global
     * transaction.
     */
@Test
public void test2PhaseCommits1() throws Exception {
    tm.begin();
    FakeXAResource res1 = new FakeXAResource("XAResource1");
    FakeXAResource res2 = new FakeXAResource("XAResource2");
    // enlist two different resources and verify that the start method
    // is invoked with correct flags
    // res1
    tm.getTransaction().enlistResource(res1);
    MethodCall[] calls1 = res1.getAndRemoveMethodCalls();
    assertEquals(1, calls1.length);
    assertEquals("start", calls1[0].getMethodName());
    // res2
    tm.getTransaction().enlistResource(res2);
    MethodCall[] calls2 = res2.getAndRemoveMethodCalls();
    assertEquals(1, calls2.length);
    assertEquals("start", calls2[0].getMethodName());
    // verify Xid
    Object[] args = calls1[0].getArgs();
    Xid xid1 = (Xid) args[0];
    assertEquals(XAResource.TMNOFLAGS, ((Integer) args[1]).intValue());
    args = calls2[0].getArgs();
    Xid xid2 = (Xid) args[0];
    assertEquals(XAResource.TMNOFLAGS, ((Integer) args[1]).intValue());
    // should have same global transaction id
    byte[] globalTxId1 = xid1.getGlobalTransactionId();
    byte[] globalTxId2 = xid2.getGlobalTransactionId();
    assertTrue(globalTxId1.length == globalTxId2.length);
    for (int i = 0; i < globalTxId1.length; i++) {
        assertEquals(globalTxId1[i], globalTxId2[i]);
    }
    byte[] branch1 = xid1.getBranchQualifier();
    byte[] branch2 = xid2.getBranchQualifier();
    // make sure a different branch was created
    if (branch1.length == branch2.length) {
        boolean same = true;
        for (int i = 0; i < branch1.length; i++) {
            if (branch1[i] != branch2[i]) {
                same = false;
                break;
            }
        }
        assertTrue(!same);
    }
    // verify delist of resource
    tm.getTransaction().delistResource(res2, XAResource.TMSUCCESS);
    calls2 = res2.getAndRemoveMethodCalls();
    assertEquals(1, calls2.length);
    tm.getTransaction().delistResource(res1, XAResource.TMSUCCESS);
    calls1 = res1.getAndRemoveMethodCalls();
    // res1
    assertEquals(1, calls1.length);
    assertEquals("end", calls1[0].getMethodName());
    args = calls1[0].getArgs();
    assertTrue(((Xid) args[0]).equals(xid1));
    assertEquals(XAResource.TMSUCCESS, ((Integer) args[1]).intValue());
    // res2
    assertEquals(1, calls2.length);
    assertEquals("end", calls2[0].getMethodName());
    args = calls2[0].getArgs();
    assertTrue(((Xid) args[0]).equals(xid2));
    assertEquals(XAResource.TMSUCCESS, ((Integer) args[1]).intValue());
    // verify proper prepare/commit
    tm.commit();
    calls1 = res1.getAndRemoveMethodCalls();
    calls2 = res2.getAndRemoveMethodCalls();
    // res1
    assertEquals(2, calls1.length);
    assertEquals("prepare", calls1[0].getMethodName());
    args = calls1[0].getArgs();
    assertTrue(((Xid) args[0]).equals(xid1));
    assertEquals("commit", calls1[1].getMethodName());
    args = calls1[1].getArgs();
    assertTrue(((Xid) args[0]).equals(xid1));
    assertEquals(false, ((Boolean) args[1]).booleanValue());
    // res2
    assertEquals(2, calls2.length);
    assertEquals("prepare", calls2[0].getMethodName());
    args = calls2[0].getArgs();
    assertTrue(((Xid) args[0]).equals(xid2));
    assertEquals("commit", calls2[1].getMethodName());
    args = calls2[1].getArgs();
    assertTrue(((Xid) args[0]).equals(xid2));
    assertEquals(false, ((Boolean) args[1]).booleanValue());
}
Also used : Xid(javax.transaction.xa.Xid) Test(org.junit.Test)

Example 13 with Xid

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

the class TestJtaCompliance method testRollback2.

/*
     * o Tests that multiple enlistments of same (according to isSameRM()
     * method) only receive one set of rollback calls.
     */
@Test
public void testRollback2() throws Exception {
    tm.begin();
    FakeXAResource res1 = new FakeXAResource("XAResource1");
    FakeXAResource res2 = new FakeXAResource("XAResource1");
    // enlist two (same) resources and verify that the start method
    // is invoked with correct flags
    // res1
    tm.getTransaction().enlistResource(res1);
    MethodCall[] calls1 = res1.getAndRemoveMethodCalls();
    assertEquals(1, calls1.length);
    assertEquals("start", calls1[0].getMethodName());
    // res2
    tm.getTransaction().enlistResource(res2);
    MethodCall[] calls2 = res2.getAndRemoveMethodCalls();
    assertEquals(1, calls2.length);
    assertEquals("start", calls2[0].getMethodName());
    // verify Xid and flags
    Object[] args = calls1[0].getArgs();
    Xid xid1 = (Xid) args[0];
    assertEquals(XAResource.TMNOFLAGS, ((Integer) args[1]).intValue());
    args = calls2[0].getArgs();
    Xid xid2 = (Xid) args[0];
    assertEquals(XAResource.TMJOIN, ((Integer) args[1]).intValue());
    assertTrue(xid1.equals(xid2));
    assertTrue(xid2.equals(xid1));
    // verify delist of resource
    tm.getTransaction().delistResource(res2, XAResource.TMSUCCESS);
    calls2 = res2.getAndRemoveMethodCalls();
    assertEquals(1, calls2.length);
    tm.getTransaction().delistResource(res1, XAResource.TMSUCCESS);
    calls1 = res1.getAndRemoveMethodCalls();
    // res1
    assertEquals(1, calls1.length);
    assertEquals("end", calls1[0].getMethodName());
    args = calls1[0].getArgs();
    assertTrue(((Xid) args[0]).equals(xid1));
    assertEquals(XAResource.TMSUCCESS, ((Integer) args[1]).intValue());
    // res2
    assertEquals(1, calls2.length);
    assertEquals("end", calls2[0].getMethodName());
    args = calls2[0].getArgs();
    assertTrue(((Xid) args[0]).equals(xid2));
    assertEquals(XAResource.TMSUCCESS, ((Integer) args[1]).intValue());
    // verify proper prepare/commit
    tm.rollback();
    calls1 = res1.getAndRemoveMethodCalls();
    calls2 = res2.getAndRemoveMethodCalls();
    // res1
    assertEquals(1, calls1.length);
    assertEquals("rollback", calls1[0].getMethodName());
    args = calls1[0].getArgs();
    assertTrue(((Xid) args[0]).equals(xid1));
    // res2
    assertEquals(0, calls2.length);
}
Also used : Xid(javax.transaction.xa.Xid) Test(org.junit.Test)

Example 14 with Xid

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

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

the class TestXa method testLogicalLog.

@Test
public void testLogicalLog() throws Exception {
    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);
    copyLogicalLog(path());
    xaCon.clearAllTransactions();
    ds.close();
    deleteLogicalLogIfExist();
    renameCopiedLogicalLog(path());
    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) RelationshipType(org.neo4j.graphdb.RelationshipType) Test(org.junit.Test)

Aggregations

Xid (javax.transaction.xa.Xid)63 Test (org.junit.Test)23 XAException (javax.transaction.xa.XAException)19 IOException (java.io.IOException)16 XAResource (javax.transaction.xa.XAResource)13 XidImpl (org.neo4j.kernel.impl.transaction.XidImpl)11 LinkedList (java.util.LinkedList)10 InOrder (org.mockito.InOrder)6 HashMap (java.util.HashMap)5 RelationshipType (org.neo4j.graphdb.RelationshipType)5 HazelcastXAResource (com.hazelcast.transaction.HazelcastXAResource)4 ArrayList (java.util.ArrayList)4 RollbackException (javax.transaction.RollbackException)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 SystemException (javax.transaction.SystemException)3 HazelcastInstance (com.hazelcast.core.HazelcastInstance)2 HashSet (java.util.HashSet)2