]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Clarify close for sync / async session
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 24 May 2021 13:59:17 +0000 (09:59 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 24 May 2021 13:59:17 +0000 (09:59 -0400)
Add seealso links from Session.close() and AsyncSession.close()
 to narrative description, clarify in both places what the method
does and does not do.

Change-Id: Ib804753a86b4761e5f198c52121e8433c851cbc4
References: #6528

doc/build/orm/session_basics.rst
lib/sqlalchemy/ext/asyncio/session.py
lib/sqlalchemy/orm/session.py

index 40c81f31c1243188bf10b6cff3b45bf31c9c2bcc..fb148f6f27f2c768ab2f74cb149f44e3ef72eac6 100644 (file)
@@ -891,6 +891,7 @@ further discussion.
 
   :ref:`session_autobegin`
 
+.. _session_closing:
 
 Closing
 -------
index 8d19819b00e009e18435fa9c509c56a0ccb9edfa..343465f37775f75af10680a522ffb5496186c3c1 100644 (file)
@@ -304,7 +304,33 @@ class AsyncSession:
         return await greenlet_spawn(self.sync_session.commit)
 
     async def close(self):
-        """Close this :class:`_asyncio.AsyncSession`."""
+        """Close out the transactional resources and ORM objects used by this
+        :class:`_asyncio.AsyncSession`.
+
+        This expunges all ORM objects associated with this
+        :class:`_asyncio.AsyncSession`, ends any transaction in progress and
+        :term:`releases` any :class:`_asyncio.AsyncConnection` objects which
+        this :class:`_asyncio.AsyncSession` itself has checked out from
+        associated :class:`_asyncio.AsyncEngine` objects. The operation then
+        leaves the :class:`_asyncio.AsyncSession` in a state which it may be
+        used again.
+
+        .. tip::
+
+            The :meth:`_asyncio.AsyncSession.close` method **does not prevent
+            the Session from being used again**. The
+            :class:`_asyncio.AsyncSession` itself does not actually have a
+            distinct "closed" state; it merely means the
+            :class:`_asyncio.AsyncSession` will release all database
+            connections and ORM objects.
+
+
+        .. seealso::
+
+            :ref:`session_closing` - detail on the semantics of
+            :meth:`_asyncio.AsyncSession.close`
+
+        """
         return await greenlet_spawn(self.sync_session.close)
 
     @classmethod
index e4a9b90463315a0efe2a14e5c515e757b963c560..ec11e7adf3807da8cb2b52f32633f4d81495b9a6 100644 (file)
@@ -1713,19 +1713,34 @@ class Session(_SessionClassMethods):
         ).scalar()
 
     def close(self):
-        """Close this Session.
+        """Close out the transactional resources and ORM objects used by this
+        :class:`_orm.Session`.
+
+        This expunges all ORM objects associated with this
+        :class:`_orm.Session`, ends any transaction in progress and
+        :term:`releases` any :class:`_engine.Connection` objects which this
+        :class:`_orm.Session` itself has checked out from associated
+        :class:`_engine.Engine` objects. The operation then leaves the
+        :class:`_orm.Session` in a state which it may be used again.
 
-        This clears all items and ends any transaction in progress.
+        .. tip::
 
-        If this Session was created with ``autocommit=False``, a new
-        transaction will be begun when the :class:`.Session` is next asked
-        to procure a database connection.
+            The :meth:`_orm.Session.close` method **does not prevent the
+            Session from being used again**.   The :class:`_orm.Session` itself
+            does not actually have a distinct "closed" state; it merely means
+            the :class:`_orm.Session` will release all database connections
+            and ORM objects.
 
         .. versionchanged:: 1.4  The :meth:`.Session.close` method does not
            immediately create a new :class:`.SessionTransaction` object;
            instead, the new :class:`.SessionTransaction` is created only if
            the :class:`.Session` is used again for a database operation.
 
+        .. seealso::
+
+            :ref:`session_closing` - detail on the semantics of
+            :meth:`_orm.Session.close`
+
         """
         self._close_impl(invalidate=False)