Search in sources :

Example 66 with ODatabaseDocumentTx

use of com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx in project orientdb by orientechnologies.

the class TransactionConsistencyTest method createLinkInTx.

@SuppressWarnings("unchecked")
@Test
public void createLinkInTx() {
    database = new ODatabaseDocumentTx(url).open("admin", "admin");
    OClass profile = database.getMetadata().getSchema().createClass("MyProfile", 1, null);
    OClass edge = database.getMetadata().getSchema().createClass("MyEdge", 1, null);
    profile.createProperty("name", OType.STRING).setMin("3").setMax("30").createIndex(OClass.INDEX_TYPE.NOTUNIQUE);
    profile.createProperty("surname", OType.STRING).setMin("3").setMax("30");
    profile.createProperty("in", OType.LINKSET, edge);
    profile.createProperty("out", OType.LINKSET, edge);
    edge.createProperty("in", OType.LINK, profile);
    edge.createProperty("out", OType.LINK, profile);
    database.begin();
    ODocument kim = new ODocument("MyProfile").field("name", "Kim").field("surname", "Bauer");
    ODocument teri = new ODocument("MyProfile").field("name", "Teri").field("surname", "Bauer");
    ODocument jack = new ODocument("MyProfile").field("name", "Jack").field("surname", "Bauer");
    ODocument myedge = new ODocument("MyEdge").field("in", kim).field("out", jack);
    myedge.save();
    ((HashSet<ODocument>) kim.field("out", new HashSet<ORID>()).field("out")).add(myedge);
    ((HashSet<ODocument>) jack.field("in", new HashSet<ORID>()).field("in")).add(myedge);
    jack.save();
    kim.save();
    teri.save();
    database.commit();
    database.close();
    database.open("admin", "admin");
    List<ODocument> result = database.command(new OSQLSynchQuery<ODocument>("select from MyProfile ")).execute();
    Assert.assertTrue(result.size() != 0);
    database.close();
}
Also used : OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ORID(com.orientechnologies.orient.core.id.ORID) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) HashSet(java.util.HashSet) Test(org.testng.annotations.Test) DatabaseAbstractTest(com.orientechnologies.DatabaseAbstractTest)

Example 67 with ODatabaseDocumentTx

use of com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx in project orientdb by orientechnologies.

the class TransactionConsistencyTest method test1RollbackOnConcurrentException.

@Test
public void test1RollbackOnConcurrentException() throws IOException {
    database1 = new ODatabaseDocumentTx(url).open("admin", "admin");
    database1.begin(TXTYPE.OPTIMISTIC);
    // Create docA.
    ODocument vDocA_db1 = database1.newInstance();
    vDocA_db1.field(NAME, "docA");
    database1.save(vDocA_db1);
    // Create docB.
    ODocument vDocB_db1 = database1.newInstance();
    vDocB_db1.field(NAME, "docB");
    database1.save(vDocB_db1);
    database1.commit();
    // Keep the IDs.
    ORID vDocA_Rid = vDocA_db1.getIdentity().copy();
    ORID vDocB_Rid = vDocB_db1.getIdentity().copy();
    int vDocA_version = -1;
    int vDocB_version = -1;
    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);
        // Concurrent update docA via database1 -> will throw OConcurrentModificationException at database2.commit().
        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");
        // Keep the last versions.
        // Following updates should failed and reverted.
        vDocA_version = vDocA_db1.getVersion();
        vDocB_version = vDocB_db1.getVersion();
        // Update docB in db2 transaction context -> should be rollbacked.
        database2.activateOnCurrentThread();
        ODocument vDocB_db2 = database2.load(vDocB_Rid);
        vDocB_db2.field(NAME, "docB_UpdatedInTranscationThatWillBeRollbacked");
        database2.save(vDocB_db2);
        // Will throw OConcurrentModificationException
        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.getStorage().close();
    database2 = new ODatabaseDocumentTx(url).open("admin", "admin");
    ODocument vDocA_db2 = database2.load(vDocA_Rid);
    Assert.assertEquals(vDocA_db2.field(NAME), "docA_v3");
    Assert.assertEquals(vDocA_db2.getVersion(), vDocA_version);
    // docB should be in the first state : "docB"
    ODocument vDocB_db2 = database2.load(vDocB_Rid);
    Assert.assertEquals(vDocB_db2.field(NAME), "docB");
    Assert.assertEquals(vDocB_db2.getVersion(), vDocB_version);
    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 68 with ODatabaseDocumentTx

