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=20, # 增加连接池大小 max_overflow=40 # 允许溢出更多连接 ) SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) Base = declarative_base() def get_db(): db = SessionLocal() try: yield db finally: db.close()