0

Sinking in big trouble,

Well can anyone tell me , how can i acquire write lock through cypher. Note : I will use REST APIs , So my cypher would in php.

EDITED : Scenario:

I am using Neo4j REST server and PHP to access it.

Now i have created a node say 'counter-node' which generates new user id. Logic is just add 1 to previous value.

Now If two users are coming simultaneously then first user read 'counter-node' value BUT before it can update it to 1 , second user read it . Thus value in 'counter-node' is not as expected.

Any Help

voila
  • 1,594
  • 2
  • 19
  • 38

2 Answers2

1

You don't need to acquire write locks explicitly. All nodes that you modify in a transaction are write-locked automatically.

So if you do this in your logic:

start tx
increment counter node
read the value of the counter node and set it on the user node as ID
commit tx

no two users will ever get the same ID.

Michal Bachman
  • 2,661
  • 17
  • 22
  • Hey thanks for correcting my concept and most importantly can you suggest me which neo4jPHP should I use. Any suggestions – voila Aug 28 '13 at 11:25
  • 1
    The problem with this Michal is that two threads can still read the old value at the same time and the write the **same** oldvalue + 1 to the property. So a write lock before the first read is required. Kind of indicating that you want to read/write as an atomic operation. – Michael Hunger Aug 28 '13 at 12:13
  • That's why I suggested the increment first, then read the value? Will that not solve the problem? – Michal Bachman Aug 28 '13 at 12:18
  • Nah, ok, I see what you're saying – Michal Bachman Aug 28 '13 at 12:19
  • @MichaelHunger: One thing is not clear, When i am writting something inside transaction then isn't it transaction that do all locking process .. Why I need to explicitly acquire read or write lock ? – voila Aug 28 '13 at 12:59
  • 1
    OK, thinking about this, my answer is plain wrong. I don't think there's a way to do this atomically in Cypher; is the only solution a Java plugin then? In that case, I should update the answer, so it doesn't confuse people... Michael? – Michal Bachman Aug 28 '13 at 13:22
  • also see http://stackoverflow.com/questions/18480505/what-does-transaction-mean-in-reference-with-neo4j-database/18486645#18486645 – Michal Bachman Aug 28 '13 at 13:48
1

The popular APOC plugin for Neo4j has a selection of explicit Locking procedures that can be called via Cypher such as call apoc.lock.nodes([nodes])

Learn more at neo4j-contrib.github.io/neo4j-apoc-procedures/#_locking

Note: as far as I can tell, this functionality doesn't exist natively in Cypher so APOC is probably your best bet.

John
  • 9,249
  • 5
  • 44
  • 76