Search in sources :

Example 11 with ORecordDuplicatedException

use of com.orientechnologies.orient.core.storage.ORecordDuplicatedException in project orientdb by orientechnologies.

the class IndexTest method testTransactionUniqueIndexTestWithDotNameTwo.

@Test(dependsOnMethods = "testTransactionUniqueIndexTestWithDotNameOne")
public void testTransactionUniqueIndexTestWithDotNameTwo() {
    ODatabaseDocumentTx db = new ODatabaseDocumentTx(database.getURL());
    db.open("admin", "admin");
    if (!db.getMetadata().getSchema().existsClass("TransactionUniqueIndexWithDotTest")) {
        final OClass termClass = db.getMetadata().getSchema().createClass("TransactionUniqueIndexWithDotTest", 1, null);
        termClass.createProperty("label", OType.STRING).createIndex(INDEX_TYPE.UNIQUE);
        db.getMetadata().getSchema().save();
    }
    final List<ODocument> resultBeforeCommit = db.query(new OSQLSynchQuery<ODocument>("select from index:TransactionUniqueIndexWithDotTest.label"));
    Assert.assertEquals(resultBeforeCommit.size(), 1);
    db.begin();
    try {
        ODocument docOne = new ODocument("TransactionUniqueIndexWithDotTest");
        docOne.field("label", "B");
        docOne.save();
        ODocument docTwo = new ODocument("TransactionUniqueIndexWithDotTest");
        docTwo.field("label", "B");
        docTwo.save();
        db.commit();
        Assert.fail();
    } catch (ORecordDuplicatedException oie) {
        db.rollback();
    }
    final List<ODocument> resultAfterCommit = db.query(new OSQLSynchQuery<ODocument>("select from  index:TransactionUniqueIndexWithDotTest.label"));
    Assert.assertEquals(resultAfterCommit.size(), 1);
}
Also used : ORecordDuplicatedException(com.orientechnologies.orient.core.storage.ORecordDuplicatedException) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test) DatabaseAbstractTest(com.orientechnologies.DatabaseAbstractTest) OrientTest(com.orientechnologies.orient.test.database.base.OrientTest)

Example 12 with ORecordDuplicatedException

use of com.orientechnologies.orient.core.storage.ORecordDuplicatedException in project orientdb by orientechnologies.

the class IndexTest method testDuplicatedIndexOnUnique.

public void testDuplicatedIndexOnUnique() {
    Profile jayMiner = new Profile("Jay", "Jay", "Miner", null);
    database.save(jayMiner);
    Profile jacobMiner = new Profile("Jay", "Jacob", "Miner", null);
    try {
        database.save(jacobMiner);
        // IT SHOULD GIVE ERROR ON DUPLICATED KEY
        Assert.assertTrue(false);
    } catch (ORecordDuplicatedException e) {
        Assert.assertTrue(true);
    }
}
Also used : ORecordDuplicatedException(com.orientechnologies.orient.core.storage.ORecordDuplicatedException) Profile(com.orientechnologies.orient.test.domain.whiz.Profile)

Example 13 with ORecordDuplicatedException

use of com.orientechnologies.orient.core.storage.ORecordDuplicatedException in project orientdb by orientechnologies.

the class IndexTest method testTransactionUniqueIndexTestTwo.

@Test(dependsOnMethods = "testTransactionUniqueIndexTestOne")
public void testTransactionUniqueIndexTestTwo() {
    ODatabaseDocumentTx db = new ODatabaseDocumentTx(database.getURL());
    db.open("admin", "admin");
    if (!db.getMetadata().getSchema().existsClass("TransactionUniqueIndexTest")) {
        final OClass termClass = db.getMetadata().getSchema().createClass("TransactionUniqueIndexTest", 1, null);
        termClass.createProperty("label", OType.STRING);
        termClass.createIndex("idxTransactionUniqueIndexTest", INDEX_TYPE.UNIQUE.toString(), null, new ODocument().fields("ignoreNullValues", true), new String[] { "label" });
        db.getMetadata().getSchema().save();
    }
    final List<ODocument> resultBeforeCommit = db.query(new OSQLSynchQuery<ODocument>("select from index:idxTransactionUniqueIndexTest"));
    Assert.assertEquals(resultBeforeCommit.size(), 1);
    db.begin();
    try {
        ODocument docOne = new ODocument("TransactionUniqueIndexTest");
        docOne.field("label", "B");
        docOne.save();
        ODocument docTwo = new ODocument("TransactionUniqueIndexTest");
        docTwo.field("label", "B");
        docTwo.save();
        db.commit();
        Assert.fail();
    } catch (ORecordDuplicatedException oie) {
        db.rollback();
    }
    final List<ODocument> resultAfterCommit = db.query(new OSQLSynchQuery<ODocument>("select from index:idxTransactionUniqueIndexTest"));
    Assert.assertEquals(resultAfterCommit.size(), 1);
}
Also used : ORecordDuplicatedException(com.orientechnologies.orient.core.storage.ORecordDuplicatedException) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test) DatabaseAbstractTest(com.orientechnologies.DatabaseAbstractTest) OrientTest(com.orientechnologies.orient.test.database.base.OrientTest)

