python
runtime_error
ai_generated
true
sqlalchemy.exc.PendingRollbackError: Can't reconnect until invalid transaction is rolled back. Please rollback() fully before proceeding
ID: python/sqlalchemy-missing-session-commit
80%Fix Rate
85%Confidence
0Evidence
2024-03-15First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.4.x | active | — | — | — |
| 2.0.x | active | — | — | — |
Root Cause
A previous SQL operation failed (e.g., constraint violation) but the session was not rolled back. Subsequent operations on the same session trigger this error because the transaction remains in a failed state.
generic中文
之前的 SQL 操作失败(如违反约束),但会话未回滚。后续在同一会话上的操作会触发此错误,因为事务仍处于失败状态。
Workarounds
-
95% success
Explicitly rollback the session after any exception: try: session.commit() except: session.rollback() raise
-
90% success
Use a context manager pattern: with session.begin(): # operations - automatically rolls back on exception
Dead Ends
Common approaches that don't work:
-
90% fail
close() does not reset the transaction state; it only releases connections. The next session usage still carries the failed transaction.
-
100% fail
The error is persistent until rollback is explicitly called; retrying the same failed operation doesn't clear the invalid transaction flag.