Skip to content

Commit 7e3b6af

Browse files
miss-islingtonErlend Egeberg Aasland
and
Erlend Egeberg Aasland
authored
gh-94017: Improve clarity of sqlite3 transaction handling docs (GH-94320)
Co-authored-by: Alex Waygood <[email protected]> Co-authored-by: CAM Gerlach <[email protected]> (cherry picked from commit 760b8cf) Co-authored-by: Erlend Egeberg Aasland <[email protected]>
1 parent 5e24c80 commit 7e3b6af

File tree

1 file changed

+61
-39
lines changed

1 file changed

+61
-39
lines changed

Doc/library/sqlite3.rst

Lines changed: 61 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -360,14 +360,24 @@ Connection Objects
360360

361361
.. attribute:: isolation_level
362362

363-
Get or set the current default isolation level. :const:`None` for autocommit mode or
364-
one of "DEFERRED", "IMMEDIATE" or "EXCLUSIVE". See section
365-
:ref:`sqlite3-controlling-transactions` for a more detailed explanation.
363+
This attribute controls the :ref:`transaction handling
364+
<sqlite3-controlling-transactions>` performed by ``sqlite3``.
365+
If set to :const:`None`, transactions are never implicitly opened.
366+
If set to one of ``"DEFERRED"``, ``"IMMEDIATE"``, or ``"EXCLUSIVE"``,
367+
corresponding to the underlying `SQLite transaction behaviour`_,
368+
implicit :ref:`transaction management
369+
<sqlite3-controlling-transactions>` is performed.
370+
371+
If not overridden by the *isolation_level* parameter of :func:`connect`,
372+
the default is ``""``, which is an alias for ``"DEFERRED"``.
366373

367374
.. attribute:: in_transaction
368375

376+
This read-only attribute corresponds to the low-level SQLite
377+
`autocommit mode`_.
378+
369379
:const:`True` if a transaction is active (there are uncommitted changes),
370-
:const:`False` otherwise. Read-only attribute.
380+
:const:`False` otherwise.
371381

372382
.. versionadded:: 3.2
373383

@@ -695,21 +705,27 @@ Cursor Objects
695705

696706
.. method:: execute(sql[, parameters])
697707

698-
Executes an SQL statement. Values may be bound to the statement using
708+
Execute an SQL statement. Values may be bound to the statement using
699709
:ref:`placeholders <sqlite3-placeholders>`.
700710

701711
:meth:`execute` will only execute a single SQL statement. If you try to execute
702712
more than one statement with it, it will raise a :exc:`Warning`. Use
703713
:meth:`executescript` if you want to execute multiple SQL statements with one
704714
call.
705715

716+
If :attr:`~Connection.isolation_level` is not :const:`None`,
717+
*sql* is an ``INSERT``, ``UPDATE``, ``DELETE``, or ``REPLACE`` statement,
718+
and there is no open transaction,
719+
a transaction is implicitly opened before executing *sql*.
720+
706721

707722
.. method:: executemany(sql, seq_of_parameters)
708723

709-
Executes a :ref:`parameterized <sqlite3-placeholders>` SQL command
724+
Execute a :ref:`parameterized <sqlite3-placeholders>` SQL command
710725
against all parameter sequences or mappings found in the sequence
711-
*seq_of_parameters*. The :mod:`sqlite3` module also allows using an
726+
*seq_of_parameters*. It is also possible to use an
712727
:term:`iterator` yielding parameters instead of a sequence.
728+
Uses the same implicit transaction handling as :meth:`~Cursor.execute`.
713729

714730
.. literalinclude:: ../includes/sqlite3/executemany_1.py
715731

@@ -720,12 +736,13 @@ Cursor Objects
720736

721737
.. method:: executescript(sql_script)
722738

723-
This is a nonstandard convenience method for executing multiple SQL statements
724-
at once. It issues a ``COMMIT`` statement first, then executes the SQL script it
725-
gets as a parameter. This method disregards :attr:`isolation_level`; any
726-
transaction control must be added to *sql_script*.
739+
Execute multiple SQL statements at once.
740+
If there is a pending transaciton,
741+
an implicit ``COMMIT`` statement is executed first.
742+
No other implicit transaction control is performed;
743+
any transaction control must be added to *sql_script*.
727744

