2
votes

I want to copy multiple nodes and then delete the old ones simultaneously. I wrote a multithread java application for this purpose. My cypher query is as below:

String cypher= "MATCH (a:Person {surname:\""+ Thread.currentThread().getName()+"\"})
            create (b:Person) 
            set b.surname=a.surname
            SET b.version = '2'
            WITH a,b
            MATCH (a)-[r:has_indication]->(c)
            WITH COLLECT(r) AS rels, a, b, c
            FOREACH (rel in rels |
                   CREATE (b)-[r2:has_indication]->(c) 
                   SET r2+=rel
            )
            with a,b,c
            MATCH (c)-[r3:has_indication]->(a)
            WITH COLLECT(r3) AS rels2, a, b, c
            FOREACH (rel in rels2 |
                   CREATE (c)-[r3:has_indication]->(b) 
                   SET r3+=rel 
            ) 
            DETACH DELETE a " ;

and my java code is as below :

public void run() {
       Driver driver = GraphDatabase.driver(
              "bolt://localhost:7687", AuthTokens.basic("neo4j", "neo4j"));
       Session session = driver.session();
       session.runAsync( cypher);       
}

I use 3 thread , when the three nodes have relation with each other only one node get updated but when these node don't have relation with each other , all the three nodes got updated successfully. How can I update them even when they have relation with each other simultaneously? I am using neo4j enterprise version 3.4.7 .

2

2 Answers

1
votes

To avoid issues (e.g., deadlocks), you should avoid running concurrent neo4j queries that update the same nodes or relationships (or nodes on the same relationships). If you really need to run concurrent queries, you should try to run them on disjoint subgraphs.

Refer to the neo4j documentation on isolation levels, default locking, deadlocks, and delete semantics for more details.

1
votes

You can't run concurrent queries on the same nodes because deadlock happens. You can use semaphore to avoid multiple queries working in critical section.