如何在多线程环境下使用sequence

How to use sequence in multi-threaded enviroment

我尝试并行创建多个顶点:

public static void main(String[] args) throws InterruptedException {

    //create db and seq
    ODatabaseDocumentTx db = new ODatabaseDocumentTx("memory:/TestDB");
    db.create();
    OSequenceLibrary seqLib = db.getMetadata().getSequenceLibrary();
    seqLib.createSequence("testSeq",
      OSequence.SEQUENCE_TYPE.ORDERED,
      new OSequence.CreateParams().setStart(0L).setIncrement(1)
    );

    OrientGraphFactory factory = new OrientGraphFactory("memory:/TestDB", "admin", "admin").setupPool(1, 8);

    //mt
    Executor executor = Executors.newFixedThreadPool(8);

    CountDownLatch latch = new CountDownLatch(1000);
    for (int i = 1; i <= 1000; i++) {
        executor.execute(() -> {
            OrientGraph g = factory.getTx();
            try {
                OSequence seq = g.getRawGraph().getMetadata().getSequenceLibrary().getSequence("testSeq");
                OrientVertex v = g.addVertex("TestClass");
                v.setProperty("seq", seq.next());
                latch.countDown();
            } finally {
                g.shutdown();
            }
        });
    }
    latch.await(5, TimeUnit.SECONDS);
    System.exit(0);
}

并收到很多异常:

com.orientechnologies.orient.core.exception.OConcurrentModificationException: Cannot UPDATE the record #7:0 because the version is not the latest. Probably you are updating an old record or it has been modified by another user (db=v2 your=v1)

mt环境下如何正确使用sequence?

OrientDB 完全基于没有锁或很少锁的乐观方法。因此,您应该捕获异常并重试。示例:

OrientGraph g = factory.getTx();
try {
  for( int retry = 0; retry < 100; ++retry ){
    try {
      OSequence seq = g.getRawGraph().getMetadata().getSequenceLibrary().getSequence("testSeq");
      OrientVertex v = g.addVertex("TestClass");
      v.setProperty("seq", seq.next());
      latch.countDown();
      break;

    } catch( ONeedRetryException e ) {
    }
  }
} finally {
  g.shutdown();
}