728-
*sql_script* can be an instance of :class:`str`.
745+
*sql_script* must be a :class:`string <str>`.
729746

730747
Example:
731748

@@ -1183,38 +1200,43 @@ This section shows recipes for common adapters and converters.
11831200
Controlling Transactions
11841201
------------------------
11851202

1186-
The underlying ``sqlite3`` library operates in ``autocommit`` mode by default,
1187-
but the Python :mod:`sqlite3` module by default does not.
1188-
1189-
``autocommit`` mode means that statements that modify the database take effect
1190-
immediately. A ``BEGIN`` or ``SAVEPOINT`` statement disables ``autocommit``
1191-
mode, and a ``COMMIT``, a ``ROLLBACK``, or a ``RELEASE`` that ends the
1192-
outermost transaction, turns ``autocommit`` mode back on.
1193-
1194-
The Python :mod:`sqlite3` module by default issues a ``BEGIN`` statement
1195-
implicitly before a Data Modification Language (DML) statement (i.e.
1196-
``INSERT``/``UPDATE``/``DELETE``/``REPLACE``).
1197-
1198-
You can control which kind of ``BEGIN`` statements :mod:`sqlite3` implicitly
1199-
executes via the *isolation_level* parameter to the :func:`connect`
1200-
call, or via the :attr:`isolation_level` property of connections.
1201-
If you specify no *isolation_level*, a plain ``BEGIN`` is used, which is
1202-
equivalent to specifying ``DEFERRED``. Other possible values are ``IMMEDIATE``
1203-
and ``EXCLUSIVE``.
1204-
1205-
You can disable the :mod:`sqlite3` module's implicit transaction management by
1206-
setting :attr:`isolation_level` to ``None``. This will leave the underlying
1207-
``sqlite3`` library operating in ``autocommit`` mode. You can then completely
1208-
control the transaction state by explicitly issuing ``BEGIN``, ``ROLLBACK``,
1209-
``SAVEPOINT``, and ``RELEASE`` statements in your code.
1210-
1211-
Note that :meth:`~Cursor.executescript` disregards
1212-
:attr:`isolation_level`; any transaction control must be added explicitly.
1203+
The ``sqlite3`` module does not adhere to the transaction handling recommended
1204+
by :pep:`249`.
1205+
1206+
If the connection attribute :attr:`~Connection.isolation_level`
1207+
is not :const:`None`,
1208+
new transactions are implicitly opened before
1209+
:meth:`~Cursor.execute` and :meth:`~Cursor.executemany` executes
1210+
``INSERT``, ``UPDATE``, ``DELETE``, or ``REPLACE`` statements.
1211+
Use the :meth:`~Connection.commit` and :meth:`~Connection.rollback` methods
1212+
to respectively commit and roll back pending transactions.
1213+
You can choose the underlying `SQLite transaction behaviour`_ —
1214+
that is, whether and what type of ``BEGIN`` statements ``sqlite3``
1215+
implicitly executes –
1216+
via the :attr:`~Connection.isolation_level` attribute.
1217+
1218+
If :attr:`~Connection.isolation_level` is set to :const:`None`,
1219+
no transactions are implicitly opened at all.
1220+
This leaves the underlying SQLite library in `autocommit mode`_,
1221+
but also allows the user to perform their own transaction handling
1222+
using explicit SQL statements.
1223+
The underlying SQLite library autocommit mode can be queried using the
1224+
:attr:`~Connection.in_transaction` attribute.
1225+
1226+
The :meth:`~Cursor.executescript` method implicitly commits
1227+
any pending transaction before execution of the given SQL script,
1228+
regardless of the value of :attr:`~Connection.isolation_level`.
12131229

12141230
.. versionchanged:: 3.6
12151231
:mod:`sqlite3` used to implicitly commit an open transaction before DDL
12161232
statements. This is no longer the case.
12171233

1234+
.. _autocommit mode:
1235+
https://www.sqlite.org/lang_transaction.html#implicit_versus_explicit_transactions
1236+
1237+
.. _SQLite transaction behaviour:
1238+
https://www.sqlite.org/lang_transaction.html#deferred_immediate_and_exclusive_transactions
1239+
12181240

12191241
Using :mod:`sqlite3` efficiently
12201242
--------------------------------

0 commit comments

Comments
 (0)