]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- add complete parameter /return value docs to session.begin() /
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 17 May 2017 14:46:52 +0000 (10:46 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 17 May 2017 14:58:37 +0000 (10:58 -0400)
session.begin_nested().  Fixes #3993

Change-Id: If485d77b364c34d94061d2f48efbde3f8a8adec9

doc/build/orm/session_transaction.rst
lib/sqlalchemy/orm/session.py

index ede455223d3f356294fe11bc0a31502083ac68a2..e686fd7f6de5a314389dd828480b2f466f06aa11 100644 (file)
@@ -114,8 +114,8 @@ reference the full state of the :class:`~sqlalchemy.orm.session.Session` right
 before :meth:`~.Session.begin_nested` was called.
 
 :meth:`~.Session.begin_nested`, in the same manner as the less often
-used :meth:`~.Session.begin` method, returns a transactional object
-which also works as a context manager.
+used :meth:`~.Session.begin` method, returns a :class:`.SessionTransaction` object
+which works as a context manager.
 It can be succinctly used around individual record inserts in order to catch
 things like unique constraint exceptions::
 
index 0d2fe6a7f897c93a43635191fb222e8684ac636b..3585166f49124fa42fafbf8c76a340a70f42292d 100644 (file)
@@ -776,18 +776,37 @@ class Session(_SessionClassMethods):
     def begin(self, subtransactions=False, nested=False):
         """Begin a transaction on this :class:`.Session`.
 
-        If this Session is already within a transaction, either a plain
-        transaction or nested transaction, an error is raised, unless
-        ``subtransactions=True`` or ``nested=True`` is specified.
+        The :meth:`.Session.begin` method is only
+        meaningful if this session is in **autocommit mode** prior to
+        it being called; see :ref:`session_autocommit` for background
+        on this setting.
+
+        The method will raise an error if this :class:`.Session`
+        is already inside of a transaction, unless
+        :paramref:`.Session.begin.subtransactions` or
+        :paramref:`.Session.begin.nested` are specified.
+
+        :param subtransactions: if True, indicates that this
+         :meth:`~.Session.begin` can create a subtransaction if a transaction
+         is already in progress. For documentation on subtransactions, please
+         see :ref:`session_subtransactions`.
+
+        :param nested: if True, begins a SAVEPOINT transaction and is equivalent
+         to calling :meth:`~.Session.begin_nested`. For documentation on
+         SAVEPOINT transactions, please see :ref:`session_begin_nested`.
+
+        :return: the :class:`.SessionTransaction` object.  Note that
+         :class:`.SessionTransaction`
+         acts as a Python context manager, allowing :meth:`.Session.begin`
+         to be used in a "with" block.  See :ref:`session_autocommit` for
+         an example.
 
-        The ``subtransactions=True`` flag indicates that this
-        :meth:`~.Session.begin` can create a subtransaction if a transaction
-        is already in progress. For documentation on subtransactions, please
-        see :ref:`session_subtransactions`.
+        .. seealso::
+
+            :ref:`session_autocommit`
+
+            :meth:`.Session.begin_nested`
 
-        The ``nested`` flag begins a SAVEPOINT transaction and is equivalent
-        to calling :meth:`~.Session.begin_nested`. For documentation on
-        SAVEPOINT transactions, please see :ref:`session_begin_nested`.
 
         """
         if self.transaction is not None:
@@ -804,14 +823,27 @@ class Session(_SessionClassMethods):
         return self.transaction  # needed for __enter__/__exit__ hook
 
     def begin_nested(self):
-        """Begin a `nested` transaction on this Session.
+        """Begin a "nested" transaction on this Session, e.g. SAVEPOINT.
 
-        The target database(s) must support SQL SAVEPOINTs or a
-        SQLAlchemy-supported vendor implementation of the idea.
+        The target database(s) and associated drivers must support SQL
+        SAVEPOINT for this method to function correctly.
 
         For documentation on SAVEPOINT
         transactions, please see :ref:`session_begin_nested`.
 
+        :return: the :class:`.SessionTransaction` object.  Note that
+         :class:`.SessionTransaction` acts as a context manager, allowing
+         :meth:`.Session.begin_nested` to be used in a "with" block.
+         See :ref:`session_begin_nested` for a usage example.
+
+        .. seealso::
+
+            :ref:`session_begin_nested`
+
+            :ref:`pysqlite_serializable` - special workarounds required
+            with the SQLite driver in order for SAVEPOINT to work
+            correctly.
+
         """
         return self.begin(nested=True)