diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2017-05-17 10:46:52 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2017-05-17 10:58:37 -0400 |
commit | 4c6917e1d68a8baab7efe10e9ce5e5c8187f65ca (patch) | |
tree | 209a64802f14d0ddb23ab8ccb769f67737cecc71 | |
parent | 2869b86ea44225d17602e54f663055290a86d4f4 (diff) | |
download | sqlalchemy-4c6917e1d68a8baab7efe10e9ce5e5c8187f65ca.tar.gz |
- add complete parameter /return value docs to session.begin() /
session.begin_nested(). Fixes #3993
Change-Id: If485d77b364c34d94061d2f48efbde3f8a8adec9
-rw-r--r-- | doc/build/orm/session_transaction.rst | 4 | ||||
-rw-r--r-- | lib/sqlalchemy/orm/session.py | 58 |
2 files changed, 47 insertions, 15 deletions
diff --git a/doc/build/orm/session_transaction.rst b/doc/build/orm/session_transaction.rst index ede455223..e686fd7f6 100644 --- a/doc/build/orm/session_transaction.rst +++ b/doc/build/orm/session_transaction.rst @@ -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:: diff --git a/lib/sqlalchemy/orm/session.py b/lib/sqlalchemy/orm/session.py index 0d2fe6a7f..3585166f4 100644 --- a/lib/sqlalchemy/orm/session.py +++ b/lib/sqlalchemy/orm/session.py @@ -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) |