Spaces:
Sleeping
Sleeping
CRITICAL PERFORMANCE FIX: Database connection optimization - Increased SQLAlchemy connection pool from 5 to 20 - Added connection recycling (5 min TTL) - Made SupabaseAuth singleton to reuse connections - Changed audit logging from commit() to flush() to batch writes - Added comprehensive performance fix documentation See PERFORMANCE_FIX.md for CRITICAL step: Must update DATABASE_URL to use Supabase connection pooler (port 6543)
54e6861
| """ | |
| Database Session Management | |
| """ | |
| from sqlalchemy import create_engine | |
| from sqlalchemy.ext.declarative import declarative_base | |
| from sqlalchemy.orm import sessionmaker | |
| from app.config import settings | |
| # Create database engine (Supabase PostgreSQL) | |
| # IMPORTANT: Use connection pooler (port 6543) for better performance | |
| # Format: postgresql://postgres:password@db.xxxxx.supabase.co:6543/postgres | |
| engine = create_engine( | |
| settings.DATABASE_URL, | |
| pool_pre_ping=True, | |
| pool_size=20, # Increased for better concurrency | |
| max_overflow=20, # Allow burst traffic | |
| pool_recycle=300, # Recycle connections every 5 minutes | |
| pool_timeout=30, # Wait 30s for connection from pool | |
| echo=settings.DEBUG | |
| ) | |
| # Create session factory | |
| SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) | |
| # Base class for ORM models | |
| Base = declarative_base() | |