Close this Session, using connection invalidation. This is a variant of :meth:`.Session.close` that will additionally ensure that the :meth:`_engine.Connection.invalidate` method will be called on each :class:`_engine.Connection` object that is currently in use for a
(self)
| 2648 | self._close_impl(invalidate=False, is_reset=True) |
| 2649 | |
| 2650 | def invalidate(self) -> None: |
| 2651 | """Close this Session, using connection invalidation. |
| 2652 | |
| 2653 | This is a variant of :meth:`.Session.close` that will additionally |
| 2654 | ensure that the :meth:`_engine.Connection.invalidate` |
| 2655 | method will be called on each :class:`_engine.Connection` object |
| 2656 | that is currently in use for a transaction (typically there is only |
| 2657 | one connection unless the :class:`_orm.Session` is used with |
| 2658 | multiple engines). |
| 2659 | |
| 2660 | This can be called when the database is known to be in a state where |
| 2661 | the connections are no longer safe to be used. |
| 2662 | |
| 2663 | Below illustrates a scenario when using `gevent |
| 2664 | <https://www.gevent.org/>`_, which can produce ``Timeout`` exceptions |
| 2665 | that may mean the underlying connection should be discarded:: |
| 2666 | |
| 2667 | import gevent |
| 2668 | |
| 2669 | try: |
| 2670 | sess = Session() |
| 2671 | sess.add(User()) |
| 2672 | sess.commit() |
| 2673 | except gevent.Timeout: |
| 2674 | sess.invalidate() |
| 2675 | raise |
| 2676 | except: |
| 2677 | sess.rollback() |
| 2678 | raise |
| 2679 | |
| 2680 | The method additionally does everything that :meth:`_orm.Session.close` |
| 2681 | does, including that all ORM objects are expunged. |
| 2682 | |
| 2683 | """ |
| 2684 | self._close_impl(invalidate=True) |
| 2685 | |
| 2686 | def _close_impl(self, invalidate: bool, is_reset: bool = False) -> None: |
| 2687 | if not is_reset and self._close_state is _SessionCloseState.ACTIVE: |