当事务处于活动状态时,不能更改模式.模式更改不是事务性的



我目前正在用嵌入式模式的Java Graph API评估OrientDB,以便在未来的项目中使用。当在分布式设置中使用事务性图实现时,我观察到以下问题:

  1. 服务器启动和激活后,我通过OrientGraphFactory::factory.getTx()获得一个图形实例
  2. 在第一个节点上,我像下面这样设置了一个简单的模式:

    graph.getRawGraph().commit();
    if (graph.getVertexType("Person") == null)
        graph.createVertexType("Person");
    final OrientVertexType vtPerson = graph.getVertexType("Person");
    if (vtPerson.getProperty("firstName") == null)
        vtPerson.createProperty("firstName", OType.STRING);
    if (vtPerson.getProperty("lastName") == null)
        vtPerson.createProperty("lastName", OType.STRING);
    vtPerson.createEdgeProperty(Direction.OUT, "lives");
    vtPerson.createEdgeProperty(Direction.OUT, "history");
    vtPerson.setStrictMode(true);
    

  3. 我关闭第一个节点并重新启动它,然后我也启动第二个节点,启动后我获得(1)所描述的图形实例。
  4. 在第一个节点上,我尝试添加一个顶点:

    try {
        final OrientVertex person = graph.addVertex("class:Person", "firstName", firstName, "lastName", lastName);
        graph.getRawGraph().commit();
        return person;
    } catch (Exception e) {
        e.printStackTrace();
        graph.rollback();
    }
    

这时我得到OSchemaException:

com.orientechnologies.orient.core.exception.OSchemaException: Cannot change the schema while a transaction is active. Schema changes are not transactional
DB name="test"
at com.orientechnologies.orient.core.metadata.schema.OSchemaShared.saveInternal(OSchemaShared.java:1284)
at com.orientechnologies.orient.core.metadata.schema.OSchemaShared.releaseSchemaWriteLock(OSchemaShared.java:606)
at com.orientechnologies.orient.core.metadata.schema.OClassImpl.releaseSchemaWriteLock(OClassImpl.java:2028)
at com.orientechnologies.orient.core.metadata.schema.OClassImpl.releaseSchemaWriteLock(OClassImpl.java:2023)
at com.orientechnologies.orient.core.metadata.schema.OClassImpl.setClusterSelectionInternal(OClassImpl.java:2062)
at com.orientechnologies.orient.server.distributed.impl.ODistributedAbstractPlugin.propagateSchemaChanges(ODistributedAbstractPlugin.java:1439)
at com.orientechnologies.orient.server.distributed.impl.ODistributedStorage.checkForCluster(ODistributedStorage.java:1803)
at com.orientechnologies.orient.server.distributed.impl.ODistributedTransactionManager.checkForClusterIds(ODistributedTransactionManager.java:275)
at com.orientechnologies.orient.server.distributed.impl.ODistributedTransactionManager.commit(ODistributedTransactionManager.java:87)
at com.orientechnologies.orient.server.distributed.impl.ODistributedStorage.commit(ODistributedStorage.java:1240)
at com.orientechnologies.orient.core.tx.OTransactionOptimistic.doCommit(OTransactionOptimistic.java:569)
at com.orientechnologies.orient.core.tx.OTransactionOptimistic.commit(OTransactionOptimistic.java:109)
at com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx.commit(ODatabaseDocumentTx.java:2667)
at com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx.commit(ODatabaseDocumentTx.java:2637)

当使用OrientGraphNoTx实现时,一切都像预期的那样工作。但是,我更喜欢使用事务图,以便能够回滚事务。

这是正确的-在Orient中模式更改不是事务性的。您应该使用非事务性操作来创建或修改模式,并使用事务性操作来对图进行数据更改。

如果您需要执行模式更改作为业务逻辑的一部分,请首先运行它们,如果它们失败,则数据事务也会失败。需要注意的是,在某些情况下,如果您使用事务上下文执行模式更改,那么OrientDB将静默地提交正在进行的事务,因此请确保永远不要这样做。

Where exactly am I performing a schema change, when adding a new Vertex?

模式更改在这个if块中完成。createVertexType("Person")改变模式

if (graph.getVertexType("Person") == null)
    graph.createVertexType("Person");

当您像您提到的那样使用call factory.getTx()时,您正在使用事务。

OrientGraphFactory::factory.getTx()

相反,试一试

graph = new OrientGraphNoTx("your URL");

和检查这里:

使用Java在OrientDB中创建图形数据库模式

最新更新