运行Hello-World嵌入式neo4j的问题

时间:2015-02-27 13:19:50

标签: java neo4j

在嵌入式neo4j中运行Hello-World需要帮助会导致出现此错误消息

Exception in thread "main" org.neo4j.graphdb.NotInTransactionException
    at org.neo4j.kernel.impl.persistence.PersistenceManager.getResource(PersistenceManager.java:228)    at org.neo4j.kernel.impl.persistence.PersistenceManager.getResource(PersistenceManager.java:228)
    at org.neo4j.kernel.impl.persistence.PersistenceManager.nodeCreate(PersistenceManager.java:155)
    at org.neo4j.kernel.impl.core.NodeManager.createNode(NodeManager.java:254)
    at org.neo4j.kernel.EmbeddedGraphDbImpl.createNode(EmbeddedGraphDbImpl.java:305)
    at org.neo4j.kernel.EmbeddedGraphDatabase.createNode(EmbeddedGraphDatabase.java:105)
    at twiapp.embeded.main(embeded.java:37)

代码如下

public static void main(String args[]) { 
  graphDb = new EmbeddedGraphDatabase(path);
   registerShutdownHook(graphDb);
   Transaction tx = graphDb.beginTx(); 
  try {
      firstNode = graphDb.createNode(); 
      firstNode.setProperty("message", "Hello, "); 
      secondNode = graphDb.createNode();
      secondNode.setProperty("message", "World!");
      relationship = firstNode.createRelationshipTo(secondNode, RelTypes.KNOWS);
     relationship.setProperty("message", "brave Neo4j ");
      tx.success();
     } finally {
          tx.finish();
        }
     }

Java结果:1

1 个答案:

答案 0 :(得分:0)

在手册中尝试一下。 Wrap operations in a transaction

try ( Transaction tx = graphDb.beginTx() )
{
  firstNode = graphDb.createNode(); 
  firstNode.setProperty("message", "Hello, "); 
  secondNode = graphDb.createNode();
  secondNode.setProperty("message", "World!");
  relationship = firstNode.createRelationshipTo(secondNode, RelTypes.KNOWS);
  relationship.setProperty("message", "brave Neo4j ");
  tx.success();
}