Added documentation about the changes in transaction control

This commit is contained in:
Daniele Varrazzo 2017-02-04 15:55:59 +00:00
parent 9054eeccc0
commit c54a614c6e
4 changed files with 79 additions and 21 deletions

9
NEWS
View File

@ -33,6 +33,10 @@ New features:
(:ticket:`#491`). (:ticket:`#491`).
- Added ``async_`` as an alias for ``async`` to support Python 3.7 where - Added ``async_`` as an alias for ``async`` to support Python 3.7 where
``async`` will become a keyword (:ticket:`#495`). ``async`` will become a keyword (:ticket:`#495`).
- Unless in autocommit, do not use :sql:`default_transaction_*` settings to
control the session characteristics as it may create problems with external
connection pools such as pgbouncer; use :sql:`BEGIN` options instead
(:ticket:`#503`).
Bug fixes: Bug fixes:
@ -44,6 +48,11 @@ Other changes:
- Dropped support for Python 2.5. - Dropped support for Python 2.5.
- Dropped support for client library older than PostgreSQL 9.1 (but older - Dropped support for client library older than PostgreSQL 9.1 (but older
server versions are still supported). server versions are still supported).
- `~connection.isolation_level` doesn't read from the database but will return
`~psycopg2.extensions.ISOLATION_LEVEL_DEFAULT` if no value was set on the
connection.
- `~connection.set_isolation_level()` will throw an exception if executed
inside a transaction; previously it would have silently rolled it back.
What's new in psycopg 2.6.3 What's new in psycopg 2.6.3

View File

@ -400,6 +400,32 @@ The ``connection`` class
.. versionadded:: 2.4.2 .. versionadded:: 2.4.2
.. versionchanged:: 2.7
Before this version, the function would have set
:sql:`default_transaction_*` attribute in the current session;
this implementation has the problem of not playing well with
external connection pooling working at transaction level and not
resetting the state of the session: changing the default
transaction would pollute the connections in the pool and create
problems to other applications using the same pool.
Starting from 2.7, if the connection is not autocommit, the
transaction characteristics are issued together with :sql:`BEGIN`
and will leave the :sql:`default_transaction_*` settings untouched.
For example::
conn.set_session(readonly=True)
will not change :sql:`default_transaction_read_only`, but
following transaction will start with a :sql:`BEGIN READ ONLY`.
Conversely, using::
conn.set_session(readonly=True, autocommit=True)
will set :sql:`default_transaction_read_only` to :sql:`on` and
rely on the server to apply the read only state to whatever
transaction, implicit or explicit, is executed in the connection.
.. attribute:: autocommit .. attribute:: autocommit
@ -428,32 +454,54 @@ The ``connection`` class
.. versionadded:: 2.4.2 .. versionadded:: 2.4.2
.. attribute:: isolation_level
.. method:: set_isolation_level(level) .. method:: set_isolation_level(level)
.. note:: .. note::
From version 2.4.2, `set_session()` and `autocommit`, offer From version 2.4.2, `set_session()` and `autocommit` offer
finer control on the transaction characteristics. finer control on the transaction characteristics.
Read or set the `transaction isolation level`_ for the current session. Set the `transaction isolation level`_ for the current session.
The level defines the different phenomena that can happen in the The level defines the different phenomena that can happen in the
database between concurrent transactions. database between concurrent transactions.
The value set or read is an integer: symbolic constants are defined in The value set is an integer: symbolic constants are defined in
the module `psycopg2.extensions`: see the module `psycopg2.extensions`: see
:ref:`isolation-level-constants` for the available values. :ref:`isolation-level-constants` for the available values.
The default level is :sql:`READ COMMITTED`: at this level a The default level is `~psycopg2.extensions.ISOLATION_LEVEL_DEFAULT`:
transaction is automatically started the first time a database command at this level a transaction is automatically started the first time a
is executed. If you want an *autocommit* mode, switch to database command is executed. If you want an *autocommit* mode,
`~psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT` before switch to `~psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT` before
executing any command:: executing any command::
>>> conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) >>> conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
See also :ref:`transactions-control`. See also :ref:`transactions-control`.
.. versionchanged:: 2.7
the function must be called outside a transaction; previously it
would have executed an implicit :sql:`ROLLBACK`; it will now raise
an exception.
.. attribute:: isolation_level
Read the `transaction isolation level`_ for the current session. The
value is one of the :ref:`isolation-level-constants` defined in the
`psycopg2.extensions` module.
.. versionchanged:: 2.7
the default value for `!isolation_level` is
`~psycopg2.extensions.ISOLATION_LEVEL_DEFAULT`; previously the
property would have queried the server and returned the real value
applied. To know this value you can run a query such as :sql:`show
transaction_isolation`. Usually the default value is `READ
COMMITTED`, but this may be changed in the server configuration.
.. index:: .. index::
pair: Client; Encoding pair: Client; Encoding

