# sqlalchemy.exc.TimeoutError: QueuePool 大小 5、溢出 10 已达上限，连接超时 30.00 秒。

- **ID:** `python/fastapi-db-session-not-closed`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

依赖项 yield 了数据库会话但异常时未关闭，连接泄漏直至连接池耗尽。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 0.100+ | active | — | — |

## 解决方案

1. **** (97% 成功率)
   ```
   def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()
   ```
2. **** (96% 成功率)
   ```
   def get_db():
    with SessionLocal() as db:
        yield db
   ```

## 无效尝试

- **** — Delays exhaustion; underlying leak persists. (80% 失败率)
- **** — Detects dead connections, does not free leaked ones. (85% 失败率)
