CoolFace
Apppublic

ashnaali22/phase-3-h2

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
connection.py31 linesDownload Raw Back to database
1from sqlalchemy import create_engine2from sqlalchemy.orm import sessionmaker3from sqlalchemy.pool import QueuePool4import os5from dotenv import load_dotenv6 7load_dotenv()8DATABASE_URL = os.getenv("DATABASE_URL")9 10# Use QueuePool for better connection management in long-running MCP server11# QueuePool maintains a pool of connections and reuses them12engine = create_engine(13    DATABASE_URL,14    poolclass=QueuePool,15    pool_size=5,              # Maintain 5 connections in pool16    max_overflow=10,          # Allow up to 10 additional connections17    pool_recycle=3600,        # Recycle connections after 1 hour18    pool_pre_ping=True,       # Verify connections before use19    echo=False,20    connect_args={"connect_timeout": 30}21)22 23SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)24 25def get_db():26    db = SessionLocal()27    try:28        yield db29    finally:30        db.close()31