Search code examples
scalaneo4jneo4j-embedded

neo4j 3.0 embedded - no nodes


There's sometime I must be missing about neo4j 3.0 embedded. After creating a node, setting some properties, and marking the transaction as success. I then re-open the DB, but there are no nodes in it! What am I missing here? The neo4j documentation is pretty poor.

      val graph1 = {
        val graphDb = new GraphDatabaseFactory()
          .newEmbeddedDatabase(new File("/opt/neo4j/deviceGraphTest" ))
        val tx = graphDb.beginTx()
        val node = graphDb.createNode()
        node.setProperty("name", "kitchen island")
        node.setProperty("bulbType", "incandescent")
        tx.success()
        graphDb.shutdown()
      }

      val graph2 = {
        val graphDb2 = new GraphDatabaseFactory()
          .newEmbeddedDatabase(new File("/opt/neo4j/deviceGraphTest" ))
        val tx2 = graphDb2.beginTx()
        val allNodes = graphDb2.getAllNodes.iterator().toList
        allNodes.foreach(node => {
          printNode(node)
        })
      }

Solution

  • The transaction what you have opened has to be closed with the command tx.close() after setting the transaction to state success. I do not know the exact scala syntax but it would be good to put the full block into a try/catch and to finally close the transaction in the finally block. Here is the documentation for Java: https://neo4j.com/docs/java-reference/current/javadocs/org/neo4j/graphdb/Transaction.html