Example 14 with ORecordDuplicatedException

use of com.orientechnologies.orient.core.storage.ORecordDuplicatedException in project orientdb by orientechnologies.

the class IndexTest method testTransactionUniqueIndexTestWithDotNameOne.

public void testTransactionUniqueIndexTestWithDotNameOne() {
    ODatabaseDocumentTx db = new ODatabaseDocumentTx(database.getURL());
    db.open("admin", "admin");
    if (!db.getMetadata().getSchema().existsClass("TransactionUniqueIndexWithDotTest")) {
        final OClass termClass = db.getMetadata().getSchema().createClass("TransactionUniqueIndexWithDotTest", 1, null);
        termClass.createProperty("label", OType.STRING).createIndex(INDEX_TYPE.UNIQUE);
        db.getMetadata().getSchema().save();
    }
    ODocument docOne = new ODocument("TransactionUniqueIndexWithDotTest");
    docOne.field("label", "A");
    docOne.save();
    final List<ODocument> resultBeforeCommit = db.query(new OSQLSynchQuery<ODocument>("select from  index:TransactionUniqueIndexWithDotTest.label"));
    Assert.assertEquals(resultBeforeCommit.size(), 1);
    long countClassBefore = db.countClass("TransactionUniqueIndexWithDotTest");
    db.begin();
    try {
        ODocument docTwo = new ODocument("TransactionUniqueIndexWithDotTest");
        docTwo.field("label", "A");
        docTwo.save();
        db.commit();
        Assert.fail();
    } catch (ORecordDuplicatedException oie) {
    }
    Assert.assertEquals(((List<ODocument>) db.command(new OCommandSQL("select from TransactionUniqueIndexWithDotTest")).execute()).size(), countClassBefore);
    final List<ODocument> resultAfterCommit = db.query(new OSQLSynchQuery<ODocument>("select from  index:TransactionUniqueIndexWithDotTest.label"));
    Assert.assertEquals(resultAfterCommit.size(), 1);
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) ORecordDuplicatedException(com.orientechnologies.orient.core.storage.ORecordDuplicatedException) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 15 with ORecordDuplicatedException

use of com.orientechnologies.orient.core.storage.ORecordDuplicatedException in project orientdb by orientechnologies.

the class OTxTask method execute.

