mirror of
https://github.com/psycopg/psycopg2.git
synced 2024-11-22 08:56:34 +03:00
Document context manager usage in connection and cursor docs
Close #1143
This commit is contained in:
parent
9387bd3c09
commit
0ee9d840a1
|
@ -21,6 +21,28 @@ The ``connection`` class
|
|||
Connections are thread safe and can be shared among many threads. See
|
||||
:ref:`thread-safety` for details.
|
||||
|
||||
Connections can be used as context managers. Note that a context wraps a
|
||||
transaction: if the context exits with success the transaction is
|
||||
committed, if it exits with an exception the transaction is rolled back.
|
||||
Note that the connection is not closed by the context and it can be used
|
||||
for several contexts.
|
||||
|
||||
.. code:: python
|
||||
|
||||
conn = psycopg2.connect(DSN)
|
||||
|
||||
with conn:
|
||||
with conn.cursor() as curs:
|
||||
curs.execute(SQL1)
|
||||
|
||||
with conn:
|
||||
with conn.cursor() as curs:
|
||||
curs.execute(SQL2)
|
||||
|
||||
# leaving contexts doesn't close the connection
|
||||
conn.close()
|
||||
|
||||
|
||||
.. method:: cursor(name=None, cursor_factory=None, scrollable=None, withhold=False)
|
||||
|
||||
Return a new `cursor` object using the connection.
|
||||
|
|
|
@ -34,6 +34,16 @@ The ``cursor`` class
|
|||
many cursors from the same connection and should use each cursor from
|
||||
a single thread. See :ref:`thread-safety` for details.
|
||||
|
||||
Cursors can be used as context managers: leaving the context will close
|
||||
the cursor.
|
||||
|
||||
.. code:: python
|
||||
|
||||
with conn.cursor() as curs:
|
||||
curs.execute(SQL)
|
||||
|
||||
# the cursor is now closed
|
||||
|
||||
|
||||
.. attribute:: description
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user