1616
1717package com .google .cloud .bigquery .jdbc ;
1818
19+ import com .google .api .client .googleapis .json .GoogleJsonResponseException ;
1920import com .google .api .client .http .HttpRequestInitializer ;
2021import com .google .api .client .http .HttpTransport ;
2122import com .google .api .client .json .gson .GsonFactory ;
@@ -1251,7 +1252,7 @@ public synchronized List<String> getDiscoveredProjects() {
12511252 HttpTransport transport = transportOptions .getHttpTransportFactory ().create ();
12521253 HttpRequestInitializer initializer = transportOptions .getHttpRequestInitializer (options );
12531254 Bigquery lowLevelBq =
1254- new Bigquery .Builder (transport , new GsonFactory (), initializer )
1255+ new Bigquery .Builder (transport , GsonFactory . getDefaultInstance (), initializer )
12551256 .setRootUrl (options .getResolvedApiaryHost (BIGQUERY_SERVICE_NAME ))
12561257 .setApplicationName (DEFAULT_JDBC_TOKEN_VALUE )
12571258 .build ();
@@ -1276,9 +1277,17 @@ public synchronized List<String> getDiscoveredProjects() {
12761277 } while (pageToken != null );
12771278
12781279 this .discoveredProjectsCache = ImmutableList .copyOf (projects );
1280+ } catch (GoogleJsonResponseException e ) {
1281+ LOG .warning (e , "Failed to list all accessible projects due to Google API error." );
1282+ int statusCode = e .getStatusCode ();
1283+ // Only cache empty list for non-transient auth/permission errors (400, 401, 403)
1284+ if (statusCode == 400 || statusCode == 401 || statusCode == 403 ) {
1285+ this .discoveredProjectsCache = ImmutableList .of ();
1286+ }
1287+ return ImmutableList .of ();
12791288 } catch (Exception e ) {
12801289 LOG .warning (e , "Failed to list all accessible projects, falling back to connection default." );
1281- this . discoveredProjectsCache = ImmutableList .of ();
1290+ return ImmutableList .of ();
12821291 }
12831292 return this .discoveredProjectsCache ;
12841293 }
0 commit comments