i have the following code in my class:
from neo4j import GraphDatabase
from typing import Dict, List, Optional, Tuple, Any
import re
from DatabaseConnector import DatabaseConnector
import logging
class Neo4jConnector(DatabaseConnector):
def __init__(self, uri: str, user: str, password: str):
self._driver = GraphDatabase.driver(uri, auth=(user, password))
def run_query(self, query: str, parameters: Optional[Dict[str, Any]] = None) -> List[Dict[str, Any]]:
with self._driver.session() as session:
result = session.run(query, parameters)
logging.debug(f"The following query was sent: {query} with parameters {parameters}")
return result.data()
but when u run it, i get the following error message:
usr/local/python/3.10.4/lib/python3.10/site-packages/neo4j/_sync/driver.py:457: DeprecationWarning: Relying on Driver's destructor to close the session is deprecated. Please make sure to close the session. Use it as a context (`with` statement) or make sure to call `.close()` explicitly. Future versions of the driver will not close drivers automatically.
I tried:
- enclosing the run in a with statement (see code above)
- closing the driver manually, which resulted in another error: "neo4j.exceptions.ServiceUnavailable: Failed to read from closed connection"
- closing the session manually (which is useless I suppose as it is within the with statement...)
def run_query(self, query: str, parameters: Optional[Dict[str, Any]] = None) -> List[Dict[str, Any]]:
with self._driver.session() as session:
result = session.run(query, parameters)
logging.debug(f"The following query was sent: {query} with parameters {parameters}")
data = result.data()
session.close()
return data
as this is the only place I am using the driver to connect and run queries to neo4j, this is confusing to say the least