考虑下面的代码:
private void testMMAPIinLab() {
OrientDB orientDB = new OrientDB("remote:localhost",OrientDBConfig.defaultConfig());
OrientDBConfigBuilder poolCfg = OrientDBConfig.builder();
poolCfg.addConfig(OGlobalConfiguration.DB_POOL_MIN, 5);
poolCfg.addConfig(OGlobalConfiguration.DB_POOL_MAX, 10);
//poolCfg.addConfig(OGlobalConfiguration.RID_BAG_EMBEDDED_TO_SBTREEBONSAI_THRESHOLD, -1);
ODatabasePool dbPool = new ODatabasePool(orientDB,"lab", "root", "toor", poolCfg.build());
ODatabaseSession db = dbPool.acquire();
db.begin();
System.out.println("creando el vértice....");
OVertex v1 = db.newVertex();
v1.save();
System.out.println("save rid: "+v1.getIdentity().toString());
OVertex v2 = db.newVertex();
v2.save();
System.out.println("v2 save rid: "+v2.getIdentity().toString());
System.out.println("crear un edge.");
OEdge oe = v1.addEdge(v2);
v1.save();
System.out.println("llamando a commit...");
db.commit();
db.close();
System.out.println("configuración grabada:");
System.out.println("v1: "+v1.getIdentity().toString());
System.out.println("v2: "+v2.getIdentity().toString());
System.out.println("edge: "+oe.getFrom()+" --> "+oe.getTo());
// abrir otra transacción
db = dbPool.acquire();
db.begin();
System.out.println("crear v3");
OVertex v3 = db.newVertex();
v3.save();
System.out.println("v3 save rid: "+v3.getIdentity().toString());
System.out.println("modificar v1...");
v1.setProperty("value", "test");
System.out.println("borrar relación con v2");
// borrar el edge anterior
Iterator<OEdge> toRemove = v1.getEdges(ODirection.OUT).iterator();
while (toRemove.hasNext()) {
OEdge removeEdge = toRemove.next();
//removeEdge = (OEdge) edge;
removeEdge.delete();
removeEdge.save();
}
System.out.println("agregar una relación de v1 a v3");
OEdge oe2 = v1.addEdge(v3);
v1.save();
// crera en edge nuevo a v3
db.commit();
System.out.println("v1 edges: "+v1.getEdges(ODirection.OUT));
System.out.println("v3 post-commit rid: "+v3.getIdentity().toString());
System.out.println("oe2: "+oe2.getFrom()+" --> "+oe2.getTo());
db.close();
}
当你运行它的时候,你会得到带有两个外边的V1。一个包含已移除边的EdgeRID,另一个指向V3。如果您循环删除的边,它会显示{}并报告404错误。顶点被持久化,因此错误发生在数据库内部。
错误出在移除边缘的那段时间。如果你使用边引用,它可以工作,但在真实的代码中,我不知道顶点有多少边。
V2和V3具有正确的IN引用。
我该怎么解决这个问题呢?
发布于 2020-09-24 11:02:43
这取决于顶点在不同数据库会话之间共享的事实。
一个简单的修复方法是在第二个会话中使用顶点之前按ID重新加载它们:
// abrir otra transacción
db = dbPool.acquire();
db.begin();
//RELOAD THE VERTEX BEFORE USING IT AGAIN!
v1 = db.load(v1.getIdentity());
https://stackoverflow.com/questions/64037033
复制