MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / invalidate

Method invalidate

lib/sqlalchemy/engine/base.py:709–766  ·  view source on GitHub ↗

Invalidate the underlying DBAPI connection associated with this :class:`_engine.Connection`. An attempt will be made to close the underlying DBAPI connection immediately; however if this operation fails, the error is logged but not raised. The connection is then dis

(self, exception: Optional[BaseException] = None)

Source from the content-addressed store, hash-verified

707 return self.connection.info
708
709 def invalidate(self, exception: Optional[BaseException] = None) -> None:
710 """Invalidate the underlying DBAPI connection associated with
711 this :class:`_engine.Connection`.
712
713 An attempt will be made to close the underlying DBAPI connection
714 immediately; however if this operation fails, the error is logged
715 but not raised. The connection is then discarded whether or not
716 close() succeeded.
717
718 Upon the next use (where "use" typically means using the
719 :meth:`_engine.Connection.execute` method or similar),
720 this :class:`_engine.Connection` will attempt to
721 procure a new DBAPI connection using the services of the
722 :class:`_pool.Pool` as a source of connectivity (e.g.
723 a "reconnection").
724
725 If a transaction was in progress (e.g. the
726 :meth:`_engine.Connection.begin` method has been called) when
727 :meth:`_engine.Connection.invalidate` method is called, at the DBAPI
728 level all state associated with this transaction is lost, as
729 the DBAPI connection is closed. The :class:`_engine.Connection`
730 will not allow a reconnection to proceed until the
731 :class:`.Transaction` object is ended, by calling the
732 :meth:`.Transaction.rollback` method; until that point, any attempt at
733 continuing to use the :class:`_engine.Connection` will raise an
734 :class:`~sqlalchemy.exc.InvalidRequestError`.
735 This is to prevent applications from accidentally
736 continuing an ongoing transactional operations despite the
737 fact that the transaction has been lost due to an
738 invalidation.
739
740 The :meth:`_engine.Connection.invalidate` method,
741 just like auto-invalidation,
742 will at the connection pool level invoke the
743 :meth:`_events.PoolEvents.invalidate` event.
744
745 :param exception: an optional ``Exception`` instance that's the
746 reason for the invalidation. is passed along to event handlers
747 and logging functions.
748
749 .. seealso::
750
751 :ref:`pool_connection_invalidation`
752
753 """
754
755 if self.invalidated:
756 return
757
758 if self.closed:
759 raise exc.ResourceClosedError("This Connection is closed")
760
761 if self._still_open_and_dbapi_connection_is_valid:
762 pool_proxied_connection = self._dbapi_connection
763 assert pool_proxied_connection is not None
764 pool_proxied_connection.invalidate(exception)
765
766 self._dbapi_connection = None

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected