The Neo4j Python driver's session object has a read_transaction method that you can use for marking read queries explicitly.
For Version 4 of the driver, the below example using the read_transaction function is correct:
def get_actors(tx, movie_name): # Define Unit of work
result = tx.run(
"MATCH (p:Person)-[:ACTED_IN]->(m) "
"WHERE toLower(m.title) CONTAINS toLower($title) "
"RETURN DISTINCT p.name as actorsTx",
title=movie_name) # Named parameters
return [record["actorsTx"] for record in result]
try:
with driver.session() as session:
# Run the unit of work within a Read Transaction
actors = session.read_transaction(get_actors, movie_name="matrix")
for actor in actors:
print(f"Found actor: {actor}")
except Exception as e:
print(f"Exception while running read query: {e}")
For Version 5 of the driver, the below example using the execute_read function is correct:
def get_actors(tx, movie_name): # Define Unit of work
result = tx.run(
"MATCH (p:Person)-[:ACTED_IN]->(m) "
"WHERE toLower(m.title) CONTAINS toLower($title) "
"RETURN DISTINCT p.name as actorsTx",
title=movie_name) # Named parameters
return [record["actorsTx"] for record in result]
try:
with driver.session() as session:
# Run the unit of work within a Read Transaction
actors = session.execute_read(get_actors, movie_name="matrix")
for actor in actors:
print(f"Found actor: {actor}")
except Exception as e:
print(f"Exception while running read query: {e}")
Comments
0 comments
Please sign in to leave a comment.