java - Opening existing embedded neo4j database -


i trying use scala on embedded jave neo4j api. having trouble opening database reading on subsequent occasions. code below should create 2 nodes , edge every time runs, return of them @ begining of each run. so, 0 nodes first time, 2 nodes second time, 4 third time etc.

import org.neo4j.tooling.globalgraphoperations import org.neo4j.graphdb.factory.graphdatabasefactory import org.neo4j.graphdb.relationshiptype  object tester extends app{    val db_path = "data/neo4j"   object knows extends relationshiptype {     override def name(): string = "knows"   }   val graphdb = new graphdatabasefactory().newembeddeddatabase(db_path)  //seems reset whole directory   println(graphdb)   try {     println("begin")     val tx = graphdb.begintx() // database operations go here     println(globalgraphoperations.at(graphdb).getallnodes.iterator)     val nodes = globalgraphoperations.at(graphdb).getallnodes.iterator     while (nodes.hasnext()) {       println(nodes.next())     }     nodes.close()     val relt = globalgraphoperations.at(graphdb).getallrelationships.iterator     while (relt.hasnext()) {       println(relt.next())     }     println("success - begin")     tx.success()   }   try {     val tx = graphdb.begintx() // database operations go here     val firstnode = graphdb.createnode     val secondnode = graphdb.createnode     val relationship = firstnode.createrelationshipto(secondnode, knows)      println(firstnode)     println(secondnode)     println(relationship)     println(relationship.gettype.name)     tx.success()     println("success")   }   println("end")   try {     val tx = graphdb.begintx() // database operations go here     println(globalgraphoperations.at(graphdb).getallnodes.iterator)     val nodes = globalgraphoperations.at(graphdb).getallnodes.iterator     while (nodes.hasnext()) {       println(nodes.next())     }     nodes.close()     val relt = globalgraphoperations.at(graphdb).getallrelationships.iterator     while (relt.hasnext()) {       println(relt.next())     }     println("success - end")     tx.success()   }   graphdb.shutdown() } 

however, every time seems give empty database , 2 new nodes. what's going on here?

embeddedgraphdatabase [data/neo4j] begin org.neo4j.tooling.globalgraphoperations$1$1@74c49a90 success - begin node[2] node[3] relationship[1] knows success end org.neo4j.tooling.globalgraphoperations$1$1@2ec0df08 node[2] node[3] relationship[1] success - end  process finished exit code 0 

this happening because not closing transaction. can calling tx.close(). think instantiating tx inside try not how should be. here working version of program:

import org.neo4j.tooling.globalgraphoperations import org.neo4j.graphdb.factory.graphdatabasefactory import org.neo4j.graphdb.relationshiptype  object tester extends app{    val db_path = "data/neo4j"   object knows extends relationshiptype {     override def name(): string = "knows"   }   val graphdb = new graphdatabasefactory().newembeddeddatabase(db_path)   println(graphdb)    val tx1 = graphdb.begintx() // database operations go here   try {     println("will list nodes")     println("1 - begin")     println("globalgraphoperations.at(graphdb).getallnodes.iterator")     val nodes = globalgraphoperations.at(graphdb).getallnodes.iterator     while (nodes.hasnext()) {       println(nodes.next())     }     nodes.close()     val relt = globalgraphoperations.at(graphdb).getallrelationships.iterator     while (relt.hasnext()) {       println(relt.next())     }     println("1 - success - begin")     tx1.success()   }   {     tx1.close()   }    val tx2 = graphdb.begintx() // database operations go here   try {     val firstnode = graphdb.createnode     val secondnode = graphdb.createnode     val relationship = firstnode.createrelationshipto(secondnode, knows)      println(firstnode)     println(secondnode)     println(relationship)     println(relationship.gettype.name)     tx2.success()     println("2 - success")   }   {     tx2.close()   }    println("2 - end")    val tx3 = graphdb.begintx() // database operations go here   try {     println(globalgraphoperations.at(graphdb).getallnodes.iterator)     val nodes = globalgraphoperations.at(graphdb).getallnodes.iterator     while (nodes.hasnext()) {       println(nodes.next())     }     nodes.close()     val relt = globalgraphoperations.at(graphdb).getallrelationships.iterator     while (relt.hasnext()) {       println(relt.next())     }     println("3 - success - end")     tx3.success()   }   {     tx3.close()   }   graphdb.shutdown() } 

extra

i tried bring program closer "scala-style". also, tried remove boilerplate , repeated code. accomplish i:

  • used javaconverters handle java collections , iterables handle them in scala
  • created method withtransaction automatic resource management our transaction in scala.

this result:

import org.neo4j.tooling.globalgraphoperations import org.neo4j.graphdb.factory.graphdatabasefactory import org.neo4j.graphdb.relationshiptype import org.neo4j.graphdb.transaction import scala.collection.javaconverters._  object tester extends app{   val db_path = "data/neo4j"   object knows extends relationshiptype {     override def name(): string = "knows"   }    def withtransaction (dowithtransaction: transaction => unit) {      val temptx = graphdb.begintx()       try {       dowithtransaction(temptx)     }     {       temptx.close()     }   }    val graphdb = new graphdatabasefactory().newembeddeddatabase(db_path)   println(graphdb)    withtransaction { tx =>      println("1 - begin")     val nodes = globalgraphoperations.at(graphdb).getallnodes      (node <- nodes.asscala)       println(node)      val relts = globalgraphoperations.at(graphdb).getallrelationships     (irelt <- relts.asscala)        println(irelt)      println("1 - success - begin")     tx.success()   }     withtransaction { tx =>      val firstnode = graphdb.createnode     val secondnode = graphdb.createnode     val relationship = firstnode.createrelationshipto(secondnode, knows)      println(firstnode)     println(secondnode)     println(relationship)     println(relationship.gettype.name)     tx.success()     println("2 - success")   }    println("2 - end")    withtransaction { tx =>      println(globalgraphoperations.at(graphdb).getallnodes.iterator)      val nodes = globalgraphoperations.at(graphdb).getallnodes      (node <- nodes.asscala)       println(node)      val relts = globalgraphoperations.at(graphdb).getallrelationships     (irelt <- relts.asscala)        println(irelt)      println("3 - success - end")     tx.success()   }    graphdb.shutdown() } 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -