22
22
import logging
23
23
24
24
from neo4j import AsyncDriver , AsyncGraphDatabase , Record
25
+ from neo4j .exceptions import Neo4jError
25
26
26
27
from uns_graphql .graphql_config import GraphDBConfig
27
28
@@ -58,15 +59,18 @@ async def get_graphdb_driver(cls, retry: int = 0) -> AsyncDriver:
58
59
AsyncDriver: The Neo4j async driver.
59
60
"""
60
61
LOGGER .debug ("GraphDB driver requested" )
61
- if cls ._graphdb_driver is None :
62
- LOGGER .info ("Creating a new GraphDB driver" )
63
- cls ._graphdb_driver = AsyncGraphDatabase .driver (
64
- uri = GraphDBConfig .conn_url , auth = (GraphDBConfig .user , GraphDBConfig .password ), database = GraphDBConfig .database
65
- )
66
62
try :
63
+ if cls ._graphdb_driver is None :
64
+ LOGGER .info ("Creating a new GraphDB driver" )
65
+ cls ._graphdb_driver = AsyncGraphDatabase .driver (
66
+ uri = GraphDBConfig .conn_url ,
67
+ auth = (GraphDBConfig .user , GraphDBConfig .password ),
68
+ database = GraphDBConfig .database ,
69
+ )
70
+
67
71
await cls ._graphdb_driver .verify_connectivity ()
68
72
LOGGER .debug ("GraphDB driver connectivity verified" )
69
- except Exception as ex :
73
+ except Neo4jError as ex :
70
74
LOGGER .error ("Failed to verify GraphDB driver connectivity: %s" , str (ex ), stack_info = True , exc_info = True )
71
75
# In case of connectivity failure, close the existing driver and create a new one
72
76
await cls .release_graphdb_driver ()
@@ -91,7 +95,7 @@ async def release_graphdb_driver(cls):
91
95
try :
92
96
await cls ._graphdb_driver .close ()
93
97
LOGGER .info ("GraphDB driver closed successfully" )
94
- except Exception as ex :
98
+ except Neo4jError as ex :
95
99
LOGGER .error ("Error closing the GraphDB driver: %s" , str (ex ), stack_info = True , exc_info = True )
96
100
finally :
97
101
cls ._graphdb_driver = None
0 commit comments