27 lines
No EOL
924 B
Python
27 lines
No EOL
924 B
Python
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker, Session
|
|
from sqlalchemy.pool import QueuePool
|
|
from .settings import settings
|
|
|
|
|
|
# Database setup with connection pooling
|
|
engine = create_engine(
|
|
settings.DATABASE_URL,
|
|
poolclass=QueuePool,
|
|
pool_size=10, # Number of connections to maintain in the pool
|
|
max_overflow=20, # Additional connections that can be created on demand
|
|
pool_timeout=30, # Timeout for getting connection from pool
|
|
pool_recycle=3600, # Recycle connections after 1 hour
|
|
pool_pre_ping=True, # Validate connections before use
|
|
echo=False # Set to True for SQL query logging
|
|
)
|
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
|
|
|
|
def get_db() -> Session:
|
|
"""Dependency to get database session"""
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close() |