Hi all,
looking to get some guidance here, I have a jupyter notebook that i'm using to try to query the neo4j cluster directly. I get the following error when I attempt to run it.
Passing user/pw/uri as a variable below.
ResultConsumedError: The result is out of scope. The associated transaction has been closed. Results can only be used while the transaction is open.
neo4j version: 4.4.23
os: Ventura 13.4.1 (22F82)
misc: dataspell version 2023.1.3, neo4j running on local machine.
Any help would be appreciated!
code below:
class NeoDB(object):
def __init__(self):
self._parse_config()
self._connect()
def _parse_config(self):
self._neo4j_uri = os.environ['NEO4J_URI']
self._neo4j_user = os.environ['NEO4J_USER']
self._neo4j_password = os.environ['NEO4J_SECRETS_PASSWORD']
def _connect(self):
self._driver = GraphDatabase.driver(self._neo4j_uri,
auth=(self._neo4j_user, self._neo4j_password))
print('Neo4J Client instantiated: {}'.format(self._neo4j_uri))
@staticmethod
Can you share the query and how you consume the results?
That is where the error is happening.
Thanks for that information.
Do you know where that error is thrown? Or can you help us narrow that down?
David,
the error is thrown after I attempt to run this part of the code:
r = ReportBuilder()
r.report_aws_inventory()
I have a query.json file that has a preset built report that I can call, and it calls that report and attempts to run it based on tags assigned to the queries. I can provide here if required but heres a sample snippet of one of the queries:
{
"name": "aws_accounts",
"tags": [
"inventory",
"security",
"cloud",
"aws"
],
"description": "List of AWSAccounts currently analysed",
"query": "MATCH (a:AWSAccount) RETURN ",
"return": "a.id, a.name",
"result_headers": [
"account_id",
"account_name"
]
},
Hi 
The problem lies within _exec_query
. You execute a query within a transaction tx.run(...)
which gives you back a Result
. This Result
is really just a cursor and doesn't contain the actual query results yet. That cursor becomes invalid the moment the transaction ends, which happens when returning from _exec_query
.
To fix your code, you need to consume the result within the transaction function. There are plenty of options. Here is one suggestion:
def _exec_query(tx, query, kwargs):
if kwargs:
result = tx.run(query, **kwargs)
else:
result = tx.run(query)
return list(result)
Other options are result.to_eager_result
, result.consume
, result.data
and more.
1 Like
Rouven,
Thank you, that resolved my issue for me, appreciate the assist! I'll sift through more of the documentation to see if I want to use any of the other suggested options.
1 Like