# SQLAlchemy + Neon: make the pool survive idle suspends

## The trap
Neon puts computes into an idle state and closes connections after 5 minutes of inactivity. SQLAlchemy keeps pooled connections open longer than that by default, so the first query after a quiet period fails on a dead connection. Agents blame Neon; it is pool config.

## The rule (from Neon's SQLAlchemy guide)
Set both of these on your engine:
- `pool_pre_ping=True`: checks the connection is alive before each checkout, transparently reconnecting if Neon closed it.
- `pool_recycle`: set to a value at or below your compute's scale-to-zero idle timeout, so connections recycle before Neon kills them.

```python
from sqlalchemy import create_engine
engine = create_engine(
    DATABASE_URL,
    pool_pre_ping=True,
    pool_recycle=280,  # below the 5-minute (300s) default idle timeout
)
```

## Checklist
- `pool_pre_ping=True` is the single highest-value setting; add it first.
- `pool_recycle` in seconds, at or under your scale-to-zero setting.
- If you changed the scale-to-zero timeout in the Console, update `pool_recycle` to match.
- Schema migrations: run them on the direct connection string, same pooled-vs-direct rule as every other framework.