cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Python App Course: session.run() vs session.read_transaction()

Hello,

I an doing the Graph Academy Course for Python Apps. Setting the environmental variables and building the connection went well.

But now I am struggling with read_transaction: In the following code the variable res1 is None (so nothing is printed), while res2 contains the names of the proper actors.

#Create a new Driver instance
driver = GraphDatabase.driver(neo4j_uri, auth=(neo4j_username, neo4j_password))

#Execute within a Read Transaction vs execute with session.run()
with driver.session() as session:
    def get_movies(tx, title):
        return tx.run("""
            MATCH (p:Person)-[:ACTED_IN]->(m:Movie)
            WHERE m.title = $title 
            RETURN p.name AS name
            LIMIT 10
        """, title=title)    
    res1 = session.read_transaction(get_movies,title="Apollo 13")    
    for r in res1:   
        print(r["name"])

      
    res2 = session.run("""
            MATCH (p:Person)-[:ACTED_IN]->(m:Movie)
            WHERE m.title = $title 
            RETURN p.name AS name
            LIMIT 10
        """, title="Apollo 13")    
    for r in res2:
        print(r["name"])
        
    session.close()

Does anybody know where I am going wrong here?

Thanks in advance

1 REPLY 1

What's going wrong is that in the transaction function example (get_movies) you are passing the result object outside of the transaction function where it's no longer bound. In the session.run() case you're not using a transaction function.

Check this article: Neo4j Driver Best Practices - Neo4j Graph Data Platform and the heading in it titled " Process Database Results Within Your Transaction Function" and it will explain in detail what's happening here and why.

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

All the sessions of the conference are now available online