0

I am new in neo4j and I am trying to learn,

I have the following java code for creating the node in neo4j and then I read the property name of created node in java,my code is as follow:

String DB_PATH = "C:/hamed";
 public static void main( String[] args )
{
    JavaQuery javaQuery = new JavaQuery();
    javaQuery.run();
}

void run()
{

    // START SNIPPET: addData
    GraphDatabaseService db = new GraphDatabaseFactory().newEmbeddedDatabase( DB_PATH );
    db.beginTx();
    try ( Transaction tx = db.beginTx(); )
    {
        Node myNode = db.createNode();
        myNode.addLabel(  DynamicLabel.label( "11" ) );
        myNode.setProperty( "name", "qq" );
        tx.success();

    }
    // END SNIPPET: addData

   // START SNIPPET: execute
    ExecutionEngine engine = new ExecutionEngine( db );

    ExecutionResult result;
    try ( Transaction ignored = db.beginTx() )
    {
        result = engine.execute( "match (n) return n, n.name" );
        // END SNIPPET: execute
        // START SNIPPET: items
        Iterator<Node> n_column = result.columnAs( "n" );
        for ( Node node : IteratorUtil.asIterable( n_column ) )
        {
            // note: we're grabbing the name property from the node,
            // not from the n.name in this case.
            nodeResult = node + ": " + node.getProperty( "name" );
            System.out.println("ss : "+nodeResult);
        }
        // END SNIPPET: items
          db.shutdown();
    }

and the system.out... prints ss : Node1: qq which is good,

Now when I run the neo4j as follow:

enter image description here

then I go to this link:

http://localhost:7474/webadmin/

and then when I write query to check nodes nothing returns:

enter image description here

But I expect to see one node with the name property of qq

can anyone help me? what am I doing wrong?

Update:

I undersood that code only removes all nodes from db. for example I created a node and check it with neo4j console : match (n) return n and the node returned but after running the code nothing returned which is very strange!!!!!!!!!!

2 Answers 2

1

All your db interactions are running inside a single transaction which is never success()'d so it rollsback when the app finishes.

It's caused by the additional db.beginTx() that you have placed immediately after the line that creates the db, i.e. the second line in this snippet:

GraphDatabaseService db = new GraphDatabaseFactory().newEmbeddedDatabase( DB_PATH );
db.beginTx();
try ( Transaction tx = db.beginTx(); )

Remove the db.beginTx(); line and you'll be fine.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes you are awesome buddy:)
0

Did you by chance not shut down your graph database in java while you were looking at the server? I didn't see any db.shutdown() in your code.

Only one process at a time can access the database directory.

2 Comments

Thanks for answering I added db.shutdown() and still the same problem:(
even before java access the database I should shut down the neo4j server which shows that java creating nodes in the same database and I should be able to see the node as a result but I can not... it is very bizzard

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.