database runtime_error ai_generated true

sqlite3.OperationalError: 无法回滚 - 没有活动的事务

sqlite3.OperationalError: cannot rollback - no transaction is active

ID: database/sqlite-cannot-rollback-no-savepoint

其他格式: JSON · Markdown 中文 · English
95%修复率
87%置信度
1证据数
2023-05-12首次发现

版本兼容性

版本状态引入弃用备注
SQLite 3.42.0 active
SQLite 3.43.2 active
SQLite 3.44.0 active

根因分析

发出了 ROLLBACK 命令,但没有对应的 BEGIN TRANSACTION,或者使用了不存在的保存点名称。

English

A ROLLBACK command was issued without a corresponding BEGIN TRANSACTION, or a savepoint name was used that does not exist.

generic

官方文档

https://www.sqlite.org/lang_transaction.html

解决方案

  1. Wrap the ROLLBACK in a try-except block and only execute it if a transaction is active. In Python: `try: cursor.execute('ROLLBACK') except sqlite3.OperationalError: pass`. Alternatively, use a context manager: `with connection: cursor.execute(...)` which auto-commits/rolls back.
  2. Review the code to ensure every ROLLBACK is paired with a BEGIN TRANSACTION. Use a connection wrapper that tracks transaction state (e.g., a flag `in_transaction` set to True on BEGIN and False on COMMIT/ROLLBACK).

无效尝试

常见但无效的做法:

  1. 60% 失败

    If the code already has a transaction, this creates nested transactions that may not be handled correctly, leading to 'cannot commit - no transaction is active' errors later.

  2. 90% 失败

    The WAL mode affects concurrency and crash recovery, not transaction management. It does not fix missing transactions.