2424import net .snowflake .client .core .Event ;
2525import net .snowflake .client .core .EventUtil ;
2626import net .snowflake .client .core .ExecTimeTelemetryData ;
27+ import net .snowflake .client .core .HttpClientSettingsKey ;
2728import net .snowflake .client .core .HttpExecutingContext ;
2829import net .snowflake .client .core .HttpExecutingContextBuilder ;
2930import net .snowflake .client .core .HttpResponseContextDto ;
@@ -197,7 +198,10 @@ public static CloseableHttpResponse execute(
197198 false , // noRetry
198199 execTimeTelemetryData ,
199200 null ,
200- sfSession );
201+ sfSession ,
202+ null ,
203+ null ,
204+ false );
201205 }
202206
203207 /**
@@ -312,7 +316,10 @@ public static CloseableHttpResponse execute(
312316 noRetry ,
313317 execTimeData ,
314318 null ,
315- sfSession );
319+ sfSession ,
320+ null ,
321+ null ,
322+ false );
316323 }
317324
318325 /**
@@ -427,7 +434,10 @@ public static CloseableHttpResponse execute(
427434 false , // noRetry
428435 execTimeData ,
429436 retryContextManager ,
430- sfSession );
437+ sfSession ,
438+ null ,
439+ null ,
440+ false );
431441 }
432442
433443 /**
@@ -488,9 +498,11 @@ public static CloseableHttpResponse execute(
488498 noRetry ,
489499 execTimeData ,
490500 retryManager ,
491- null );
501+ null ,
502+ null ,
503+ null ,
504+ false );
492505 }
493-
494506 /**
495507 * Execute an HTTP request with retry logic.
496508 *
@@ -512,6 +524,10 @@ public static CloseableHttpResponse execute(
512524 * @param retryManager RetryContextManager - object allowing to optionally pass custom logic that
513525 * should be executed before and/or after the retry
514526 * @param sfSession the session associated with the request
527+ * @param key HttpClientSettingsKey object
528+ * @param httpHeaderCustomizer HttpHeadersCustomizer object for customization of HTTP headers for
529+ * requests sent by the Snowflake JDBC driver.
530+ * @param isHttpClientWithoutDecompression flag for create client without Decompression
515531 * @return HttpResponse Object get from server
516532 * @throws net.snowflake.client.jdbc.SnowflakeSQLException Request timeout Exception or Illegal
517533 * State Exception i.e. connection is already shutdown etc
@@ -533,7 +549,10 @@ public static CloseableHttpResponse execute(
533549 boolean noRetry ,
534550 ExecTimeTelemetryData execTimeData ,
535551 RetryContextManager retryManager ,
536- SFBaseSession sfSession )
552+ SFBaseSession sfSession ,
553+ HttpClientSettingsKey key ,
554+ List <HttpHeadersCustomizer > httpHeaderCustomizer ,
555+ boolean isHttpClientWithoutDecompression )
537556 throws SnowflakeSQLException {
538557 return executeWithRetries (
539558 httpClient ,
@@ -550,7 +569,10 @@ public static CloseableHttpResponse execute(
550569 retryHTTP403 , // retry on HTTP 403
551570 noRetry ,
552571 new ExecTimeTelemetryData (),
553- sfSession )
572+ sfSession ,
573+ key ,
574+ httpHeaderCustomizer ,
575+ isHttpClientWithoutDecompression )
554576 .getHttpResponse ();
555577 }
556578
@@ -738,7 +760,10 @@ public static HttpResponseContextDto executeWithRetries(
738760 boolean retryHTTP403 ,
739761 boolean unpackResponse ,
740762 ExecTimeTelemetryData execTimeTelemetryData ,
741- SFBaseSession sfSession )
763+ SFBaseSession sfSession ,
764+ HttpClientSettingsKey key ,
765+ List <HttpHeadersCustomizer > httpHeaderCustomizer ,
766+ boolean isHttpClientWithoutDecompression )
742767 throws SnowflakeSQLException {
743768 return executeWithRetries (
744769 httpClient ,
@@ -756,7 +781,10 @@ public static HttpResponseContextDto executeWithRetries(
756781 false ,
757782 unpackResponse ,
758783 execTimeTelemetryData ,
759- sfSession );
784+ sfSession ,
785+ key ,
786+ httpHeaderCustomizer ,
787+ isHttpClientWithoutDecompression );
760788 }
761789
762790 /**
@@ -799,7 +827,10 @@ public static HttpResponseContextDto executeWithRetries(
799827 boolean noRetry ,
800828 boolean unpackResponse ,
801829 ExecTimeTelemetryData execTimeTelemetryData ,
802- SFBaseSession sfSession )
830+ SFBaseSession sfSession ,
831+ HttpClientSettingsKey key ,
832+ List <HttpHeadersCustomizer > httpHeaderCustomizer ,
833+ boolean isHttpClientWithoutDecompression )
803834 throws SnowflakeSQLException {
804835 String requestIdStr = URLUtil .getRequestIdLogStr (httpRequest .getURI ());
805836 String requestInfoScrubbed = SecretDetector .maskSASToken (httpRequest .toString ());
@@ -820,7 +851,15 @@ public static HttpResponseContextDto executeWithRetries(
820851 .loginRequest (SessionUtil .isNewRetryStrategyRequest (httpRequest ))
821852 .withSfSession (sfSession )
822853 .build ();
823- return executeWithRetries (httpClient , httpRequest , context , execTimeTelemetryData , null );
854+ return executeWithRetries (
855+ httpClient ,
856+ httpRequest ,
857+ context ,
858+ execTimeTelemetryData ,
859+ null ,
860+ key ,
861+ httpHeaderCustomizer ,
862+ isHttpClientWithoutDecompression );
824863 }
825864
826865 /**
@@ -840,7 +879,10 @@ public static HttpResponseContextDto executeWithRetries(
840879 HttpRequestBase httpRequest ,
841880 HttpExecutingContext httpExecutingContext ,
842881 ExecTimeTelemetryData execTimeData ,
843- RetryContextManager retryManager )
882+ RetryContextManager retryManager ,
883+ HttpClientSettingsKey key ,
884+ List <HttpHeadersCustomizer > httpHeaderCustomizer ,
885+ boolean isHttpClientWithoutDecompression )
844886 throws SnowflakeSQLException {
845887 Stopwatch networkComunnicationStapwatch = null ;
846888 Stopwatch requestReponseStopWatch = null ;
@@ -908,7 +950,26 @@ public static HttpResponseContextDto executeWithRetries(
908950 responseDto .setHttpResponse (response );
909951 execTimeData .setHttpClientEnd ();
910952 } catch (Exception ex ) {
911- responseDto .setSavedEx (handlingNotRetryableException (ex , httpExecutingContext ));
953+ if (ex instanceof IllegalStateException ) {
954+ // if exception is caused by illegal state, e.g shutdown of http client
955+ // because of closing of connection, then recreate the http client and remove existing
956+ // from the cache.
957+ logger .warn (
958+ "IllegalStateException encountered while processing the HTTP request."
959+ + " The HttpClient was shut down due to connection closure. "
960+ + "Attempting to rebuild the HttpClient and retry the request." );
961+ // Clear the httpClient cache.
962+ HttpUtil .httpClient .remove (key );
963+ // rebuild the http client.
964+ if (isHttpClientWithoutDecompression ) {
965+ httpClient = HttpUtil .getHttpClientWithoutDecompression (key , httpHeaderCustomizer );
966+ } else {
967+ httpClient = HttpUtil .getHttpClient (key , httpHeaderCustomizer );
968+ }
969+ continue ;
970+ } else {
971+ responseDto .setSavedEx (handlingNotRetryableException (ex , httpExecutingContext ));
972+ }
912973 } finally {
913974 // Reset the socket timeout to its original value if it is not the
914975 // very first iteration.
0 commit comments