Search in sources :

Example 36 with OConcurrentModificationException

use of com.orientechnologies.orient.core.exception.OConcurrentModificationException in project orientdb by orientechnologies.

the class ORidBagTest method testContentChange.

public void testContentChange() {
    ODocument document = new ODocument();
    final ORidBag ridBag = new ORidBag();
    document.field("ridBag", ridBag);
    document.save();
    ridBag.add(new ORecordId("#77:10"));
    Assert.assertTrue(document.isDirty());
    boolean expectCME = false;
    if (ORecordInternal.isContentChanged(document)) {
        assertEmbedded(true);
        expectCME = true;
    } else {
        assertEmbedded(false);
    }
    document.save();
    ODocument copy = new ODocument();
    copy.fromStream(document.toStream());
    ORecordInternal.setIdentity(copy, new ORecordId(document.getIdentity()));
    ORecordInternal.setVersion(copy, document.getVersion());
    ORidBag copyRidBag = copy.field("ridBag");
    Assert.assertNotSame(copyRidBag, ridBag);
    copyRidBag.add(new ORecordId("#77:11"));
    Assert.assertTrue(copy.isDirty());
    Assert.assertTrue(!document.isDirty());
    ridBag.add(new ORecordId("#77:12"));
    Assert.assertTrue(document.isDirty());
    document.save();
    try {
        copy.save();
        Assert.assertTrue(!expectCME);
    } catch (OConcurrentModificationException cme) {
        Assert.assertTrue(expectCME);
    }
}
Also used : ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) OConcurrentModificationException(com.orientechnologies.orient.core.exception.OConcurrentModificationException) ORecordId(com.orientechnologies.orient.core.id.ORecordId) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 37 with OConcurrentModificationException

use of com.orientechnologies.orient.core.exception.OConcurrentModificationException in project orientdb by orientechnologies.

the class TransactionAtomicTest method testMVCC.