View File

@ -567,15 +567,16 @@ Isolation level constants
------------------------- -------------------------
Psycopg2 `connection` objects hold informations about the PostgreSQL Psycopg2 `connection` objects hold informations about the PostgreSQL
`transaction isolation level`_. The current transaction level can be read `transaction isolation level`_. By default Psycopg doesn't change the default
from the `~connection.isolation_level` attribute. The default isolation configuration of the server (`ISOLATION_LEVEL_DEFAULT`); the default for
level is :sql:`READ COMMITTED`. A different isolation level con be set PostgreSQL servers is typically :sql:`READ COMMITTED`, but this may be changed
through the `~connection.set_isolation_level()` method. The level can be in the server configuration files. A different isolation level can be set
set to one of the following constants: through the `~connection.set_isolation_level()` or `~connection.set_session()`
methods. The level can be set to one of the following constants:
.. data:: ISOLATION_LEVEL_AUTOCOMMIT .. data:: ISOLATION_LEVEL_AUTOCOMMIT
No transaction is started when command are issued and no No transaction is started when commands are executed and no
`~connection.commit()` or `~connection.rollback()` is required. `~connection.commit()` or `~connection.rollback()` is required.
Some PostgreSQL command such as :sql:`CREATE DATABASE` or :sql:`VACUUM` Some PostgreSQL command such as :sql:`CREATE DATABASE` or :sql:`VACUUM`
can't run into a transaction: to run such command use:: can't run into a transaction: to run such command use::
@ -653,11 +654,12 @@ set to one of the following constants:
.. data:: ISOLATION_LEVEL_DEFAULT .. data:: ISOLATION_LEVEL_DEFAULT
Whatever is defined by the server, either via server configuration or by A new transaction is started at the first `~cursor.execute()` command, but
statements executed within the session outside Pyscopg control; Psycopg the isolation level is not explicitly selected by Psycopg: the server will
will not force an isolation level of its own. If you want to know what the use whatever level is defined in its configuration or by statements
value is you can use a query such as :sql:`show transaction_isolation` or executed within the session outside Pyscopg control. If you want to know
:sql:`show default_transaction_isolation`. what the value is you can use a query such as :sql:`show
transaction_isolation`.
.. versionadded:: 2.7 .. versionadded:: 2.7

View File

@ -676,8 +676,7 @@ commands executed will be immediately committed and no rollback is possible. A
few commands (e.g. :sql:`CREATE DATABASE`, :sql:`VACUUM`...) require to be run few commands (e.g. :sql:`CREATE DATABASE`, :sql:`VACUUM`...) require to be run
outside any transaction: in order to be able to run these commands from outside any transaction: in order to be able to run these commands from
Psycopg, the connection must be in autocommit mode: you can use the Psycopg, the connection must be in autocommit mode: you can use the
`~connection.autocommit` property (`~connection.set_isolation_level()` in `~connection.autocommit` property.
older versions).
.. warning:: .. warning::