编程语言
首页 > 编程语言> > java.lang.IllegalStateException: The vertex or type is not associated with this transaction

java.lang.IllegalStateException: The vertex or type is not associated with this transaction

作者:互联网

经过

给字段建索引时,报异常。
java.lang.IllegalStateException: The vertex or type is not associated with this transaction [person_name]

代码

StandardJanusGraph standardGraphFactory = GraphUtil.getStandardGraphFactory();
        JanusGraphManagement mgmt = standardGraphFactory.openManagement();
        VertexLabel person = mgmt.getVertexLabel("person");
        PropertyKey name = mgmt.getPropertyKey("person_name");
        mgmt = standardGraphFactory.openManagement();
        mgmt.buildIndex("person_name_index", Vertex.class).addKey(name).buildCompositeIndex();
        mgmt.commit();
        GraphTraversalSource g = standardGraphFactory.traversal();
        g.addV("person").property("birthDate", new Date(), "person_name", "limin");
        g.tx().commit();

异常信息

java.lang.IllegalStateException: The vertex or type is not associated with this transaction [person_name]

	at org.janusgraph.graphdb.transaction.StandardJanusGraphTx.verifyAccess(StandardJanusGraphTx.java:287)
	at org.janusgraph.graphdb.transaction.StandardJanusGraphTx.verifyWriteAccess(StandardJanusGraphTx.java:279)
	at org.janusgraph.graphdb.transaction.StandardJanusGraphTx.addEdge(StandardJanusGraphTx.java:701)
	at org.janusgraph.graphdb.transaction.StandardJanusGraphTx.addSchemaEdge(StandardJanusGraphTx.java:903)
	at org.janusgraph.graphdb.database.management.ManagementSystem.addSchemaEdge(ManagementSystem.java:231)
	at org.janusgraph.graphdb.database.management.ManagementSystem.createCompositeIndex(ManagementSystem.java:691)
	at org.janusgraph.graphdb.database.management.ManagementSystem.access$300(ManagementSystem.java:79)
	at org.janusgraph.graphdb.database.management.ManagementSystem$IndexBuilder.buildCompositeIndex(ManagementSystem.java:759)
	at cn.haizhi.bigdata.graph.client.DataInsertTest.testInsertDateData(DataInsertTest.java:59)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

问题排查

原因是建索引的时候,用的不是首次standardGraphFactory.openManagement()打开的事务,所以会报这个异常。只需要把后面的代码mgmt = standardGraphFactory.openManagement();注释掉就OK了。
即:

```java
StandardJanusGraph standardGraphFactory = GraphUtil.getStandardGraphFactory();
        JanusGraphManagement mgmt = standardGraphFactory.openManagement();
        VertexLabel person = mgmt.getVertexLabel("person");
        PropertyKey name = mgmt.getPropertyKey("person_name");
        mgmt.buildIndex("person_name_index", Vertex.class).addKey(name).buildCompositeIndex();
        mgmt.commit();
        GraphTraversalSource g = standardGraphFactory.traversal();
        g.addV("person").property("birthDate", new Date(), "person_name", "limin");
        g.tx().commit();
汐朔 发布了38 篇原创文章 · 获赞 18 · 访问量 21万+ 私信 关注

标签:lang,ParentRunner,transaction,java,person,org,associated,junit,runners
来源: https://blog.csdn.net/arlanhon/article/details/104474530