@Test
public void testMVCC() throws IOException {
    ODocument doc = new ODocument("Account");
    doc.field("version", 0);
    doc.save();
    doc.setDirty();
    doc.field("testmvcc", true);
    ORecordInternal.setVersion(doc, doc.getVersion() + 1);
    try {
        doc.save();
        Assert.assertTrue(false);
    } catch (OConcurrentModificationException e) {
        Assert.assertTrue(true);
    }
}
Also used : OConcurrentModificationException(com.orientechnologies.orient.core.exception.OConcurrentModificationException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Example 38 with OConcurrentModificationException

use of com.orientechnologies.orient.core.exception.OConcurrentModificationException in project orientdb by orientechnologies.

the class TransactionConsistencyTest method test3RollbackWithCopyCacheStrategy.

@Test
public void test3RollbackWithCopyCacheStrategy() throws IOException {
    database1 = new ODatabaseDocumentTx(url).open("admin", "admin");
    // Create docA.
    ODocument vDocA_db1 = database1.newInstance();
    vDocA_db1.field(NAME, "docA");
    database1.save(vDocA_db1);
    // Keep the IDs.
    ORID vDocA_Rid = vDocA_db1.getIdentity().copy();
    database2 = new ODatabaseDocumentTx(url).open("admin", "admin");
    database2.begin(TXTYPE.OPTIMISTIC);
    try {
        // Get docA and update in db2 transaction context
        ODocument vDocA_db2 = database2.load(vDocA_Rid);
        vDocA_db2.field(NAME, "docA_v2");
        database2.save(vDocA_db2);
        database1.activateOnCurrentThread();
        database1.begin(TXTYPE.OPTIMISTIC);
        try {
            vDocA_db1.field(NAME, "docA_v3");
            database1.save(vDocA_db1);
            database1.commit();
        } catch (OConcurrentModificationException e) {
            Assert.fail("Should not failed here...");
        }
        Assert.assertEquals(vDocA_db1.field(NAME), "docA_v3");
        // Will throw OConcurrentModificationException
        database2.activateOnCurrentThread();
        database2.commit();
        Assert.fail("Should throw OConcurrentModificationException");
    } catch (OConcurrentModificationException e) {
        database2.rollback();
    }
    // Force reload all (to be sure it is not a cache problem)
    database1.activateOnCurrentThread();
    database1.close();
    database2.activateOnCurrentThread();
    database2.close();
    database2 = new ODatabaseDocumentTx(url).open("admin", "admin");
    // docB should be in the last state : "docA_v3"
    ODocument vDocB_db2 = database2.load(vDocA_Rid);
    Assert.assertEquals(vDocB_db2.field(NAME), "docA_v3");
    database1.activateOnCurrentThread();
    database1.close();
    database2.activateOnCurrentThread();
    database2.close();
}
Also used : ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ORID(com.orientechnologies.orient.core.id.ORID) OConcurrentModificationException(com.orientechnologies.orient.core.exception.OConcurrentModificationException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test) DatabaseAbstractTest(com.orientechnologies.DatabaseAbstractTest)

Example 39 with OConcurrentModificationException

use of com.orientechnologies.orient.core.exception.OConcurrentModificationException in project orientdb by orientechnologies.

the class TransactionConsistencyTest method TransactionRollbackConstistencyTest.

public void TransactionRollbackConstistencyTest() {
    // System.out.println("**************************TransactionRollbackConsistencyTest***************************************");
    database = new ODatabaseDocumentTx(url).open("admin", "admin");
    OClass vertexClass = database.getMetadata().getSchema().createClass("TRVertex");
    OClass edgeClass = database.getMetadata().getSchema().createClass("TREdge");
    vertexClass.createProperty("in", OType.LINKSET, edgeClass);
    vertexClass.createProperty("out", OType.LINKSET, edgeClass);
    edgeClass.createProperty("in", OType.LINK, vertexClass);
    edgeClass.createProperty("out", OType.LINK, vertexClass);
    OClass personClass = database.getMetadata().getSchema().createClass("TRPerson", vertexClass);
    personClass.createProperty("name", OType.STRING).createIndex(OClass.INDEX_TYPE.UNIQUE);
    personClass.createProperty("surname", OType.STRING).createIndex(OClass.INDEX_TYPE.NOTUNIQUE);
    personClass.createProperty("version", OType.INTEGER);
    database.getMetadata().getSchema().save();
    database.close();
    final int cnt = 4;
    database.open("admin", "admin");
    database.begin();
    Vector inserted = new Vector();
    for (int i = 0; i < cnt; i++) {
        ODocument person = new ODocument("TRPerson");
        person.field("name", Character.toString((char) ('A' + i)));
        person.field("surname", Character.toString((char) ('A' + (i % 3))));
        person.field("myversion", 0);
        person.field("in", new HashSet<ODocument>());
        person.field("out", new HashSet<ODocument>());
        if (i >= 1) {
            ODocument edge = new ODocument("TREdge");
            edge.field("in", person.getIdentity());
            edge.field("out", inserted.elementAt(i - 1));
            ((Set<ODocument>) person.field("out")).add(edge);
            ((Set<ODocument>) ((ODocument) inserted.elementAt(i - 1)).field("in")).add(edge);
            edge.save();
        }
        inserted.add(person);
        person.save();
    }
    database.commit();
    final List<ODocument> result1 = database.command(new OCommandSQL("select from TRPerson")).execute();
    Assert.assertNotNull(result1);
    Assert.assertEquals(result1.size(), cnt);
    try {
        database.begin();
        Vector inserted2 = new Vector();
        for (int i = 0; i < cnt; i++) {
            ODocument person = new ODocument("TRPerson");
            person.field("name", Character.toString((char) ('a' + i)));
            person.field("surname", Character.toString((char) ('a' + (i % 3))));
            person.field("myversion", 0);
            person.field("in", new HashSet<ODocument>());
            person.field("out", new HashSet<ODocument>());
            if (i >= 1) {
                ODocument edge = new ODocument("TREdge");
                edge.field("in", person.getIdentity());
                edge.field("out", inserted2.elementAt(i - 1));
                ((Set<ODocument>) person.field("out")).add(edge);
                ((Set<ODocument>) ((ODocument) inserted2.elementAt(i - 1)).field("in")).add(edge);
                edge.save();
            }
            inserted2.add(person);
            person.save();
        }
        for (int i = 0; i < cnt; i++) {
            if (i != cnt - 1) {
                ((ODocument) inserted.elementAt(i)).field("myversion", 2);
                ((ODocument) inserted.elementAt(i)).save();
            }
        }
        ((ODocument) inserted.elementAt(cnt - 1)).delete();
        ORecordInternal.setVersion(((ODocument) inserted.elementAt(cnt - 2)), 0);
        ((ODocument) inserted.elementAt(cnt - 2)).save();
        database.commit();
        Assert.assertTrue(false);
    } catch (OConcurrentModificationException e) {
        Assert.assertTrue(true);
        database.rollback();
    }
    final List<ODocument> result2 = database.command(new OCommandSQL("select from TRPerson")).execute();
    Assert.assertNotNull(result2);
    // System.out.println("After transaction commit failure/rollback");
    // for (ODocument d : result2)
    // System.out.println(d);
    Assert.assertEquals(result2.size(), cnt);
// System.out.println("**************************TransactionRollbackConstistencyTest***************************************");
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) HashSet(java.util.HashSet) Set(java.util.Set) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OConcurrentModificationException(com.orientechnologies.orient.core.exception.OConcurrentModificationException) Vector(java.util.Vector) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 40 with OConcurrentModificationException

use of com.orientechnologies.orient.core.exception.OConcurrentModificationException in project orientdb by orientechnologies.

the class TransactionOptimisticTest method testTransactionOptimisticConcurrentException.

@Test(dependsOnMethods = "testTransactionOptimisticCommit")
public void testTransactionOptimisticConcurrentException() throws IOException {
    if (database.getClusterIdByName("binary") == -1)
        database.addBlobCluster("binary");
    ODatabaseDocumentTx db2 = new ODatabaseDocumentTx(database.getURL());
    db2.open("admin", "admin");
    database.activateOnCurrentThread();
    OBlob record1 = new ORecordBytes("This is the first version".getBytes());
    record1.save("binary");
    try {
        database.begin();
        // RE-READ THE RECORD
        record1.load();
        ODatabaseRecordThreadLocal.INSTANCE.set(db2);
        OBlob record2 = db2.load(record1.getIdentity());
        record2.setDirty();
        record2.fromStream("This is the second version".getBytes());
        record2.save();
        ODatabaseRecordThreadLocal.INSTANCE.set(database);
        record1.setDirty();
        record1.fromStream("This is the third version".getBytes());
        record1.save();
        database.commit();
        Assert.assertTrue(false);
    } catch (OConcurrentModificationException e) {
        Assert.assertTrue(true);
        database.rollback();
    } finally {
        database.close();
        db2.activateOnCurrentThread();
        db2.close();
    }
}
Also used : OBlob(com.orientechnologies.orient.core.record.impl.OBlob) ORecordBytes(com.orientechnologies.orient.core.record.impl.ORecordBytes) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OConcurrentModificationException(com.orientechnologies.orient.core.exception.OConcurrentModificationException) Test(org.testng.annotations.Test)

Aggregations

OConcurrentModificationException (com.orientechnologies.orient.core.exception.OConcurrentModificationException)40 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)25 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)11 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)11 ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)11 OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)8 ArrayList (java.util.ArrayList)8 Test (org.testng.annotations.Test)8 Vertex (com.tinkerpop.blueprints.Vertex)7 DatabaseAbstractTest (com.orientechnologies.DatabaseAbstractTest)6 Test (org.junit.Test)5 ORID (com.orientechnologies.orient.core.id.ORID)4 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)4 OrientBaseGraph (com.tinkerpop.blueprints.impls.orient.OrientBaseGraph)4 OrientGraph (com.tinkerpop.blueprints.impls.orient.OrientGraph)4 ORecordNotFoundException (com.orientechnologies.orient.core.exception.ORecordNotFoundException)3 ONeedRetryException (com.orientechnologies.common.concur.ONeedRetryException)2 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)2 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)2 ORecord (com.orientechnologies.orient.core.record.ORecord)2