@@ -155,7 +155,6 @@ public class BigQueryConnection extends BigQueryNoOpsConnection {
155155 BigQueryJdbcUrlUtility .HTTP_CONNECT_TIMEOUT_PROPERTY_NAME ,
156156 BigQueryJdbcUrlUtility .HTTP_READ_TIMEOUT_PROPERTY_NAME ,
157157 BigQueryJdbcUrlUtility .BYOID_AUDIENCE_URI_PROPERTY_NAME ,
158- BigQueryJdbcUrlUtility .BYOID_CREDENTIAL_SOURCE_PROPERTY_NAME ,
159158 BigQueryJdbcUrlUtility .BYOID_POOL_USER_PROJECT_PROPERTY_NAME ,
160159 BigQueryJdbcUrlUtility .BYOID_SA_IMPERSONATION_URI_PROPERTY_NAME ,
161160 BigQueryJdbcUrlUtility .BYOID_SUBJECT_TOKEN_TYPE_PROPERTY_NAME ,
@@ -264,6 +263,35 @@ public class BigQueryConnection extends BigQueryNoOpsConnection {
264263 private final AtomicReference <SessionState > sessionState =
265264 new AtomicReference <>(new SessionState (null , Collections .emptyList (), false ));
266265
266+ static String sanitizeForLog (String value ) {
267+ if (value == null ) {
268+ return null ;
269+ }
270+ StringBuilder sb = null ;
271+ for (int i = 0 ; i < value .length (); i ++) {
272+ char c = value .charAt (i );
273+ if (c < 0x20 || c == 0x7F ) {
274+ if (sb == null ) {
275+ sb = new StringBuilder (value .length ()).append (value , 0 , i );
276+ }
277+ sb .append ('?' );
278+ } else if (sb != null ) {
279+ sb .append (c );
280+ }
281+ }
282+ return sb == null ? value : sb .toString ();
283+ }
284+
285+ static String maskPropertyValue (String key , Object value ) {
286+ if (value == null ) {
287+ return null ;
288+ }
289+ if (key == null || !SAFE_TO_LOG_PROPERTIES .contains (key )) {
290+ return "*****" ;
291+ }
292+ return sanitizeForLog (String .valueOf (value ));
293+ }
294+
267295 BigQueryConnection (String url ) throws SQLException {
268296 this (url , DataSource .fromUrl (url ));
269297 }
@@ -279,32 +307,37 @@ public class BigQueryConnection extends BigQueryNoOpsConnection {
279307 if (rawLogLevel == null ) {
280308 rawLogLevel = System .getenv (BigQueryJdbcUrlUtility .LOG_LEVEL_ENV_VAR );
281309 }
282- String connLogPath = ds .getLogPath ();
283- if (connLogPath == null ) {
284- connLogPath = System .getenv (BigQueryJdbcUrlUtility .LOG_PATH_ENV_VAR );
285- }
286- if (connLogPath == null && rawLogLevel != null && !ds .getEnableGcpLogExporter ()) {
287- connLogPath = BigQueryJdbcUrlUtility .DEFAULT_LOG_PATH ;
288- }
310+ String connLogPath =
311+ BigQueryJdbcUrlUtility .resolveEffectiveLogPath (
312+ ds .getLogPath (),
313+ System .getenv (BigQueryJdbcUrlUtility .LOG_PATH_ENV_VAR ),
314+ rawLogLevel != null && !ds .getEnableGcpLogExporter ());
289315 BigQueryJdbcRootLogger .registerConnection (this .connectionId , rawLogLevel , connLogPath );
290316 boolean initialized = false ;
317+ ExecutorService createdQueryExecutor = null ;
318+ ExecutorService createdMetadataExecutor = null ;
291319 try (BigQueryJdbcMdc .MdcCloseable mdc = BigQueryJdbcMdc .registerInstance (this .connectionId )) {
292320 this .connectionUrl = url ;
293321 if (LOG .isLoggable (Level .CONFIG )) {
294322 Properties connectionProps = ds .createProperties ();
295323 Properties maskedProps = new Properties ();
296324 for (String name : connectionProps .stringPropertyNames ()) {
297- String value = connectionProps .getProperty (name );
298- if (!SAFE_TO_LOG_PROPERTIES .contains (name )) {
299- value = "*****" ;
300- }
301- maskedProps .setProperty (name , value );
325+ maskedProps .setProperty (
326+ sanitizeForLog (name ), maskPropertyValue (name , connectionProps .getProperty (name )));
302327 }
303328 LOG .config ("Connection properties: %s" , maskedProps .toString ());
304329 }
305330 this .openStatements = ConcurrentHashMap .newKeySet ();
306331 this .autoCommit = true ;
307332 this .sqlWarnings = new ArrayList <>();
333+ if (ds .getLogPath () != null && !BigQueryJdbcUrlUtility .isUrlLogPathAllowed ()) {
334+ this .sqlWarnings .add (
335+ new SQLWarning (
336+ String .format (
337+ "Ignoring URL/Properties %s because system property %s is false." ,
338+ BigQueryJdbcUrlUtility .LOG_PATH_PROPERTY_NAME ,
339+ BigQueryJdbcUrlUtility .ALLOW_URL_LOG_PATH_SYSTEM_PROPERTY )));
340+ }
308341 this .transactionStarted = false ;
309342 this .isClosed = false ;
310343
@@ -429,15 +462,27 @@ public class BigQueryConnection extends BigQueryNoOpsConnection {
429462 this .bigQuery = getBigQueryConnection ();
430463 // Cached pool executes queries immediately without queueing and reclaims all idle threads
431464 // when inactive, minimizing resources.
432- this . queryExecutor =
465+ createdQueryExecutor =
433466 BigQueryJdbcMdc .newCachedThreadPool (String .format ("BQ-Query-%s" , connectionId ));
434- this .metadataExecutor =
467+ this .queryExecutor = createdQueryExecutor ;
468+ createdMetadataExecutor =
435469 BigQueryJdbcMdc .newFixedThreadPool (
436470 String .format ("BQ-Metadata-%s" , connectionId ), this .metadataFetchThreadCount );
471+ this .metadataExecutor = createdMetadataExecutor ;
472+ BigQueryJdbcRootLogger .trackConnectionLifecycle (this , this .connectionId );
437473 initialized = true ;
438474 } finally {
439475 if (!initialized ) {
476+ if (createdQueryExecutor != null ) {
477+ createdQueryExecutor .shutdownNow ();
478+ }
479+ if (createdMetadataExecutor != null ) {
480+ createdMetadataExecutor .shutdownNow ();
481+ }
440482 BigQueryJdbcRootLogger .closeConnectionHandler (this .connectionId );
483+ BigQueryJdbcOpenTelemetry .unregisterConnection (this .connectionId );
484+ BigQueryJdbcOpenTelemetry .releaseSdk (this .openTelemetry );
485+ this .openTelemetry = null ;
441486 }
442487 }
443488 }
@@ -1237,7 +1282,9 @@ private void closeImpl() throws SQLException {
12371282 } catch (ConcurrentModificationException ex ) {
12381283 throw new BigQueryJdbcException ("Concurrent modification during close" , ex );
12391284 } finally {
1240- BigQueryJdbcMdc .clear ();
1285+ if (this .connectionId .equals (BigQueryJdbcMdc .getConnectionId ())) {
1286+ BigQueryJdbcMdc .clear ();
1287+ }
12411288 BigQueryJdbcRootLogger .closeConnectionHandler (this .connectionId );
12421289 BigQueryJdbcOpenTelemetry .unregisterConnection (this .connectionId );
12431290 BigQueryJdbcOpenTelemetry .releaseSdk (this .openTelemetry );
@@ -1354,6 +1401,7 @@ private OpenTelemetry getOpenTelemetryInstance() {
13541401 effectiveProjectId ,
13551402 this .credentials ,
13561403 this .proxyProperties );
1404+ this .openTelemetry = openTelemetry ;
13571405
13581406 boolean hasExternalOtel = this .customOpenTelemetry != null || this .useGlobalOpenTelemetry ;
13591407 Logging localLoggingClient = null ;
0 commit comments