Add db.begin(), db.commit() and db.rollback() methods

Manual transaction control previously required mixing raw
db.execute("begin") strings with methods on db.conn. These thin
wrappers give the documentation and callers a single consistent
idiom. Docs updated to use them throughout.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UnLnhsH25Nnv7LHhekUfPd
This commit is contained in:
Claude 2026-07-04 22:31:22 +00:00
commit 788c393a30
No known key found for this signature in database
5 changed files with 75 additions and 6 deletions

View file

@ -455,6 +455,31 @@ class Database:
self.conn.rollback()
raise
def begin(self) -> None:
"""
Start a transaction with ``BEGIN``, taking manual control of transaction
handling. End it by calling :meth:`commit` or :meth:`rollback`.
Raises ``sqlite3.OperationalError`` if a transaction is already open.
Most code should use the :meth:`atomic` context manager instead, which
commits and rolls back automatically. See :ref:`python_api_transactions`.
"""
self.execute("BEGIN")
def commit(self) -> None:
"""
Commit the current transaction. Does nothing if no transaction is open.
"""
self.conn.commit()
def rollback(self) -> None:
"""
Roll back the current transaction, discarding its changes. Does nothing
if no transaction is open.
"""
self.conn.rollback()
@contextlib.contextmanager
def ensure_autocommit_off(self) -> Generator[None, None, None]:
"""