@Override
public Object execute(final ODistributedRequestId requestId, final OServer iServer, ODistributedServerManager iManager, final ODatabaseDocumentInternal database) throws Exception {
    ODistributedServerLog.debug(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.IN, "Executing transaction db=%s (reqId=%s)...", database.getName(), requestId);
    ODatabaseRecordThreadLocal.INSTANCE.set(database);
    final ODistributedDatabase ddb = iManager.getMessageService().getDatabase(database.getName());
    // CREATE A CONTEXT OF TX
    reqContext = ddb.registerTxContext(requestId);
    final ODistributedConfiguration dCfg = iManager.getDatabaseConfiguration(database.getName());
    result = new OTxTaskResult();
    if (tasks.size() == 0)
        // RETURN AFTER REGISTERED THE CONTEXT
        return result;
    database.begin();
    try {
        final OTransactionOptimistic tx = (OTransactionOptimistic) database.getTransaction();
        // REGISTER CREATE FIRST TO RESOLVE TEMP RIDS
        for (OAbstractRecordReplicatedTask task : tasks) {
            if (task instanceof OCreateRecordTask) {
                final OCreateRecordTask createRT = (OCreateRecordTask) task;
                final ORecordId rid = createRT.getRid();
                if (rid != null && rid.isPersistent()) {
                    if (rid.getRecord() != null)
                        // ALREADY CREATED: SKIP REGISTERING IN TX
                        continue;
                }
                final int clId = createRT.clusterId > -1 ? createRT.clusterId : createRT.getRid().isValid() ? createRT.getRid().getClusterId() : -1;
                final String clusterName = clId > -1 ? database.getClusterNameById(clId) : null;
                if (dCfg.isServerContainingCluster(iManager.getLocalNodeName(), clusterName))
                    tx.addRecord(createRT.getRecord(), ORecordOperation.CREATED, clusterName);
            }
        }
        final List<ORecordId> rids2Lock = new ArrayList<ORecordId>();
        // LOCK ALL THE RECORDS FIRST (ORDERED TO AVOID DEADLOCK)
        for (OAbstractRecordReplicatedTask task : tasks) rids2Lock.add(task.getRid());
        Collections.sort(rids2Lock);
        for (ORecordId rid : rids2Lock) reqContext.lock(rid, getRecordLock());
        for (OAbstractRecordReplicatedTask task : tasks) {
            final Object taskResult;
            // CHECK LOCAL CLUSTER IS AVAILABLE ON CURRENT SERVER
            if (!task.checkForClusterAvailability(iManager.getLocalNodeName(), dCfg))
                // SKIP EXECUTION BECAUSE THE CLUSTER IS NOT ON LOCAL NODE: THIS CAN HAPPENS IN CASE OF DISTRIBUTED TX WITH SHARDING
                taskResult = NON_LOCAL_CLUSTER;
            else {
                task.setLockRecords(false);
                task.checkRecordExists();
                taskResult = task.execute(requestId, iServer, iManager, database);
                reqContext.addUndoTask(task.getUndoTask(requestId));
            }
            result.results.add(taskResult);
        }
        database.commit();
        // SEND BACK CHANGED VALUE TO UPDATE
        for (int i = 0; i < result.results.size(); ++i) {
            final Object currentResult = result.results.get(i);
            if (currentResult == NON_LOCAL_CLUSTER)
                // SKIP IT
                continue;
            final OAbstractRecordReplicatedTask task = tasks.get(i);
            if (task instanceof OCreateRecordTask) {
                // SEND RID + VERSION
                final OCreateRecordTask t = (OCreateRecordTask) task;
                result.results.set(i, new OPlaceholder(t.getRecord()));
            } else if (task instanceof OUpdateRecordTask) {
                // SEND VERSION
                result.results.set(i, task.getRecord().getVersion());
            }
        }
        return result;
    } catch (Throwable e) {
        // if (e instanceof ODistributedRecordLockedException)
        // ddb.dumpLocks();
        ODistributedServerLog.debug(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.IN, "Rolling back transaction on local server db=%s (reqId=%s error=%s)...", database.getName(), requestId, e);
        database.rollback();
        // ddb.popTxContext(requestId);
        reqContext.unlock();
        if (!(e instanceof ONeedRetryException || e instanceof OTransactionException || e instanceof ORecordDuplicatedException || e instanceof ORecordNotFoundException))
            // DUMP ONLY GENERIC EXCEPTIONS
            ODistributedServerLog.info(this, getNodeSource(), null, DIRECTION.NONE, "Error on distributed transaction commit", e);
        return e;
    }
}
Also used : OPlaceholder(com.orientechnologies.orient.core.db.record.OPlaceholder) OAbstractRecordReplicatedTask(com.orientechnologies.orient.server.distributed.task.OAbstractRecordReplicatedTask) ArrayList(java.util.ArrayList) ORecordId(com.orientechnologies.orient.core.id.ORecordId) ONeedRetryException(com.orientechnologies.common.concur.ONeedRetryException) ORecordDuplicatedException(com.orientechnologies.orient.core.storage.ORecordDuplicatedException) OTransactionOptimistic(com.orientechnologies.orient.core.tx.OTransactionOptimistic) OTransactionException(com.orientechnologies.orient.core.exception.OTransactionException) ORecordNotFoundException(com.orientechnologies.orient.core.exception.ORecordNotFoundException)

Aggregations

ORecordDuplicatedException (com.orientechnologies.orient.core.storage.ORecordDuplicatedException)33 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)19 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)17 Test (org.junit.Test)10 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)8 OCommandScript (com.orientechnologies.orient.core.command.script.OCommandScript)6 ORecordNotFoundException (com.orientechnologies.orient.core.exception.ORecordNotFoundException)5 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)5 Vertex (com.tinkerpop.blueprints.Vertex)5 ONeedRetryException (com.orientechnologies.common.concur.ONeedRetryException)4 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)4 OrientGraph (com.tinkerpop.blueprints.impls.orient.OrientGraph)4 OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)4 ArrayList (java.util.ArrayList)4 Test (org.testng.annotations.Test)4 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)3 DatabaseAbstractTest (com.orientechnologies.DatabaseAbstractTest)2 OTransactionException (com.orientechnologies.orient.core.exception.OTransactionException)2 ORecordId (com.orientechnologies.orient.core.id.ORecordId)2 OrientTest (com.orientechnologies.orient.test.database.base.OrientTest)2