mirror of
https://github.com/psycopg/psycopg2.git
synced 2024-11-22 08:56:34 +03:00
Highlight in docs that the context manager doesn't close the connection
Code as in #889 is not robust, but the behaviour is actually counter-intuitive.
This commit is contained in:
parent
4058f363d6
commit
5e01c47818
|
@ -797,9 +797,7 @@ is rolled back.
|
|||
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.
|
||||
|
||||
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
|
||||
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)
|
||||
|
@ -814,6 +812,18 @@ and each ``with`` block is effectively wrapped in a separate transaction::
|
|||
|
||||
conn.close()
|
||||
|
||||
.. warning::
|
||||
|
||||
Unlike file objects or other resources, exiting the connection's
|
||||
``with`` block **doesn't close the connection**, but only the transaction
|
||||
associated to it. If you want to make sure the connection is closed after
|
||||
a certain point, you should still use a try-catch block::
|
||||
|
||||
conn = psycopg2.connect(DSN)
|
||||
try:
|
||||
# connection usage
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
.. index::
|
||||
|
|
Loading…
Reference in New Issue
Block a user