Search code examples
neo4jtransactionslockingcypherblocking

Does Neo4j locking strategy block a user from accessing a node that is relocated at the same instant?


In order to keep track of the history of changes to a contract, I am growing a tail off the current version, which is being created at each change. A change (renewal of contract) involves deleting old relationships (not deleting any node), inserting the new current version node, and creating new relationships. I assume this all happens in a single transaction. If a client tries to access the current version the moment that change is happening, will the client request...

  1. fail to find the node or its contents?
  2. find the previous version of the contract node?
  3. find the new version of the contract node?

In the example code below we have a coach and an athlete who renew their joint contract at the beginning of each year. If you run the first query once, and the second query several times it will build this model. I don't know how to conclusively test this race-condition scenario.

contract history tail

//INITIALIZE FIRST CONTRACT
CREATE (coach:PERSON {name:'coach'})
CREATE (athlete:PERSON {name:'athlete'})
CREATE (curr:CONTRACT {name:'contract', year:2017, content: "The signees herein..."})
MERGE (coach)-[:BOUND_TO]->(curr)<-[:BOUND_TO]-(athlete)


//RENEW CONTRACT
MATCH (coach:PERSON {name:'coach'} )
MATCH (athlete:PERSON {name:'athlete'} )
MATCH (coach)-[r1:BOUND_TO]->(curr)<-[r2:BOUND_TO]-(athlete)
MERGE (coach)-[:BOUND_TO]->(new:CONTRACT {name:'contract', year:curr.year + 1, content: "The signees herein..."})<-[:BOUND_TO]-(athlete)
MERGE (new)-[:PREV]->(curr)
DELETE r1,r2

Solution

  • Neo4j is a full ACID database, so your question will depend of when the commit is realized :

    • before the commit : users will find the previous version
    • after the commit : users will find the new version