Skip to content

Commit 37a47b1

Browse files
miss-islingtonErlend Egeberg Aasland
andauthored
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 0187b60 commit 37a47b1

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
@@ -396,14 +396,24 @@ Connection Objects
396396

397397
.. attribute:: isolation_level
398398

399-
Get or set the current default isolation level. :const:`None` for autocommit mode or
400-
one of "DEFERRED", "IMMEDIATE" or "EXCLUSIVE". See section
401-
:ref:`sqlite3-controlling-transactions` for a more detailed explanation.
399+
This attribute controls the :ref:`transaction handling
400+
<sqlite3-controlling-transactions>` performed by ``sqlite3``.
401+
If set to :const:`None`, transactions are never implicitly opened.
402+
If set to one of ``"DEFERRED"``, ``"IMMEDIATE"``, or ``"EXCLUSIVE"``,
403+
corresponding to the underlying `SQLite transaction behaviour`_,
404+
implicit :ref:`transaction management
405+
<sqlite3-controlling-transactions>` is performed.
406+
407+
If not overridden by the *isolation_level* parameter of :func:`connect`,
408+
the default is ``""``, which is an alias for ``"DEFERRED"``.
402409

403410
.. attribute:: in_transaction
404411

412+
This read-only attribute corresponds to the low-level SQLite
413+
`autocommit mode`_.
414+
405415
:const:`True` if a transaction is active (there are uncommitted changes),
406-
:const:`False` otherwise. Read-only attribute.
416+
:const:`False` otherwise.
407417

408418
.. versionadded:: 3.2
409419

@@ -858,21 +868,27 @@ Cursor Objects
858868

859869
.. method:: execute(sql[, parameters])
860870

861-
Executes an SQL statement. Values may be bound to the statement using
871+
Execute an SQL statement. Values may be bound to the statement using
862872
:ref:`placeholders <sqlite3-placeholders>`.
863873

864874
:meth:`execute` will only execute a single SQL statement. If you try to execute
865875
more than one statement with it, it will raise a :exc:`ProgrammingError`. Use
866876
:meth:`executescript` if you want to execute multiple SQL statements with one
867877
call.
868878

879+
If :attr:`~Connection.isolation_level` is not :const:`None`,
880+
*sql* is an ``INSERT``, ``UPDATE``, ``DELETE``, or ``REPLACE`` statement,
881+
and there is no open transaction,
882+
a transaction is implicitly opened before executing *sql*.
883+
869884

870885
.. method:: executemany(sql, seq_of_parameters)
871886

872-
Executes a :ref:`parameterized <sqlite3-placeholders>` SQL command
887+
Execute a :ref:`parameterized <sqlite3-placeholders>` SQL command
873888
against all parameter sequences or mappings found in the sequence
874-
*seq_of_parameters*. The :mod:`sqlite3` module also allows using an
889+
*seq_of_parameters*. It is also possible to use an
875890
:term:`iterator` yielding parameters instead of a sequence.
891+
Uses the same implicit transaction handling as :meth:`~Cursor.execute`.
876892

877893
.. literalinclude:: ../includes/sqlite3/executemany_1.py
878894

@@ -883,12 +899,13 @@ Cursor Objects
883899

884900
.. method:: executescript(sql_script)
885901

886-
This is a nonstandard convenience method for executing multiple SQL statements
887-
at once. It issues a ``COMMIT`` statement first, then executes the SQL script it
888-
gets as a parameter. This method disregards :attr:`isolation_level`; any
889-
transaction control must be added to *sql_script*.
902+
Execute multiple SQL statements at once.
903+
If there is a pending transaciton,
904+
an implicit ``COMMIT`` statement is executed first.
905+
No other implicit transaction control is performed;
906+
any transaction control must be added to *sql_script*.
890907

891-
*sql_script* can be an instance of :class:`str`.
908+
*sql_script* must be a :class:`string <str>`.
892909

893910
Example:
894911

@@ -1415,38 +1432,43 @@ This section shows recipes for common adapters and converters.
14151432
Controlling Transactions
14161433
------------------------
14171434

