@@ -681,13 +681,34 @@ def _initialize_cursor(self) -> None:
681681 Initialize the DDBC statement handle.
682682 """
683683 self ._allocate_statement_handle ()
684+ self ._set_timeout ()
684685
685686 def _allocate_statement_handle (self ) -> None :
686687 """
687688 Allocate the DDBC statement handle.
688689 """
689690 self .hstmt = self ._connection ._conn .alloc_statement_handle ()
690691
692+ def _set_timeout (self ) -> None :
693+ """
694+ Set the query timeout attribute on the statement handle.
695+ This is called once when the cursor is created and after any handle reallocation.
696+ Following pyodbc's approach for better performance.
697+ """
698+ if self ._timeout > 0 :
699+ logger .debug ("_set_timeout: Setting query timeout=%d seconds" , self ._timeout )
700+ try :
701+ timeout_value = int (self ._timeout )
702+ ret = ddbc_bindings .DDBCSQLSetStmtAttr (
703+ self .hstmt ,
704+ ddbc_sql_const .SQL_ATTR_QUERY_TIMEOUT .value ,
705+ timeout_value ,
706+ )
707+ check_error (ddbc_sql_const .SQL_HANDLE_STMT .value , self .hstmt , ret )
708+ logger .debug ("Query timeout set to %d seconds" , timeout_value )
709+ except Exception as e : # pylint: disable=broad-exception-caught
710+ logger .warning ("Failed to set query timeout: %s" , str (e ))
711+
691712 def _reset_cursor (self ) -> None :
692713 """
693714 Reset the DDBC statement handle.
@@ -1216,20 +1237,6 @@ def execute( # pylint: disable=too-many-locals,too-many-branches,too-many-state
12161237 encoding_settings = self ._get_encoding_settings ()
12171238
12181239 # Apply timeout if set (non-zero)
1219- if self ._timeout > 0 :
1220- logger .debug ("execute: Setting query timeout=%d seconds" , self ._timeout )
1221- try :
1222- timeout_value = int (self ._timeout )
1223- ret = ddbc_bindings .DDBCSQLSetStmtAttr (
1224- self .hstmt ,
1225- ddbc_sql_const .SQL_ATTR_QUERY_TIMEOUT .value ,
1226- timeout_value ,
1227- )
1228- check_error (ddbc_sql_const .SQL_HANDLE_STMT .value , self .hstmt , ret )
1229- logger .debug ("Set query timeout to %d seconds" , timeout_value )
1230- except Exception as e : # pylint: disable=broad-exception-caught
1231- logger .warning ("Failed to set query timeout: %s" , str (e ))
1232-
12331240 logger .debug ("execute: Creating parameter type list" )
12341241 param_info = ddbc_bindings .ParamInfo
12351242 parameters_type = []
0 commit comments