[Neo] Question about "Neo mistakes" wiki page: What's wrong with the synchronization example?
johan at neotechnology.com
Wed Feb 10 13:23:58 CET 2010
On Wed, Feb 10, 2010 at 10:48 AM, Thomas Andersson
<greddbullen at gmail.com> wrote:
> // don't do this
> synchronized void methodA()
> nodeA.setProperty( "prop1", 1 );
> synchronized void methodB()
> nodeB.setProperty( "prop2", 2 );
> According to the test, "The code above is very deadlock prone when
> methods A and B are called concurrently.".
> Exactly why is that? I try to understand what the situation is that
> should be avoided, but when I run the code in my head I can't figure
> out when and why the deadlock occurs.
All Neo4j API operations must be invoked within a transaction and the
modifying operations (such as Node.setProperty) will synchronize
access between concurrent transactions (on a node and relationship
level). I updated the wiki with a link to
http://wiki.neo4j.org/content/Transactions#Isolation that explains
this a bit better.
To give you a deadlock scenario with the code above:
- tx1 modifies nodeB
- tx2 calls methodB but blocks on nodeB.setProperty
- tx1 calls methodA and will then block when trying to call methodB
- deadlock that can not be detected by the Neo4j kernel
More information about the User