from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, declarative_base
from app.core.config import settings

# SQLAlchemy 2.0 style
engine = create_engine(
    settings.DATABASE_URI,
    pool_pre_ping=True,
    echo=False,
    pool_size=100,          # 基础连接池大小（支持更高并发）
    max_overflow=200,        # 允许溢出更多连接（最大300个并发连接）
    pool_recycle=3600        # 连接回收时间（1小时，防止MySQL连接超时）
)

SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

Base = declarative_base()

def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()
