Search code examples
orientdb

OrientDB multithread Concurrent Modification Exception and other errors


I am writing an application that writes data to a graph on orientDB (v 2.2.3) this graph is something like the following: enter image description here

I have threads that add vertices to C vertices each C vertex has an independent thread which is responsible add D vertexes with there edges.

Each thread is working on a separate transaction, I have been getting various errors and exception like the following:

com.orientechnologies.orient.core.exception.OStorageException: Error on commit
at      com.orientechnologies.orient.client.remote.OStorageRemote.baseNetworkOperation(OStorageRemote.java:253)
at com.orientechnologies.orient.client.remote.OStorageRemote.networkOperation(OStorageRemote.java:189)
at com.orientechnologies.orient.client.remote.OStorageRemote.commit(OStorageRemote.java:1271)
at com.orientechnologies.orient.core.tx.OTransactionOptimistic.doCommit(OTransactionOptimistic.java:549)
at com.orientechnologies.orient.core.tx.OTransactionOptimistic.commit(OTransactionOptimistic.java:109)
at com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx.commit(ODatabaseDocumentTx.java:2665)
at com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx.commit(ODatabaseDocumentTx.java:2634)
at com.tinkerpop.blueprints.impls.orient.OrientTransactionalGraph.commit(OrientTransactionalGraph.java:175)
at JSONManager$.commitGrap2(JSONManager.scala:371)
at JSONManager$$anonfun$main$2$$anon$1.run(JSONManager.scala:87)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
 .....
Caused by: java.util.ConcurrentModificationException
at java.util.LinkedHashMap$LinkedHashIterator.nextNode(LinkedHashMap.java:711)
at java.util.LinkedHashMap$LinkedValueIterator.next(LinkedHashMap.java:739)
at com.orientechnologies.orient.client.remote.OStorageRemote$28.execute(OStorageRemote.java:1284)
at com.orientechnologies.orient.client.remote.OStorageRemote$28.execute(OStorageRemote.java:1271)
at com.orientechnologies.orient.client.remote.OStorageRemote$2.execute(OStorageRemote.java:192)
at com.orientechnologies.orient.client.remote.OStorageRemote.baseNetworkOperation(OStorageRemote.java:224)
... 12 more

UPDATE code:

    val t: Runnable = new Runnable {
    override def run(): Unit = {
      graph = factory.getTx
      saveDUnits(dUnit, graph)
      commitGrap(graph)
      graph.shutdown()
    }
  };
  pool.execute(t)

def commitGrap(graph: OrientGraph): Unit = {
var retryCount = 0
while (retryCount < 10) {
  try {
    graph.commit()
    retryCount = 11
  } catch {
    case e: Exception => println("Commit Error")
      e.printStackTrace()
      var sleepTime = 50
      if (retryCount > 5) {
        sleepTime = 6000
      }
      Thread.sleep(sleepTime);
  } 
  retryCount = retryCount + 1
}

}


Solution

  • Finally I found the error what i did, the problem was in creating OrientGraphFactory instance, the non thread safe factory is created like the follwoing

    var factory: OrientGraphFactory = new OrientGraphFactory("remote:106.140.20.233/test", "root", "123")
    

    The thread safe factory is created like the following:

    var factory: OrientGraphFactory = new OrientGraphFactory("remote:106.140.20.233/test", "root", "123").setupPool(1, 20)
    

    I missed adding .setPool(1,20)

    That's it