Neo4j connector in python keeps mentioning the legacy warning for closing the session
Question:
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
Answers:
The warning is about the driver
not being closed, not the session
.
One way to address this is to create (and close) the driver inside run_query
:
class Neo4jConnector(DatabaseConnector):
def __init__(self, uri: str, user: str, password: str):
self._uri = uri
self._auth = (user, password)
def run_query(self, query: str, parameters: Optional[Dict[str, Any]] = None) -> List[Dict[str, Any]]:
with (
neo4j.GraphDatabase.driver(self._uri, auth=self._auth) as driver,
driver.session() as session
):
result = session.run(query, parameters)
logging.debug(f"The following query was sent: {query} with parameters {parameters}")
return result.data()
Or, if you are going to be performing multiple queries with the same connector instance, you should consider using the __enter__ and __exit__ pattern shown in this answer.
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
The warning is about the driver
not being closed, not the session
.
One way to address this is to create (and close) the driver inside run_query
:
class Neo4jConnector(DatabaseConnector):
def __init__(self, uri: str, user: str, password: str):
self._uri = uri
self._auth = (user, password)
def run_query(self, query: str, parameters: Optional[Dict[str, Any]] = None) -> List[Dict[str, Any]]:
with (
neo4j.GraphDatabase.driver(self._uri, auth=self._auth) as driver,
driver.session() as session
):
result = session.run(query, parameters)
logging.debug(f"The following query was sent: {query} with parameters {parameters}")
return result.data()
Or, if you are going to be performing multiple queries with the same connector instance, you should consider using the __enter__ and __exit__ pattern shown in this answer.