Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
gh-83638: Add sqlite3.Connection.autocommit for PEP 249 compliant behaviour#93823
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
10d3a656ca9045577267606609f38b6b866a604a57cbc104141b898cb11ebd2d3009d4fb986155e4626ea8e3b9ed46e9bec2e2f1a3bf3ee1757f70ba5ef40cae37fbb341e0dd77651c00ac782c7743f346628e30060bd3ac96adbfa7c7c4cf1c5843491426e05879a997ad734bf3a85760f72247f645ee88fb2b94deed17f3a9e372981a6ba5b7864fd6659a52e37366e9207359fbd29894fccefff43074ac0fa064c6482e4947d05981536527e6a3697b65a429987476122be6902738c37ebb52e9083cb944a90bce921f78e04f0bd91e349d66daad4c0583a3aa9aac9b05ffd5e33f10cf022a50093722c9b117de3bd4efca9c567de6bf3e0fa134e0bedf6e33946acd155054feefa1720ac11f7c2ae488e14207b878308322683aca1e22File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -258,7 +258,8 @@ Module functions | ||
| .. function:: connect(database, timeout=5.0, detect_types=0, \ | ||
| isolation_level="DEFERRED", check_same_thread=True, \ | ||
| factory=sqlite3.Connection, cached_statements=128, \ | ||
| uri=False) | ||
| uri=False, \*, \ | ||
| autocommit=sqlite3.LEGACY_TRANSACTION_CONTROL) | ||
| Open a connection to an SQLite database. | ||
| @@ -290,11 +291,12 @@ Module functions | ||
| By default (``0``), type detection is disabled. | ||
| :param isolation_level: | ||
| The :attr:`~Connection.isolation_level` of the connection, | ||
| controlling whether and how transactions are implicitly opened. | ||
| See :attr:`Connection.isolation_level` and | ||
| :ref:`sqlite3-transaction-control-isolation-level` for more information. | ||
| Can be ``"DEFERRED"`` (default), ``"EXCLUSIVE"`` or ``"IMMEDIATE"``; | ||
| or ``None`` to disable opening transactions implicitly. | ||
| See :ref:`sqlite3-controlling-transactions` for more. | ||
| Has no effect unless :attr:`Connection.autocommit` is set to | ||
| :data:`~sqlite3.LEGACY_TRANSACTION_CONTROL` (the default). | ||
| :type isolation_level: str | None | ||
| :param bool check_same_thread: | ||
| @@ -321,6 +323,14 @@ Module functions | ||
| The query string allows passing parameters to SQLite, | ||
| enabling various :ref:`sqlite3-uri-tricks`. | ||
| :param autocommit: | ||
| See :attr:`Connection.autocommit` and | ||
| :ref:`sqlite3-transaction-control-autocommit` for more information. | ||
| *autocommit* currently defaults to | ||
| :data:`~sqlite3.LEGACY_TRANSACTION_CONTROL`. | ||
| The default will change to ``False`` in a future Python release. | ||
| :type autocommit: bool | ||
| :rtype: Connection | ||
| .. audit-event:: sqlite3.connect database sqlite3.connect | ||
| @@ -335,6 +345,9 @@ Module functions | ||
| .. versionadded:: 3.10 | ||
| The ``sqlite3.connect/handle`` auditing event. | ||
| .. versionadded:: 3.12 | ||
| The *autocommit* parameter. | ||
| .. function:: complete_statement(statement) | ||
| Return ``True`` if the string *statement* appears to contain | ||
| @@ -418,6 +431,12 @@ Module functions | ||
| Module constants | ||
| ^^^^^^^^^^^^^^^^ | ||
| .. data:: LEGACY_TRANSACTION_CONTROL | ||
| Set :attr:`~Connection.autocommit` to this constant to select | ||
| old style (pre-Python 3.12) transaction control behaviour. | ||
erlend-aasland marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| See :ref:`sqlite3-transaction-control-isolation-level` for more information. | ||
| .. data:: PARSE_COLNAMES | ||
erlend-aasland marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| Pass this flag value to the *detect_types* parameter of | ||
| @@ -616,18 +635,27 @@ Connection objects | ||
| .. method:: commit() | ||
| Commit any pending transaction to the database. | ||
| If there is no open transaction, this method is a no-op. | ||
| If :attr:`autocommit` is ``True``, or there is no open transaction, | ||
erlend-aasland marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| this method does nothing. | ||
| If :attr:`!autocommit` is ``False``, a new transaction is implicitly | ||
| opened if a pending transaction was committed by this method. | ||
erlend-aasland marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| .. method:: rollback() | ||
| Roll back to the start of any pending transaction. | ||
| If there is no open transaction, this method is a no-op. | ||
| If :attr:`autocommit` is ``True``, or there is no open transaction, | ||
| this method does nothing. | ||
| If :attr:`!autocommit` is ``False``, a new transaction is implicitly | ||
| opened if a pending transaction was rolled back by this method. | ||
erlend-aasland marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| .. method:: close() | ||
| Close the database connection. | ||
| Any pending transaction is not committed implicitly; | ||
| make sure to :meth:`commit` before closing | ||
| If :attr:`autocommit` is ``False``, | ||
erlend-aasland marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| any pending transaction is implicitly rolled back. | ||
| If :attr:`!autocommit` is ``True`` or :data:`LEGACY_TRANSACTION_CONTROL`, | ||
| no implicit transaction control is executed. | ||
| Make sure to :meth:`commit` before closing | ||
erlend-aasland marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| to avoid losing pending changes. | ||
| .. method:: execute(sql, parameters=(), /) | ||
| @@ -1224,6 +1252,38 @@ Connection objects | ||
| .. versionadded:: 3.11 | ||
| .. attribute:: autocommit | ||
| This attribute controls :pep:`249`-compliant transaction behaviour. | ||
| :attr:`!autocommit` has three allowed values: | ||
| * ``False``: Select :pep:`249`-compliant transaction behaviour, | ||
| implying that :mod:`!sqlite3` ensures a transaction is always open. | ||
| Use :meth:`commit` and :meth:`rollback` to close transactions. | ||
| This is the recommended value of :attr:`!autocommit`. | ||
| * ``True``: Use SQLite's `autocommit mode`_. | ||
erlend-aasland marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| :meth:`commit` and :meth:`rollback` have no effect in this mode. | ||
erlend-aasland marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| * :data:`LEGACY_TRANSACTION_CONTROL`: | ||
| Pre-Python 3.12 (non-:pep:`249`-compliant) transaction control. | ||
erlend-aasland marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| See :attr:`isolation_level` for more details. | ||
| This is currently the default value of :attr:`!autocommit`. | ||
| Changing :attr:`!autocommit` to ``False`` will open a new transaction, | ||
| and changing it to ``True`` will commit any pending transaction. | ||
| See :ref:`sqlite3-transaction-control-autocommit` for more details. | ||
| .. note:: | ||
| The :attr:`isolation_level` attribute has no effect unless | ||
| :attr:`autocommit` is :data:`LEGACY_TRANSACTION_CONTROL`. | ||
erlend-aasland marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| .. versionadded:: 3.12 | ||
| .. attribute:: in_transaction | ||
| This read-only attribute corresponds to the low-level SQLite | ||
| @@ -1236,17 +1296,24 @@ Connection objects | ||
| .. attribute:: isolation_level | ||
| This attribute controls the :ref:`transaction handling | ||
| <sqlite3-controlling-transactions>` performed by :mod:`!sqlite3`. | ||
| Controls the :ref:`legacy transaction handling mode | ||
| <sqlite3-transaction-control-isolation-level>` of :mod:`!sqlite3`. | ||
| If set to ``None``, transactions are never implicitly opened. | ||
| If set to one of ``"DEFERRED"``, ``"IMMEDIATE"``, or ``"EXCLUSIVE"``, | ||
| corresponding to the underlying `SQLite transaction behaviour`_, | ||
| implicit :ref:`transaction management | ||
| <sqlite3-controlling-transactions>` is performed. | ||
| :ref:`implicit transaction management | ||
| <sqlite3-transaction-control-isolation-level>` is performed. | ||
| If not overridden by the *isolation_level* parameter of :func:`connect`, | ||
| the default is ``""``, which is an alias for ``"DEFERRED"``. | ||
erlend-aasland marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| .. note:: | ||
| Using :attr:`autocommit` to control transaction handling is | ||
| recommended over using :attr:`!isolation_level`. | ||
| :attr:`!isolation_level` has no effect unless :attr:`autocommit` is | ||
| set to :data:`LEGACY_TRANSACTION_CONTROL` (the default). | ||
| .. attribute:: row_factory | ||
| A callable that accepts two arguments, | ||
| @@ -1375,7 +1442,9 @@ Cursor objects | ||
| :meth:`executescript` if you want to execute multiple SQL statements with one | ||
| call. | ||
| If :attr:`~Connection.isolation_level` is not ``None``, | ||
| If :attr:`~Connection.autocommit` is | ||
| :data:`LEGACY_TRANSACTION_CONTROL`, | ||
| :attr:`~Connection.isolation_level` is not ``None``, | ||
| *sql* is an ``INSERT``, ``UPDATE``, ``DELETE``, or ``REPLACE`` statement, | ||
| and there is no open transaction, | ||
| a transaction is implicitly opened before executing *sql*. | ||
| @@ -1403,7 +1472,9 @@ Cursor objects | ||
| .. method:: executescript(sql_script, /) | ||
| Execute the SQL statements in *sql_script*. | ||
| If there is a pending transaction, | ||
| If the :attr:`~Connection.autocommit` is | ||
| :data:`LEGACY_TRANSACTION_CONTROL` | ||
| and there is a pending transaction, | ||
| an implicit ``COMMIT`` statement is executed first. | ||
| No other implicit transaction control is performed; | ||
| any transaction control must be added to *sql_script*. | ||
| @@ -2183,9 +2254,12 @@ the transaction is committed. | ||
| If this commit fails, | ||
| or if the body of the ``with`` statement raises an uncaught exception, | ||
| the transaction is rolled back. | ||
| If :attr:`~Connection.autocommit` is ``False``, | ||
| a new transaction is implicitly opened after committing or rolling back. | ||
| If there is no open transaction upon leaving the body of the ``with`` statement, | ||
| the context manager is a no-op. | ||
| or if :attr:`~Connection.autocommit` is ``True``, | ||
| the context manager does nothing. | ||
| .. note:: | ||
| @@ -2270,13 +2344,72 @@ can be found in the `SQLite URI documentation`_. | ||
| Explanation | ||
| ----------- | ||
| .. _sqlite3-transaction-control: | ||
| .. _sqlite3-controlling-transactions: | ||
| Transaction control | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
erlend-aasland marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| The :mod:`!sqlite3` module does not adhere to the transaction handling recommended | ||
| by :pep:`249`. | ||
| :mod:`!sqlite3` offers multiple methods of controlling whether, | ||
| when and how database transactions are opened and closed. | ||
| :ref:`sqlite3-transaction-control-autocommit` is recommended, | ||
| while :ref:`sqlite3-transaction-control-isolation-level` | ||
| retains the pre-Python 3.12 behaviour. | ||
| .. _sqlite3-transaction-control-autocommit: | ||
erlend-aasland marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| Transaction control via the ``autocommit`` attribute | ||
| """""""""""""""""""""""""""""""""""""""""""""""""""" | ||
| The recommended way of controlling transaction behaviour is through | ||
| the :attr:`Connection.autocommit` attribute, | ||
| which should preferrably be set using the *autocommit* parameter | ||
| of :func:`connect`. | ||
| It is suggested to set *autocommit* to ``False``, | ||
| which implies :pep:`249`-compliant transaction control. | ||
| This means: | ||
| * :mod:`!sqlite3` ensures that a transaction is always open, | ||
| so :meth:`Connection.commit` and :meth:`Connection.rollback` | ||
| will implicitly open a new transaction immediately after closing | ||
| the pending one. | ||
| :mod:`!sqlite3` uses ``BEGIN DEFERRED`` statements when opening transactions. | ||
| * Transactions should be committed explicitly using :meth:`!commit`. | ||
| * Transactions should be rolled back explicitly using :meth:`!rollback`. | ||
| * An implicit rollback is performed if the database is | ||
| :meth:`~Connection.close`-ed with pending changes. | ||
erlend-aasland marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| Set *autocommit* to ``True`` to enable SQLite's `autocommit mode`_. | ||
| In this mode, :meth:`Connection.commit` and :meth:`Connection.rollback` | ||
| have no effect. | ||
erlend-aasland marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| Note that SQLite's autocommit mode is distinct from | ||
| the :pep:`249`-compliant :attr:`Connection.autocommit` attribute; | ||
| use :attr:`Connection.in_transaction` to query | ||
| the low-level SQLite autocommit mode. | ||
erlend-aasland marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| Set *autocommit* to :data:`LEGACY_TRANSACTION_CONTROL` | ||
| to leave transaction control behaviour to the | ||
| :attr:`Connection.isolation_level` attribute. | ||
| See :ref:`sqlite3-transaction-control-isolation-level` for more information. | ||
| .. _sqlite3-transaction-control-isolation-level: | ||
| Transaction control via the ``isolation_level`` attribute | ||
| """"""""""""""""""""""""""""""""""""""""""""""""""""""""" | ||
| .. note:: | ||
| The recommended way of controlling transactions is via the | ||
| :attr:`~Connection.autocommit` attribute. | ||
| See :ref:`sqlite3-transaction-control-autocommit`. | ||
| If :attr:`Connection.autocommit` is set to | ||
| :data:`LEGACY_TRANSACTION_CONTROL` (the default), | ||
| transaction behaviour is controlled using | ||
| the :attr:`Connection.isolation_level` attribute. | ||
erlend-aasland marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| Otherwise, :attr:`!isolation_level` has no effect. | ||
| If the connection attribute :attr:`~Connection.isolation_level` | ||
| is not ``None``, | ||
| @@ -2307,6 +2440,10 @@ regardless of the value of :attr:`~Connection.isolation_level`. | ||
| :mod:`!sqlite3` used to implicitly commit an open transaction before DDL | ||
| statements. This is no longer the case. | ||
| .. versionchanged:: 3.12 | ||
| The recommended way of controlling transactions is now via the | ||
| :attr:`~Connection.autocommit` attribute. | ||
erlend-aasland marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| .. _autocommit mode: | ||
| https://www.sqlite.org/lang_transaction.html#implicit_versus_explicit_transactions | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.