【问题标题】:Created nodes don't appear to be added to the neo4j database创建的节点似乎没有添加到 neo4j 数据库中
【发布时间】:2014-10-28 07:51:40
【问题描述】:

我在一个 groovy 应用程序中的 neo4j 数据库中创建了几个节点,但是当我使用 shell 客户端连接到数据库时,它们似乎并不存在。

我正在创建的数据库是 http://neo4j.com/docs/stable/tutorials-java-embedded-hello-world.html 中描述的:

def graphDb = new GraphDatabaseFactory().newEmbeddedDatabase("/tmp/foo.db");

Transaction tx = graphDb.beginTx()
def firstNode = graphDb.createNode();
firstNode.setProperty("message", "Hello, ");
def secondNode = graphDb.createNode();
secondNode.setProperty("message", "World!");

tx.success();

System.err.print(firstNode.getProperty("message"));
System.err.print(relationship.getProperty("message"));
System.err.print(secondNode.getProperty("message"));

graphDb.shutdown()

运行应用程序后,我可以看到文件系统上已经创建了数据库,但是当我从 shell 客户端连接时,数据库中似乎没有节点:

$ ./neo4j-community-2.1.5/bin/neo4j-shell -path /tmp/foo.db/ -v
neo4j-sh (?)$ match (m) return m;
+---+
| m |
+---+
+---+
0 row

我可能做错了什么?

【问题讨论】:

  • 你使用的是同一个版本的 neo4j 吗?
  • 是的,我相信是的。我在 groovy 代码中使用的是早期版本,但后来 shell 客户端抱怨它不会打开旧数据库。
  • 我看不出为什么它不应该工作:(
  • 是的,我就是这么想的。我想知道我是否需要做任何其他事情来使数据库刷新。此外,shutdown() 似乎需要几秒钟才能运行......这看起来很奇怪。

标签: neo4j


【解决方案1】:

您没有关闭交易。 tx.success() 只是将事务标记为成功但不会提交。要完成交易,请使用tx.close()。最佳实践是在执行 Java 时使用 try-with-resources 块 - 这关心自动调用 close()

GraphDatabaseService graphDb = ...;
try (Transaction tx = graphDb.beginTx()) { 
    // do stuff
    tx.success();  
}

由于您的代码有 def,我假设您使用的是不支持 try-with-resources 的 groovy。因此代码如下所示:

def graphDb = ....
Transaction tx = graphDb.beginTx()
try {
    // do stuff e.g. create nodes
    tx.success()
} finally {
    tx.close()
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2011-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-10
  • 2020-10-26
相关资源
最近更新 更多