1418-
The underlying ``sqlite3`` library operates in ``autocommit`` mode by default,
1419-
but the Python :mod:`sqlite3` module by default does not.
1420-
1421-
``autocommit`` mode means that statements that modify the database take effect
1422-
immediately. A ``BEGIN`` or ``SAVEPOINT`` statement disables ``autocommit``
1423-
mode, and a ``COMMIT``, a ``ROLLBACK``, or a ``RELEASE`` that ends the
1424-
outermost transaction, turns ``autocommit`` mode back on.
1425-
1426-
The Python :mod:`sqlite3` module by default issues a ``BEGIN`` statement
1427-
implicitly before a Data Modification Language (DML) statement (i.e.
1428-
``INSERT``/``UPDATE``/``DELETE``/``REPLACE``).
1429-
1430-
You can control which kind of ``BEGIN`` statements :mod:`sqlite3` implicitly
1431-
executes via the *isolation_level* parameter to the :func:`connect`
1432-
call, or via the :attr:`isolation_level` property of connections.
1433-
If you specify no *isolation_level*, a plain ``BEGIN`` is used, which is
1434-
equivalent to specifying ``DEFERRED``. Other possible values are ``IMMEDIATE``
1435-
and ``EXCLUSIVE``.
1436-
1437-
You can disable the :mod:`sqlite3` module's implicit transaction management by
1438-
setting :attr:`isolation_level` to ``None``. This will leave the underlying
1439-
``sqlite3`` library operating in ``autocommit`` mode. You can then completely
1440-
control the transaction state by explicitly issuing ``BEGIN``, ``ROLLBACK``,
1441-
``SAVEPOINT``, and ``RELEASE`` statements in your code.
1442-
1443-
Note that :meth:`~Cursor.executescript` disregards
1444-
:attr:`isolation_level`; any transaction control must be added explicitly.
1435+
The ``sqlite3`` module does not adhere to the transaction handling recommended
1436+
by :pep:`249`.
1437+
1438+
If the connection attribute :attr:`~Connection.isolation_level`
1439+
is not :const:`None`,
1440+
new transactions are implicitly opened before
1441+
:meth:`~Cursor.execute` and :meth:`~Cursor.executemany` executes
1442+
``INSERT``, ``UPDATE``, ``DELETE``, or ``REPLACE`` statements.
1443+
Use the :meth:`~Connection.commit` and :meth:`~Connection.rollback` methods
1444+
to respectively commit and roll back pending transactions.
1445+
You can choose the underlying `SQLite transaction behaviour`_ —
1446+
that is, whether and what type of ``BEGIN`` statements ``sqlite3``
1447+
implicitly executes –
1448+
via the :attr:`~Connection.isolation_level` attribute.
1449+
1450+
If :attr:`~Connection.isolation_level` is set to :const:`None`,
1451+
no transactions are implicitly opened at all.
1452+
This leaves the underlying SQLite library in `autocommit mode`_,
1453+
but also allows the user to perform their own transaction handling
1454+
using explicit SQL statements.
1455+
The underlying SQLite library autocommit mode can be queried using the
1456+
:attr:`~Connection.in_transaction` attribute.
1457+
1458+
The :meth:`~Cursor.executescript` method implicitly commits
1459+
any pending transaction before execution of the given SQL script,
1460+
regardless of the value of :attr:`~Connection.isolation_level`.
14451461

14461462
.. versionchanged:: 3.6
14471463
:mod:`sqlite3` used to implicitly commit an open transaction before DDL
14481464
statements. This is no longer the case.
14491465

1466+
.. _autocommit mode:
1467+
https://www.sqlite.org/lang_transaction.html#implicit_versus_explicit_transactions
1468+
1469+
.. _SQLite transaction behaviour:
1470+
https://www.sqlite.org/lang_transaction.html#deferred_immediate_and_exclusive_transactions
1471+
14501472

14511473
Using :mod:`sqlite3` efficiently
14521474
--------------------------------

0 commit comments

Comments
 (0)