@@ -110,6 +110,7 @@ public ImmutableSessionInfo createSession(
110110 }
111111
112112 String sessionId = byteBufferToString (response .sessionHandle .getSessionId ().guid );
113+ DatabricksThreadContextHolder .setSessionId (sessionId );
113114 LOGGER .debug ("Session created with ID {}" , sessionId );
114115 return ImmutableSessionInfo .builder ()
115116 .sessionId (sessionId )
@@ -123,6 +124,7 @@ public void deleteSession(ImmutableSessionInfo sessionInfo) throws DatabricksSQL
123124 LOGGER .debug (
124125 String .format (
125126 "public void deleteSession(Session session = {%s}))" , sessionInfo .toString ()));
127+ DatabricksThreadContextHolder .setSessionId (sessionInfo .sessionId ());
126128 TCloseSessionReq closeSessionReq =
127129 new TCloseSessionReq ().setSessionHandle (sessionInfo .sessionHandle ());
128130 TCloseSessionResp response =
@@ -191,7 +193,7 @@ private TExecuteStatementReq getRequest(
191193 IDatabricksStatementInternal parentStatement ,
192194 boolean runAsync )
193195 throws SQLException {
194-
196+ DatabricksThreadContextHolder . setSessionId ( session . getSessionId ());
195197 TSparkArrowTypes arrowNativeTypes = new TSparkArrowTypes ().setTimestampAsArrow (true );
196198
197199 // Convert the parameters to a list of TSparkParameter objects.
@@ -247,6 +249,7 @@ public void closeStatement(StatementId statementId) throws DatabricksSQLExceptio
247249 String .format (
248250 "public void closeStatement(String statementId = {%s}) using Thrift client" ,
249251 statementId ));
252+ DatabricksThreadContextHolder .setStatementId (statementId );
250253 TCloseOperationReq request =
251254 new TCloseOperationReq ().setOperationHandle (getOperationHandle (statementId ));
252255 TCloseOperationResp resp = thriftAccessor .closeOperation (request );
@@ -259,6 +262,7 @@ public void cancelStatement(StatementId statementId) throws DatabricksSQLExcepti
259262 String .format (
260263 "public void cancelStatement(String statementId = {%s}) using Thrift client" ,
261264 statementId ));
265+ DatabricksThreadContextHolder .setStatementId (statementId );
262266 TCancelOperationReq request =
263267 new TCancelOperationReq ().setOperationHandle (getOperationHandle (statementId ));
264268 TCancelOperationResp resp = thriftAccessor .cancelOperation (request );
@@ -275,6 +279,8 @@ public DatabricksResultSet getStatementResult(
275279 String .format (
276280 "public DatabricksResultSet getStatementResult(String statementId = {%s}) using Thrift client" ,
277281 statementId ));
282+ DatabricksThreadContextHolder .setStatementId (statementId );
283+ DatabricksThreadContextHolder .setSessionId (session .getSessionId ());
278284 return thriftAccessor .getStatementResult (
279285 getOperationHandle (statementId ), parentStatement , session );
280286 }
@@ -287,6 +293,7 @@ public Collection<ExternalLink> getResultChunks(StatementId statementId, long ch
287293 "public Optional<ExternalLink> getResultChunk(String statementId = {%s}, long chunkIndex = {%s}) using Thrift client" ,
288294 statementId , chunkIndex );
289295 LOGGER .debug (context );
296+ DatabricksThreadContextHolder .setStatementId (statementId );
290297 TFetchResultsResp fetchResultsResp ;
291298 List <ExternalLink > externalLinks = new ArrayList <>();
292299 AtomicInteger index = new AtomicInteger (0 );
@@ -318,6 +325,7 @@ public DatabricksResultSet listCatalogs(IDatabricksSession session) throws SQLEx
318325 String context =
319326 String .format ("Fetching catalogs using Thrift client. Session {%s}" , session .toString ());
320327 LOGGER .debug (context );
328+ DatabricksThreadContextHolder .setSessionId (session .getSessionId ());
321329 TGetCatalogsReq request =
322330 new TGetCatalogsReq ()
323331 .setSessionHandle (Objects .requireNonNull (session .getSessionInfo ()).sessionHandle ());
@@ -337,6 +345,7 @@ public DatabricksResultSet listSchemas(
337345 "Fetching schemas using Thrift client. Session {%s}, catalog {%s}, schemaNamePattern {%s}" ,
338346 session .toString (), catalog , schemaNamePattern );
339347 LOGGER .debug (context );
348+ DatabricksThreadContextHolder .setSessionId (session .getSessionId ());
340349 TGetSchemasReq request =
341350 new TGetSchemasReq ()
342351 .setSessionHandle (Objects .requireNonNull (session .getSessionInfo ()).sessionHandle ())
@@ -365,6 +374,7 @@ public DatabricksResultSet listTables(
365374 "Fetching tables using Thrift client. Session {%s}, catalog {%s}, schemaNamePattern {%s}, tableNamePattern {%s}" ,
366375 session .toString (), catalog , schemaNamePattern , tableNamePattern );
367376 LOGGER .debug (context );
377+ DatabricksThreadContextHolder .setSessionId (session .getSessionId ());
368378 TGetTablesReq request =
369379 new TGetTablesReq ()
370380 .setSessionHandle (Objects .requireNonNull (session .getSessionInfo ()).sessionHandle ())
@@ -387,6 +397,7 @@ public DatabricksResultSet listTableTypes(IDatabricksSession session) {
387397 LOGGER .debug (
388398 String .format (
389399 "Fetching table types using Thrift client. Session {%s}" , session .toString ()));
400+ DatabricksThreadContextHolder .setSessionId (session .getSessionId ());
390401 return metadataResultSetBuilder .getTableTypesResult ();
391402 }
392403
@@ -403,6 +414,7 @@ public DatabricksResultSet listColumns(
403414 "Fetching columns using Thrift client. Session {%s}, catalog {%s}, schemaNamePattern {%s}, tableNamePattern {%s}, columnNamePattern {%s}" ,
404415 session .toString (), catalog , schemaNamePattern , tableNamePattern , columnNamePattern );
405416 LOGGER .debug (context );
417+ DatabricksThreadContextHolder .setSessionId (session .getSessionId ());
406418 TGetColumnsReq request =
407419 new TGetColumnsReq ()
408420 .setSessionHandle (Objects .requireNonNull (session .getSessionInfo ()).sessionHandle ())
@@ -429,6 +441,7 @@ public DatabricksResultSet listFunctions(
429441 String .format (
430442 "Fetching functions using Thrift client. Session {%s}, catalog {%s}, schemaNamePattern {%s}, functionNamePattern {%s}." ,
431443 session .toString (), catalog , schemaNamePattern , functionNamePattern );
444+ DatabricksThreadContextHolder .setSessionId (session .getSessionId ());
432445 LOGGER .debug (context );
433446 TGetFunctionsReq request =
434447 new TGetFunctionsReq ()
@@ -452,6 +465,7 @@ public DatabricksResultSet listPrimaryKeys(
452465 "Fetching primary keys using Thrift client. session {%s}, catalog {%s}, schema {%s}, table {%s}" ,
453466 session .toString (), catalog , schema , table );
454467 LOGGER .debug (context );
468+ DatabricksThreadContextHolder .setSessionId (session .getSessionId ());
455469 TGetPrimaryKeysReq request =
456470 new TGetPrimaryKeysReq ()
457471 .setSessionHandle (Objects .requireNonNull (session .getSessionInfo ()).sessionHandle ())
@@ -474,6 +488,7 @@ public DatabricksResultSet listImportedKeys(
474488 "Fetching imported keys using Thrift client for session {%s}, catalog {%s}, schema {%s}, table {%s}" ,
475489 session .toString (), catalog , schema , table );
476490 LOGGER .debug (context );
491+ DatabricksThreadContextHolder .setSessionId (session .getSessionId ());
477492 // GetImportedKeys is implemented using GetCrossReferences
478493 // When only foreign table name is provided, we get imported keys
479494 TGetCrossReferenceReq request =
0 commit comments