Question about how to explicitly acquire a read lock for a transaction

Hello!

I was wondering if I explicitly acquire a read lock then use the node, will the transaction drop the lock?

I tried running a test for this but for some reason when I tried to run more than 2 cypher queries in a transaction using neo4j sandbox, the transaction stalled on the third and didn't give me a reason.

Is the read lock on node (a: A) ever dropped during the transaction, after it is acquired in the explicit apoc.lock.read call?

def fn(tx):
    tx.run("MATCH (a: A) call apoc.lock.read.nodes([a]) RETURN NULL").consume()
    # POINT 1
    tx.run("MATCH (a: A) RETURN a.prop").consume()
   # POINT 3
   tx.run("call apoc.util.sleep(10000) RETURN NULL").consume()
   # POINT 4
   return TRUE

with driver.session() as session:
    session.read_transaction(fn)

Hi @dansolnik I think maybe this could help you Shared vs Exclusive Transaction locks - Knowledge Base

Thank you! I have already read through that page. That's actually what helped me come up with the test case.

I was unable to run the test case on the sandbox and I do not have access to enterprise edition of neo4j.

Specifically, I run the following code on my computer:

from neo4j import GraphDatabase, basic_auth


def fn(tx):
    tx.run("MATCH (m: E) call apoc.lock.read.nodes([a]) RETURN NULL").consume()
    print("point 1")
    tx.run("MATCH (m: E) RETURN m.prop").consume()
    print("point 2")
    tx.run("call apoc.util.sleep(30000) RETURN NULL").consume()
    print("point 3")
    return True


def main():
    print("STARTING!")
    neo4j = GraphDatabase.driver(
        location, 
        auth=basic_auth("neo4j", password))
    print("CONNECTED")
    with neo4j.session() as session:
        session.read_transaction(fn)
    print("DONE")

main()

And then I try to use the techniques on that page to find the locks held by the process. However, it seems like the process just never runs the third part of the transaction and just gets stuck after point 2 with no active query.

hence, I was unable to answer my question.

ok, but now is it working?

I just gave it another try and it worked this time! There must have been something wrong with the sandbox I had.

The answer, for all those curious, is that the lock is held throughout the whole transaction. It is only released when the transaction is over.

1 Like

Thank you for your help!

no problem, if there is anything that I could help you let me know