Skip to content

Commit 91460c0

Browse files
pythongh-94017: Improve clarity of sqlite3 transaction handling docs
1 parent 71868a0 commit 91460c0

File tree

1 file changed

+56
-38
lines changed

1 file changed

+56
-38
lines changed

Doc/library/sqlite3.rst

Lines changed: 56 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,14 @@ Connection Objects
406406

407407
.. attribute:: isolation_level
408408

409-
Get or set the current default isolation level. :const:`None` for autocommit mode or
410-
one of "DEFERRED", "IMMEDIATE" or "EXCLUSIVE". See section
411-
:ref:`sqlite3-controlling-transactions` for a more detailed explanation.
409+
Get or set the current default isolation level.
410+
Set to :const:`None` to disable implicit transaction handling,
411+
or one of "", "DEFERRED", "IMMEDIATE" or "EXCLUSIVE".
412+
Defaults to the former (`""`), unless overridden at :func:`connect`,
413+
using the *isolation_level* parameter.
414+
"" and "DEFERRED" carry the same meaning;
415+
they both imply deferred isolation level.
416+
See :ref:`sqlite3-controlling-transactions` for more details.
412417

413418
.. attribute:: in_transaction
414419

@@ -868,21 +873,27 @@ Cursor Objects
868873

869874
.. method:: execute(sql[, parameters])
870875

871-
Executes an SQL statement. Values may be bound to the statement using
876+
Execute an SQL statement. Values may be bound to the statement using
872877
:ref:`placeholders <sqlite3-placeholders>`.
873878

874879
:meth:`execute` will only execute a single SQL statement. If you try to execute
875880
more than one statement with it, it will raise a :exc:`ProgrammingError`. Use
876881
:meth:`executescript` if you want to execute multiple SQL statements with one
877882
call.
878883

884+
If :attr:`isolation_level` is not :const:`None`,
885+
*sql* is an INSERT, UPDATE, DELETE, or REPLACE statement,
886+
and there is no open transaction,
887+
``sqlite3`` will implicitly open a new transaction.
888+
879889

880890
.. method:: executemany(sql, seq_of_parameters)
881891

882-
Executes a :ref:`parameterized <sqlite3-placeholders>` SQL command
892+
Execute a :ref:`parameterized <sqlite3-placeholders>` SQL command
883893
against all parameter sequences or mappings found in the sequence
884-
*seq_of_parameters*. The :mod:`sqlite3` module also allows using an
894+
*seq_of_parameters*. The ``sqlite3`` module also allows using an
885895
:term:`iterator` yielding parameters instead of a sequence.
896+
Uses the same implicit transaction handling as :meth:`~Cursor.execute`.
886897

887898
.. literalinclude:: ../includes/sqlite3/executemany_1.py
888899

@@ -893,12 +904,13 @@ Cursor Objects
893904

894905
.. method:: executescript(sql_script)
895906

896-
This is a nonstandard convenience method for executing multiple SQL statements
897-
at once. It issues a ``COMMIT`` statement first, then executes the SQL script it
898-
gets as a parameter. This method disregards :attr:`isolation_level`; any
899-
transaction control must be added to *sql_script*.
907+
Execute multiple SQL statements at once.
908+
If there is a pending transaciton,
909+
an implicit ``COMMIT`` statement is executed first.
910+
This method disregards :attr:`isolation_level`;
911+
any transaction control must be added to *sql_script*.
900912

901-
*sql_script* can be an instance of :class:`str`.
913+
*sql_script* must be a :class:`string <str>`.
902914

903915
Example:
904916

@@ -1425,33 +1437,39 @@ This section shows recipes for common adapters and converters.
14251437
Controlling Transactions
14261438
------------------------
14271439

1428-
The underlying ``sqlite3`` library operates in ``autocommit`` mode by default,
1429-
but the Python :mod:`sqlite3` module by default does not.
1430-
1431-
``autocommit`` mode means that statements that modify the database take effect
1432-
immediately. A ``BEGIN`` or ``SAVEPOINT`` statement disables ``autocommit``
1433-
mode, and a ``COMMIT``, a ``ROLLBACK``, or a ``RELEASE`` that ends the
1434-
outermost transaction, turns ``autocommit`` mode back on.
1435-
1436-
The Python :mod:`sqlite3` module by default issues a ``BEGIN`` statement
1437-
implicitly before a Data Modification Language (DML) statement (i.e.
1438-
``INSERT``/``UPDATE``/``DELETE``/``REPLACE``).
1439-
1440-
You can control which kind of ``BEGIN`` statements :mod:`sqlite3` implicitly
1441-
executes via the *isolation_level* parameter to the :func:`connect`
1442-
call, or via the :attr:`isolation_level` property of connections.
1443-
If you specify no *isolation_level*, a plain ``BEGIN`` is used, which is
1444-
equivalent to specifying ``DEFERRED``. Other possible values are ``IMMEDIATE``
1445-
and ``EXCLUSIVE``.
1446-
1447-
You can disable the :mod:`sqlite3` module's implicit transaction management by
1448-
setting :attr:`isolation_level` to ``None``. This will leave the underlying
1449-
``sqlite3`` library operating in ``autocommit`` mode. You can then completely
1450-
control the transaction state by explicitly issuing ``BEGIN``, ``ROLLBACK``,
1451-
``SAVEPOINT``, and ``RELEASE`` statements in your code.
1452-
1453-
Note that :meth:`~Cursor.executescript` disregards
1454-
:attr:`isolation_level`; any transaction control must be added explicitly.
1440+
The ``sqlite3`` module does not adhere to the transaction handling recommended
1441+
by PEP 249.
1442+
Instead of keeping a transaction open and requiring the user to use the
1443+
:meth:`~Connection.commit` and :meth:`~Connection.rollback` methods,
1444+
``sqlite3`` only implicitly opens new transactions before
1445+
:meth:`~Cursor.execute` and :meth:`~Cursor.executemany` executes any of the
1446+
following statements:
1447+
1448+
* INSERT
1449+
* UPDATE
1450+
* DELETE
1451+
* REPLACE
1452+
1453+
In addition, any pending transaction is implicitly committed in
1454+
:meth:`~Cursor.executescript`, before execution of the given SQL script.
1455+
No other implicit transaction handling is performed.
1456+
1457+
You can control which kind of ``BEGIN`` statements ``sqlite3`` implicitly
1458+
executes via the :attr:`isolation_level` connection attribute.
1459+
1460+
The ``sqlite3`` module lets the user choose bypass its transaction handling, by
1461+
setting :attr:`isolation_level` to :const:`None`.
1462+
This leaves the underlying SQLite library in autocommit mode,
1463+
but also allows the user to perform any transaction handling using explicit SQL
1464+
statements.
1465+
The SQLite library autocommit mode can be queried using the
1466+
:attr:`in_transaction` connection attribute.
1467+
1468+
.. note::
1469+
1470+
PEP 249's autocommit concept must not be mistaken for SQLite's autocommit
1471+
mode.
1472+
Though related, they are different concepts with different semantics.
14551473

14561474
.. versionchanged:: 3.6
14571475
:mod:`sqlite3` used to implicitly commit an open transaction before DDL

0 commit comments

Comments
 (0)