初始提交:项目基础结构
- backend: FastAPI 后端服务 (Python) - frontend: React + Tauri 前端应用 - docker-compose.yml: 容器编排配置
This commit is contained in:
24
backend/app/core/database.py
Normal file
24
backend/app/core/database.py
Normal file
@ -0,0 +1,24 @@
|
||||
"""数据库连接 — 异步引擎 + 连接池"""
|
||||
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession
|
||||
from app.core.config import settings
|
||||
|
||||
engine = create_async_engine(
|
||||
settings.DATABASE_URL,
|
||||
echo=settings.DEBUG,
|
||||
pool_size=20, # 连接池常驻连接数
|
||||
max_overflow=10, # 超出 pool_size 时最多再创建的连接数
|
||||
pool_recycle=3600, # 连接回收时间(秒),防止 MySQL 8 小时断连
|
||||
pool_pre_ping=True, # 每次取出连接前先 ping 检测可用性
|
||||
)
|
||||
|
||||
AsyncSessionLocal = async_sessionmaker(
|
||||
engine,
|
||||
class_=AsyncSession,
|
||||
expire_on_commit=False,
|
||||
)
|
||||
|
||||
|
||||
async def get_db() -> AsyncSession:
|
||||
"""FastAPI 依赖注入:每次请求获取一个数据库会话"""
|
||||
async with AsyncSessionLocal() as session:
|
||||
yield session
|
||||
Reference in New Issue
Block a user