Execute when the span of a :class:`.SessionTransaction` ends. This event differs from :meth:`~.SessionEvents.after_commit` in that it corresponds to all :class:`.SessionTransaction` objects in use, including those for nested transactions and subtransactions, and is a
(
self, session: Session, transaction: SessionTransaction
)
| 1776 | """ |
| 1777 | |
| 1778 | def after_transaction_end( |
| 1779 | self, session: Session, transaction: SessionTransaction |
| 1780 | ) -> None: |
| 1781 | """Execute when the span of a :class:`.SessionTransaction` ends. |
| 1782 | |
| 1783 | This event differs from :meth:`~.SessionEvents.after_commit` |
| 1784 | in that it corresponds to all :class:`.SessionTransaction` |
| 1785 | objects in use, including those for nested transactions |
| 1786 | and subtransactions, and is always matched by a corresponding |
| 1787 | :meth:`~.SessionEvents.after_transaction_create` event. |
| 1788 | |
| 1789 | :param session: the target :class:`.Session`. |
| 1790 | :param transaction: the target :class:`.SessionTransaction`. |
| 1791 | |
| 1792 | To detect if this is the outermost |
| 1793 | :class:`.SessionTransaction`, as opposed to a "subtransaction" or a |
| 1794 | SAVEPOINT, test that the :attr:`.SessionTransaction.parent` attribute |
| 1795 | is ``None``:: |
| 1796 | |
| 1797 | @event.listens_for(session, "after_transaction_create") |
| 1798 | def after_transaction_end(session, transaction): |
| 1799 | if transaction.parent is None: |
| 1800 | ... # work with top-level transaction |
| 1801 | |
| 1802 | To detect if the :class:`.SessionTransaction` is a SAVEPOINT, use the |
| 1803 | :attr:`.SessionTransaction.nested` attribute:: |
| 1804 | |
| 1805 | @event.listens_for(session, "after_transaction_create") |
| 1806 | def after_transaction_end(session, transaction): |
| 1807 | if transaction.nested: |
| 1808 | ... # work with SAVEPOINT transaction |
| 1809 | |
| 1810 | .. seealso:: |
| 1811 | |
| 1812 | :class:`.SessionTransaction` |
| 1813 | |
| 1814 | :meth:`~.SessionEvents.after_transaction_create` |
| 1815 | |
| 1816 | """ |
| 1817 | |
| 1818 | def before_commit(self, session: Session) -> None: |
| 1819 | """Execute before commit is called. |