HMdeveloper
HMdeveloper

Reputation: 2884

when create a node from java in neo4j I can not see the node in neo4j

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!!!!!!!!!!

Upvotes: 0

Views: 579

Answers (2)

Dave Hallam
Dave Hallam

Reputation: 136

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.

Upvotes: 1

Michael Hunger
Michael Hunger

Reputation: 41706

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.

Upvotes: 0

Related Questions