Improve docs about connection's with only closing the transaction

This commit is contained in:
Daniele Varrazzo 2015-05-03 11:59:06 +01:00
parent a70a541e0b
commit 76ac3ec34e

View File

@ -705,13 +705,28 @@ managers* and can be used with the ``with`` statement::
When a connection exits the ``with`` block, if no exception has been raised by When a connection exits the ``with`` block, if no exception has been raised by
the block, the transaction is committed. In case of exception the transaction the block, the transaction is committed. In case of exception the transaction
is rolled back. In no case the connection is closed: a connection can be used is rolled back.
in more than a ``with`` statement and each ``with`` block is effectively
wrapped in a transaction.
When a cursor exits the ``with`` block it is closed, releasing any resource When a cursor exits the ``with`` block it is closed, releasing any resource
eventually associated with it. The state of the transaction is not affected. eventually associated with it. The state of the transaction is not affected.
Note that, unlike file objects or other resources, exiting the connection's
``with`` block *doesn't close the connection* but only the transaction
associated with it: a connection can be used in more than a ``with`` statement
and each ``with`` block is effectively wrapped in a separate transaction::
conn = psycopg2.connect(DSN)
with conn:
with conn.cursor() as curs:
curs.execute(SQL1)
with conn:
with conn.cursor() as curs:
curs.execute(SQL2)
conn.close()
.. index:: .. index::