I am trying to use scala over the embedded jave Neo4j api. I am having trouble opening the database for reading on subsequent occasions. The code below should create two nodes and an edge every time it runs, but return all of them at the 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 to reset the 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 it simply seems to give an empty database and then the 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 with exit code 0