@@ -185,7 +185,7 @@ def get_pyodbc_attrs_before(credentials: SQLServerCredentials) -> Dict:
185
185
"""
186
186
global _TOKEN
187
187
attrs_before : Dict
188
- MAX_QUERY_TIME_IN_SECONDS = 3300
188
+ MAX_REMAINING_TIME = 300
189
189
190
190
azure_auth_function_type = Callable [[SQLServerCredentials ], AccessToken ]
191
191
azure_auth_functions : Mapping [str , azure_auth_function_type ] = {
@@ -194,21 +194,24 @@ def get_pyodbc_attrs_before(credentials: SQLServerCredentials) -> Dict:
194
194
}
195
195
196
196
authentication = credentials .authentication .lower ()
197
- if authentication not in azure_auth_functions :
198
- attrs_before = {}
199
- elif _TOKEN is None or ( _TOKEN . expires_on - time . time ()) < MAX_QUERY_TIME_IN_SECONDS :
200
- azure_auth_function = azure_auth_functions [ authentication ]
201
- _TOKEN = _TOKEN or azure_auth_function ( credentials )
202
- token_bytes = convert_access_token_to_mswindows_byte_string ( _TOKEN )
197
+ if authentication in azure_auth_functions :
198
+ time_remaining = ( _TOKEN . expires_on - time . time ()) if _TOKEN else MAX_REMAINING_TIME
199
+
200
+ if _TOKEN is None or ( time_remaining < MAX_REMAINING_TIME ):
201
+ azure_auth_function = azure_auth_functions [ authentication ]
202
+ _TOKEN = azure_auth_function ( credentials )
203
203
204
+ token_bytes = convert_access_token_to_mswindows_byte_string (_TOKEN )
204
205
sql_copt_ss_access_token = 1256 # see source in docstring
205
206
attrs_before = {sql_copt_ss_access_token : token_bytes }
207
+ else :
208
+ attrs_before = {}
209
+
206
210
return attrs_before
207
211
208
212
209
213
class SQLServerConnectionManager (SQLConnectionManager ):
210
214
TYPE = "sqlserver"
211
- TOKEN = None
212
215
213
216
@contextmanager
214
217
def exception_handler (self , sql ):
0 commit comments