Dr Joe
Dr Joe

Reputation: 760

Created nodes don't appear to be added to the neo4j database

I’ve created a couple of nodes in a neo4j database from within a groovy app, but when I connect to the database using the shell client they don’t appear to be there.

The database I’m creating is that described in 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()

After running the app, I can see that the database has been created on the filesystem, however when I connect from the shell client, it appears that there are no nodes in the database:

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

What might I be doing wrong?

Upvotes: 2

Views: 108

Answers (1)

Stefan Armbruster
Stefan Armbruster

Reputation: 39905

You didn't close the transaction. tx.success() just marks the transaction as being successful but won't commit it. For finishing a transaction use tx.close(). Best practice is to use try-with-resources blocks when doing Java - this cares about calling close() automatically.

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

Since your code has a def I assume you're using groovy, which does not support try-with-resources. Hence the code looks like:

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

Upvotes: 3

Related Questions