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);
}
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);
}
}
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);
}
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);
}
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;
}
}
Aggregations