Search code examples
python-3.xneo4jbolt

Python Neo4j Driver write_transaction returning empty result


I am using Neo4j version 4.0.1, with python driver version 4.0 on python 3.7.2

My Problem is, that within a transaction, the result from database contains records, while it does not if returned to outside the transaction. In my opinion, with a normal MATCH() query it should not be a problem to issue a query and consume it out-of-transaction, if one does not mind the possibility of lost-updates occuring.

The following code produces a minimum example.

driver = GraphDatabase.driver(uri, auth=(user, password), encrypted=False)

def get_all_foo_tx(tx):
    result = tx.run("MATCH(n:Foo) RETURN n.id")
    # result can be iterated over here
    # for record in result: 
    #     print(record)  
    return result

def get_all_foo():
    session = driver.session()
    db_result = session.write_transaction(get_all_foo_tx)
    # result is empty here
    # for record in result: 
    #     print(record)  
    

My question is now: Is this expected behaviour?

With the previous Neo4j driver version, the code schematically outlined above worked, while it does not with the newest driver version. I have taken a look at the "Breaking Changes" section of the documentation, and to me was nothing that could explain the change in behaviour.

Is consuming results outside the transaction a "bad practice"?

Notes: The code I'm actually executing is way more complicated than this, this is a minimum example. The behaviour is consistent over a range of queries, where the result contains multiple records. I have a property named id, which is separate from the internal Neo4j id; this is confusing on first sight.


Solution

  • Maybe your problem is that you are using session.write_transaction, but you want to only read the data from Neo4j. write_transaction method is designed for write queries where you include CREATE, MERGE, or SET in cypher. Try to use:

    session = driver.session()
    db_result = session.run(get_all_foo_tx)
    // Just dont forget to close the session
    session.close()
    

    Hopefully, this will help.