# sqlalchemy.exc.OperationalError：(psycopg2.OperationalError) SSL 连接意外关闭

- **ID:** `python/sqlalchemy-connection-pool-recycle`
- **领域:** python
- **类别:** network_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

数据库服务器因超时关闭了空闲连接；连接池仍持有过时连接。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

1. **Set pool_recycle to a value less than server idle timeout.** (95% 成功率)
   ```
   engine = create_engine(url, pool_recycle=3600)  # Recycle connections every hour
   ```
2. **Enable pool_pre_ping to check connection health.** (90% 成功率)
   ```
   engine = create_engine(url, pool_pre_ping=True)
   ```

## 无效尝试

- **Disabling SSL entirely.** — Security risk; may not be allowed by policy. (50% 失败率)
- **Increasing pool_timeout.** — Does not refresh stale connections. (70% 失败率)