use of com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx in project orientdb by orientechnologies.

the class TransactionConsistencyTest method test4RollbackWithPin.

@Test
public void test4RollbackWithPin() 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 69 with ODatabaseDocumentTx

use of com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx in project orientdb by orientechnologies.

the class TraverseTest method traverseAndCheckReturn.

@Test
public void traverseAndCheckReturn() {
    try {
        String q = "traverse in('married')  from " + nicoleKidman.getIdentity() + "";
        ODatabaseDocumentTx db = database.copy();
        ODatabaseRecordThreadLocal.INSTANCE.set(db);
        List<Object> result1 = db.command(new OSQLSynchQuery<ODocument>(q)).execute();
        Assert.assertEquals(result1.size(), 2);
        boolean found = false;
        Integer i = 0;
        for (Object doc : result1) {
            Assert.assertTrue(doc instanceof ODocument);
        }
    } finally {
        ODatabaseRecordThreadLocal.INSTANCE.set(database);
    }
}
Also used : OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Example 70 with ODatabaseDocumentTx

use of com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx in project orientdb by orientechnologies.

the class TransactionIsolationTest method testIsolationRepeatableReadScript.

@Test
public void testIsolationRepeatableReadScript() throws ExecutionException, InterruptedException {
    final ODatabaseDocumentTx db1 = new ODatabaseDocumentTx(url);
    db1.open("admin", "admin");
    final ODocument record1 = new ODocument();
    record1.field("name", "This is the first version").save();
    Future<List<OIdentifiable>> txFuture = Orient.instance().submit(new Callable<List<OIdentifiable>>() {

        @Override
        public List<OIdentifiable> call() throws Exception {
            String cmd = "";
            cmd += "begin isolation REPEATABLE_READ;";
            cmd += "let r1 = select from " + record1.getIdentity() + ";";
            cmd += "sleep 2000;";
            cmd += "let r2 = select from " + record1.getIdentity() + ";";
            cmd += "commit;";
            cmd += "return $r2;";
            db1.activateOnCurrentThread();
            return db1.command(new OCommandScript("sql", cmd)).execute();
        }
    });
    Thread.sleep(500);
    // CHANGE THE RECORD FROM DB2
    ODatabaseDocumentTx db2 = new ODatabaseDocumentTx(url);
    db2.open("admin", "admin");
    ODocument record2 = db2.load(record1.getIdentity());
    record2.field("name", "This is the second version").save();
    List<OIdentifiable> txRecord = txFuture.get();
    Assert.assertNotNull(txRecord);
    Assert.assertEquals(txRecord.size(), 1);
    Assert.assertEquals(((ODocument) txRecord.get(0).getRecord()).field("name"), "This is the first version");
    db1.activateOnCurrentThread();
    db1.close();
    db2.activateOnCurrentThread();
    db2.close();
}
Also used : OCommandScript(com.orientechnologies.orient.core.command.script.OCommandScript) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) List(java.util.List) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Aggregations

ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)590 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)257 Test (org.testng.annotations.Test)182 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)120 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)81 Test (org.junit.Test)79 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)61 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)47 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)46 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)41 ORID (com.orientechnologies.orient.core.id.ORID)39 ORecordId (com.orientechnologies.orient.core.id.ORecordId)34 Before (org.junit.Before)34 File (java.io.File)33 OProperty (com.orientechnologies.orient.core.metadata.schema.OProperty)25 ArrayList (java.util.ArrayList)25 BeforeClass (org.testng.annotations.BeforeClass)23 BeforeMethod (org.testng.annotations.BeforeMethod)23 OStorage (com.orientechnologies.orient.core.storage.OStorage)21 List (java.util.List)20