mirror of
https://github.com/psycopg/psycopg2.git
synced 2024-11-28 11:53:43 +03:00
Improve docs about connection's with only closing the transaction
This commit is contained in:
parent
a70a541e0b
commit
76ac3ec34e
|
@ -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::
|
||||||
|
|
Loading…
Reference in New Issue
Block a user