<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Proper way to collect Statement ID from JDBC Connection in Administration &amp; Architecture</title>
    <link>https://community.databricks.com/t5/administration-architecture/proper-way-to-collect-statement-id-from-jdbc-connection/m-p/67068#M1098</link>
    <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/9"&gt;@Retired_mod&lt;/a&gt;&amp;nbsp;for the suggestion, but I still would need the Statement ID for further analysis (as Query History performs best with direct ID). - Any way to Unwrap the java.sql.Connection interface to the implementing class and getting the metadata through it?&lt;/P&gt;</description>
    <pubDate>Tue, 23 Apr 2024 13:49:51 GMT</pubDate>
    <dc:creator>harripy</dc:creator>
    <dc:date>2024-04-23T13:49:51Z</dc:date>
    <item>
      <title>Proper way to collect Statement ID from JDBC Connection</title>
      <link>https://community.databricks.com/t5/administration-architecture/proper-way-to-collect-statement-id-from-jdbc-connection/m-p/66996#M1087</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Hi,&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;We are executing DML calls on Databricks SQL Warehouse programmatically, with Java and Python.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;There can be thousands of executions running on daily level, so in case of an error occurs, it would be very beneficial t&lt;/SPAN&gt;&lt;SPAN&gt;o spot the Statement ID of the failed execution.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;We just recently discovered that the Statement ID can be retrieved on Python SDK as follows from the connection cursor:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;if cursor and cursor.active_op_handle is not None:
    queryId = UUID(bytes= cursor.active_op_handle.operationId.guid)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;But what would be equivalent for doing the same on Java SDK?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;We are getting the Java Connection from the DriverManager.getConnection(jdbcUrl, props) -call.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 23 Apr 2024 06:10:51 GMT</pubDate>
      <guid>https://community.databricks.com/t5/administration-architecture/proper-way-to-collect-statement-id-from-jdbc-connection/m-p/66996#M1087</guid>
      <dc:creator>harripy</dc:creator>
      <dc:date>2024-04-23T06:10:51Z</dc:date>
    </item>
    <item>
      <title>Re: Proper way to collect Statement ID from JDBC Connection</title>
      <link>https://community.databricks.com/t5/administration-architecture/proper-way-to-collect-statement-id-from-jdbc-connection/m-p/67068#M1098</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/9"&gt;@Retired_mod&lt;/a&gt;&amp;nbsp;for the suggestion, but I still would need the Statement ID for further analysis (as Query History performs best with direct ID). - Any way to Unwrap the java.sql.Connection interface to the implementing class and getting the metadata through it?&lt;/P&gt;</description>
      <pubDate>Tue, 23 Apr 2024 13:49:51 GMT</pubDate>
      <guid>https://community.databricks.com/t5/administration-architecture/proper-way-to-collect-statement-id-from-jdbc-connection/m-p/67068#M1098</guid>
      <dc:creator>harripy</dc:creator>
      <dc:date>2024-04-23T13:49:51Z</dc:date>
    </item>
    <item>
      <title>Re: Proper way to collect Statement ID from JDBC Connection</title>
      <link>https://community.databricks.com/t5/administration-architecture/proper-way-to-collect-statement-id-from-jdbc-connection/m-p/112406#M3112</link>
      <description>&lt;P&gt;Found a way to extract it for below dbx-java library.&lt;/P&gt;&lt;P&gt;-&amp;gt; databricks-jdbc library version : `2.6.32`&lt;/P&gt;&lt;LI-CODE lang="java"&gt;    private static String extractQueryIdFromDbxStatement(Hive42PreparedStatement statement) {
        byte[] guid = ((HiveJDBCNativeQueryExecutor) statement.getQueryExecutor())
                .getExeContext()
                .m_fetchResultsReq
                .getOperationHandle()
                .getOperationId()
                .getGuid();


        return (guid != null &amp;amp;&amp;amp; guid.length &amp;gt; 0) ?
                formatGuid(HexFormat.of().formatHex(guid)) :
                EMPTY_STRING;
    }

    private static String formatGuid(String input) {
        if (input.length() != 32) return input;
        return String.format("%s-%s-%s-%s-%s",
                input.substring(0, 8),
                input.substring(8, 12),
                input.substring(12, 16),
                input.substring(16, 20),
                input.substring(20, 32));
    }&lt;/LI-CODE&gt;&lt;P&gt;In short - get preparedStatement from the connection obtained from DriverManager above like&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;PRE&gt;PreparedStatement pstmt = conn.prepareStatement&lt;SPAN&gt;(&lt;/SPAN&gt;query&lt;SPAN&gt;)&lt;/SPAN&gt;;&lt;/PRE&gt;&lt;P&gt;&amp;amp; then pass preapredStatement to `extractQueryIdFromDbxStatement` method shown above - which will either return formatted statement-id or a empty string if not found.&lt;/P&gt;&lt;P&gt;To be on safer side - use try/catch for extracting &amp;amp; handle errors as your desire.&lt;/P&gt;&lt;/DIV&gt;</description>
      <pubDate>Wed, 12 Mar 2025 17:57:17 GMT</pubDate>
      <guid>https://community.databricks.com/t5/administration-architecture/proper-way-to-collect-statement-id-from-jdbc-connection/m-p/112406#M3112</guid>
      <dc:creator>oprime</dc:creator>
      <dc:date>2025-03-12T17:57:17Z</dc:date>
    </item>
  </channel>
</rss>

