File size: 1,430 Bytes
17af839
e22cec2
 
 
 
 
 
 
 
 
 
 
 
 
17af839
e22cec2
 
 
17af839
e22cec2
 
17af839
 
 
 
 
e22cec2
17af839
e22cec2
 
 
 
 
 
 
17af839
e22cec2
 
17af839
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# backend/app/core/graph_db.py
from neo4j import AsyncGraphDatabase, AsyncDriver
from app.core.config import settings
import logging

logger = logging.getLogger(__name__)

class Neo4jConnection:
    def __init__(self):
        self.driver: AsyncDriver | None = None
        self.connect()

    def connect(self):
        if not settings.NEO4J_URI or not settings.NEO4J_PASSWORD:
            logger.warning("Neo4j credentials missing. Graph features will be disabled.")
            return

        try:
            # Initialize the ASYNC driver with robust AuraDB pooling settings
            self.driver = AsyncGraphDatabase.driver(
                settings.NEO4J_URI,
                auth=(settings.NEO4J_USERNAME, settings.NEO4J_PASSWORD),
                keep_alive=True,
                max_connection_lifetime=30 * 60, # Recycle connections every 30 mins
                max_connection_pool_size=50,     # Prevent connection exhaustion
                connection_acquisition_timeout=2 * 60
            )
            logger.info("Successfully initialized Async Neo4j AuraDB driver with pooling!")
        except Exception as e:
            logger.error(f"Failed to connect to Neo4j: {str(e)}")
            self.driver = None

    async def close(self):
        if self.driver:
            await self.driver.close()
            logger.info("Neo4j driver connection closed.")

# Instantiate globally
neo4j_db = Neo4jConnection()