python
runtime_error
ai_generated
true
sqlalchemy.exc.InvalidRequestError: 此会话处于 'committed' 状态;无法在此事务中发出更多 SQL。
sqlalchemy.exc.InvalidRequestError: This session is in 'committed' state; no further SQL can be emitted within this transaction.
ID: python/sqlalchemy-invalid-request-state
80%修复率
87%置信度
0证据数
2024-05-20首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.x | active | — | — | — |
根因分析
在提交会话后尝试执行 SQL 查询,事务已关闭。
English
Attempting to execute SQL queries after committing the session, which closes the transaction.
解决方案
-
100% 成功率 Create a new session for subsequent operations
from sqlalchemy.orm import sessionmaker Session = sessionmaker(bind=engine) new_session = Session() new_session.query(User).all()
-
95% 成功率 Use session.begin_nested() for savepoints
with session.begin_nested(): session.add(new_user) session.commit()
无效尝试
常见但无效的做法:
-
Calling session.rollback() after commit
90% 失败
Rollback is not allowed after commit; the transaction is already finalized.
-
Using session.begin() again without a new session
85% 失败
The session is in a committed state; you need to create a new session or use begin_nested.