@@ -1677,7 +1677,7 @@ def _log_telemetry(self, telemetry_data) -> None:
16771677 self ._telemetry .try_add_log_to_batch (telemetry_data )
16781678
16791679 def _add_heartbeat (self ) -> None :
1680- """Add an hourly heartbeat query in order to keep connection alive."""
1680+ """Add a periodic heartbeat query in order to keep connection alive."""
16811681 if not self .heartbeat_thread :
16821682 self ._validate_client_session_keep_alive_heartbeat_frequency ()
16831683 heartbeat_wref = weakref .WeakMethod (self ._heartbeat_tick )
@@ -1703,7 +1703,7 @@ def _cancel_heartbeat(self) -> None:
17031703 logger .debug ("stopped heartbeat" )
17041704
17051705 def _heartbeat_tick (self ) -> None :
1706- """Execute a hearbeat if connection isn't closed yet."""
1706+ """Execute a heartbeat if connection isn't closed yet."""
17071707 if not self .is_closed ():
17081708 logger .debug ("heartbeating!" )
17091709 self .rest ._heartbeat ()
@@ -1990,3 +1990,21 @@ def _log_telemetry_imported_packages(self) -> None:
19901990 connection = self ,
19911991 )
19921992 )
1993+
1994+ def is_valid (self ) -> bool :
1995+ """This function tries to answer the question: Is this connection still good for sending queries?
1996+ Attempts to validate the connections both on the TCP/IP and Session levels."""
1997+ logger .debug ("validating connection and session" )
1998+ if self .is_closed ():
1999+ logger .debug ("connection is already closed and not valid" )
2000+ return False
2001+
2002+ try :
2003+ logger .debug ("trying to heartbeat into the session to validate" )
2004+ hb_result = self .rest ._heartbeat ()
2005+ session_valid = hb_result .get ("success" )
2006+ logger .debug ("session still valid? %s" , session_valid )
2007+ return bool (session_valid )
2008+ except Exception as e :
2009+ logger .debug ("session could not be validated due to exception: %s" , e )
2010+ return False
0 commit comments