MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / invalidate

Method invalidate

lib/sqlalchemy/orm/session.py:2556–2590  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

2554 self._close_impl(invalidate=False, is_reset=True)
2555
2556 def invalidate(self) -> None:
2557 """Close this Session, using connection invalidation.
2558
2559 This is a variant of :meth:`.Session.close` that will additionally
2560 ensure that the :meth:`_engine.Connection.invalidate`
2561 method will be called on each :class:`_engine.Connection` object
2562 that is currently in use for a transaction (typically there is only
2563 one connection unless the :class:`_orm.Session` is used with
2564 multiple engines).
2565
2566 This can be called when the database is known to be in a state where
2567 the connections are no longer safe to be used.
2568
2569 Below illustrates a scenario when using `gevent
2570 <https://www.gevent.org/>`_, which can produce ``Timeout`` exceptions
2571 that may mean the underlying connection should be discarded::
2572
2573 import gevent
2574
2575 try:
2576 sess = Session()
2577 sess.add(User())
2578 sess.commit()
2579 except gevent.Timeout:
2580 sess.invalidate()
2581 raise
2582 except:
2583 sess.rollback()
2584 raise
2585
2586 The method additionally does everything that :meth:`_orm.Session.close`
2587 does, including that all ORM objects are expunged.
2588
2589 """
2590 self._close_impl(invalidate=True)
2591
2592 def _close_impl(self, invalidate: bool, is_reset: bool = False) -> None:
2593 if not is_reset and self._close_state is _SessionCloseState.ACTIVE:

Calls 1

_close_implMethod · 0.95