Neo4j and Php handle counter within transaction

点点圈 提交于 2019-12-08 10:00:24

问题


Well scenario is like this:

I create a one node called counter node. Its initial value is 0 and incremented as user create its account on my website.

So there are three operation happen to operate this:

Read counter node value
Do some logic in php . Here like +1 to previous value of counter node
Write new value of counter node

Now problem is, If two or more users are coming exactly same time and creating such a condition that

Before first user write new value to counter node , it is being read by second user. Thus this will leave value of my 'counter node' in unstable condition.

Hope you got what I meant..

Any Solution ??

I am using neo4j 1.9.5 and php

Php Jadell :

https://github.com/jadell/Neo4jPHP‎

I heard of batch processing but not sure whether it will work. If any solution , Can you please give me a short example.

Thanks Amit Aggarwal


回答1:


You can't do that with the pure REST API. I would try it with Cypher, maybe something like:

START n=node(123)
SET n.noOfUsers = n.noOfUsers + 1
RETURN n.noOfUsers

This should work in the latest version of Cypher http://console.neo4j.org/?id=tnkldf




回答2:


Neo4j 2.0 has mandatory transactions. If you incremented your counter property noOfUsers in a transaction, I'd think that would help you with your concurrency issue.

Just a thought, but first a question: What's the purpose of the counter? Is it for assigning user IDs, or is it strictly informational? If the latter, must you have an exact count? Eg if you wanted the total number of twitter or facebook users, would it matter if the count was off by a few? If the count doesn't need to be exact (or exact at a particular instance in time), you could run a periodic process to return the count of user nodes, like:

MATCH n:User
return count(*)

This would also help you deal with deleted nodes.



来源:https://stackoverflow.com/questions/18449978/neo4j-and-php-handle-counter-within-transaction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!