Orientdb 如何在多线程环境中使用序列

Orientdb 如何在多线程环境中使用序列,orientdb,Orientdb,我尝试并行创建多个顶点: public static void main(String[] args) throws InterruptedException { //create db and seq ODatabaseDocumentTx db = new ODatabaseDocumentTx("memory:/TestDB"); db.create(); OSequenceLibrary seqLib = db.getMetadata().getSequ

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

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);
}
publicstaticvoidmain(String[]args)抛出InterruptedException{
//创建数据库和seq
ODatabaseDocumentTx db=新的ODatabaseDocumentTx(“内存:/TestDB”);
db.create();
OSequenceLibrary seqLib=db.getMetadata().getSequenceLibrary();
seqLib.createSequence(“testSeq”,
OSequence.SEQUENCE\u TYPE.ORDERED,
新建OSequence.CreateParams().setStart(0L).setIncrement(1)
);
OrientGraphFactory=新的OrientGraphFactory(“内存:/TestDB”,“管理”,“管理”)。设置池(1,8);
//mt
Executor Executor=Executors.newFixedThreadPool(8);
倒计时闩锁=新倒计时闩锁(1000);
对于(int i=1;i{
OrientGraph g=factory.getTx();
试一试{
OSequence seq=g.getRawGraph().getMetadata().getSequenceLibrary().getSequence(“testSeq”);
OrientVertex v=g.addVertex(“TestClass”);
v、 setProperty(“seq”,seq.next());
倒计时();
}最后{
g、 关机();
}
});
}
等待(5,时间单位秒);
系统出口(0);
}
并收到许多例外情况:

com.orientechnologies.orient.core.exception.OConcurrentModificationException: 无法更新记录#7:0,因为该版本不是最新版本。 可能您正在更新旧记录,或者它已被 另一个用户(db=v2=v1)


如何在机器翻译环境中正确使用序列?

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();
}
OrientGraph g=factory.getTx();
试一试{
对于(int retry=0;retry<100;++重试){
试一试{
OSequence seq=g.getRawGraph().getMetadata().getSequenceLibrary().getSequence(“testSeq”);
OrientVertex v=g.addVertex(“TestClass”);
v、 setProperty(“seq”,seq.next());
倒计时();
打破
}捕获(ONeedRetryException e){
}
}
}最后{
g、 关机();
}

谢谢,顺序不明显。