【问题标题】:OrientDB: Cannot change the schema while a transaction is active. Schema changes are not transactionalOrientDB:事务处于活动状态时无法更改架构。架构更改不是事务性的
【发布时间】:2016-08-23 14:48:47
【问题描述】:

我目前正在使用嵌入式模式的 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 实现时,一切都按预期工作。但是,我更喜欢使用事务图,以便能够回滚事务。

【问题讨论】:

  • 它告诉您架构更改不是事务性的。数据变化是。

标签: java transactions orientdb


【解决方案1】:

这是正确的 - Orient 中的架构更改不是事务性的。您应该使用非事务性操作来创建或修改架构,并使用事务性操作来更改图表的数据。

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

【讨论】:

  • 添加新顶点时,我究竟在哪里执行架构更改?此外,我已经尝试使用非事务图来初始设置架构,然后使用事务图来添加顶点。但是在添加多个顶点后,我得到了同样的异常。
【解决方案2】:

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");

并在这里检查: Create schema for graph database with Java in OrientDB

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-22
    • 2022-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-21
    相关资源
    最近更新 更多