Execute when a new :class:`.SessionTransaction` is created. This event differs from :meth:`~.SessionEvents.after_begin` in that it occurs for each :class:`.SessionTransaction` overall, as opposed to when transactions are begun on individual database connections. It
(
self, session: Session, transaction: SessionTransaction
)
| 1744 | """ |
| 1745 | |
| 1746 | def after_transaction_create( |
| 1747 | self, session: Session, transaction: SessionTransaction |
| 1748 | ) -> None: |
| 1749 | """Execute when a new :class:`.SessionTransaction` is created. |
| 1750 | |
| 1751 | This event differs from :meth:`~.SessionEvents.after_begin` |
| 1752 | in that it occurs for each :class:`.SessionTransaction` |
| 1753 | overall, as opposed to when transactions are begun |
| 1754 | on individual database connections. It is also invoked |
| 1755 | for nested transactions and subtransactions, and is always |
| 1756 | matched by a corresponding |
| 1757 | :meth:`~.SessionEvents.after_transaction_end` event |
| 1758 | (assuming normal operation of the :class:`.Session`). |
| 1759 | |
| 1760 | :param session: the target :class:`.Session`. |
| 1761 | :param transaction: the target :class:`.SessionTransaction`. |
| 1762 | |
| 1763 | To detect if this is the outermost |
| 1764 | :class:`.SessionTransaction`, as opposed to a "subtransaction" or a |
| 1765 | SAVEPOINT, test that the :attr:`.SessionTransaction.parent` attribute |
| 1766 | is ``None``:: |
| 1767 | |
| 1768 | @event.listens_for(session, "after_transaction_create") |
| 1769 | def after_transaction_create(session, transaction): |
| 1770 | if transaction.parent is None: |
| 1771 | ... # work with top-level transaction |
| 1772 | |
| 1773 | To detect if the :class:`.SessionTransaction` is a SAVEPOINT, use the |
| 1774 | :attr:`.SessionTransaction.nested` attribute:: |
| 1775 | |
| 1776 | @event.listens_for(session, "after_transaction_create") |
| 1777 | def after_transaction_create(session, transaction): |
| 1778 | if transaction.nested: |
| 1779 | ... # work with SAVEPOINT transaction |
| 1780 | |
| 1781 | .. seealso:: |
| 1782 | |
| 1783 | :class:`.SessionTransaction` |
| 1784 | |
| 1785 | :meth:`~.SessionEvents.after_transaction_end` |
| 1786 | |
| 1787 | """ |
| 1788 | |
| 1789 | def after_transaction_end( |
| 1790 | self, session: Session, transaction: SessionTransaction |