This is an automated email from the ASF dual-hosted git repository.

huaxingao pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg.git


The following commit(s) were added to refs/heads/main by this push:
     new bb37293484 Fix JDBC resource leaks in JdbcCatalog and JdbcUtil (#15463)
bb37293484 is described below

commit bb37293484468f0502de9986955860505aef9776
Author: ZIHAN DAI <[email protected]>
AuthorDate: Tue Mar 17 11:12:26 2026 +1100

    Fix JDBC resource leaks in JdbcCatalog and JdbcUtil (#15463)
    
    * Fix JDBC resource leaks in JdbcCatalog and JdbcUtil
    
    Use try-with-resources for ResultSet and PreparedStatement objects
    in JdbcUtil.tableOrView(), JdbcCatalog.atomicCreateTable(), and
    JdbcCatalog.updateSchemaIfRequired() to ensure they are always
    closed even when exceptions occur.
    
    Closes #15462
    
    * Fix checkstyle: extract nested try into helper method 
executeV1CatalogUpdate
---
 .../java/org/apache/iceberg/jdbc/JdbcCatalog.java  | 58 ++++++++++++----------
 .../java/org/apache/iceberg/jdbc/JdbcUtil.java     | 26 +++++-----
 2 files changed, 45 insertions(+), 39 deletions(-)

diff --git a/core/src/main/java/org/apache/iceberg/jdbc/JdbcCatalog.java 
b/core/src/main/java/org/apache/iceberg/jdbc/JdbcCatalog.java
index a3f40512a0..55c00319a0 100644
--- a/core/src/main/java/org/apache/iceberg/jdbc/JdbcCatalog.java
+++ b/core/src/main/java/org/apache/iceberg/jdbc/JdbcCatalog.java
@@ -20,6 +20,7 @@ package org.apache.iceberg.jdbc;
 
 import java.io.IOException;
 import java.io.UncheckedIOException;
+import java.sql.Connection;
 import java.sql.DatabaseMetaData;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
@@ -165,13 +166,12 @@ public class JdbcCatalog extends BaseMetastoreViewCatalog
           // check the existence of a table name
           Predicate<String> tableTest =
               name -> {
-                try {
-                  ResultSet result =
-                      dbMeta.getTables(
-                          null /* catalog name */,
-                          null /* schemaPattern */,
-                          name /* tableNamePattern */,
-                          null /* types */);
+                try (ResultSet result =
+                    dbMeta.getTables(
+                        null /* catalog name */,
+                        null /* schemaPattern */,
+                        name /* tableNamePattern */,
+                        null /* types */)) {
                   return result.next();
                 } catch (SQLException e) {
                   return false;
@@ -187,8 +187,8 @@ public class JdbcCatalog extends BaseMetastoreViewCatalog
           }
 
           LOG.debug("Creating table {} {}", tableName, reason);
-          try {
-            conn.prepareStatement(sqlCommand).execute();
+          try (PreparedStatement stmt = conn.prepareStatement(sqlCommand)) {
+            stmt.execute();
             return true;
           } catch (SQLException e) {
             // see if table was created by another thread or process.
@@ -229,25 +229,27 @@ public class JdbcCatalog extends BaseMetastoreViewCatalog
       connections.run(
           conn -> {
             DatabaseMetaData dbMeta = conn.getMetaData();
-            ResultSet typeColumn =
+            try (ResultSet typeColumn =
                 dbMeta.getColumns(
-                    null, null, JdbcUtil.CATALOG_TABLE_VIEW_NAME, 
JdbcUtil.RECORD_TYPE);
-            if (typeColumn.next()) {
-              LOG.debug("{} already supports views", 
JdbcUtil.CATALOG_TABLE_VIEW_NAME);
-              schemaVersion = JdbcUtil.SchemaVersion.V1;
-              return true;
-            } else {
-              if (PropertyUtil.propertyAsString(
-                      catalogProperties,
-                      JdbcUtil.SCHEMA_VERSION_PROPERTY,
-                      JdbcUtil.SchemaVersion.V0.name())
-                  .equalsIgnoreCase(JdbcUtil.SchemaVersion.V1.name())) {
-                LOG.debug("{} is being updated to support views", 
JdbcUtil.CATALOG_TABLE_VIEW_NAME);
+                    null, null, JdbcUtil.CATALOG_TABLE_VIEW_NAME, 
JdbcUtil.RECORD_TYPE)) {
+              if (typeColumn.next()) {
+                LOG.debug("{} already supports views", 
JdbcUtil.CATALOG_TABLE_VIEW_NAME);
                 schemaVersion = JdbcUtil.SchemaVersion.V1;
-                return 
conn.prepareStatement(JdbcUtil.V1_UPDATE_CATALOG_SQL).execute();
-              } else {
-                LOG.warn(VIEW_WARNING_LOG_MESSAGE);
                 return true;
+              } else {
+                if (PropertyUtil.propertyAsString(
+                        catalogProperties,
+                        JdbcUtil.SCHEMA_VERSION_PROPERTY,
+                        JdbcUtil.SchemaVersion.V0.name())
+                    .equalsIgnoreCase(JdbcUtil.SchemaVersion.V1.name())) {
+                  LOG.debug(
+                      "{} is being updated to support views", 
JdbcUtil.CATALOG_TABLE_VIEW_NAME);
+                  schemaVersion = JdbcUtil.SchemaVersion.V1;
+                  return executeV1CatalogUpdate(conn);
+                } else {
+                  LOG.warn(VIEW_WARNING_LOG_MESSAGE);
+                  return true;
+                }
               }
             }
           });
@@ -263,6 +265,12 @@ public class JdbcCatalog extends BaseMetastoreViewCatalog
     }
   }
 
+  private static boolean executeV1CatalogUpdate(Connection conn) throws 
SQLException {
+    try (PreparedStatement stmt = 
conn.prepareStatement(JdbcUtil.V1_UPDATE_CATALOG_SQL)) {
+      return stmt.execute();
+    }
+  }
+
   @Override
   protected TableOperations newTableOps(TableIdentifier tableIdentifier) {
     return new JdbcTableOperations(
diff --git a/core/src/main/java/org/apache/iceberg/jdbc/JdbcUtil.java 
b/core/src/main/java/org/apache/iceberg/jdbc/JdbcUtil.java
index d59da3ad04..259bd78125 100644
--- a/core/src/main/java/org/apache/iceberg/jdbc/JdbcUtil.java
+++ b/core/src/main/java/org/apache/iceberg/jdbc/JdbcUtil.java
@@ -614,21 +614,19 @@ final class JdbcUtil {
             sql.setString(1, catalogName);
             sql.setString(2, namespaceToString(identifier.namespace()));
             sql.setString(3, identifier.name());
-            ResultSet rs = sql.executeQuery();
-
-            if (rs.next()) {
-              tableOrView.put(CATALOG_NAME, rs.getString(CATALOG_NAME));
-              tableOrView.put(TABLE_NAMESPACE, rs.getString(TABLE_NAMESPACE));
-              tableOrView.put(TABLE_NAME, rs.getString(TABLE_NAME));
-              tableOrView.put(
-                  BaseMetastoreTableOperations.METADATA_LOCATION_PROP,
-                  
rs.getString(BaseMetastoreTableOperations.METADATA_LOCATION_PROP));
-              tableOrView.put(
-                  BaseMetastoreTableOperations.PREVIOUS_METADATA_LOCATION_PROP,
-                  
rs.getString(BaseMetastoreTableOperations.PREVIOUS_METADATA_LOCATION_PROP));
+            try (ResultSet rs = sql.executeQuery()) {
+              if (rs.next()) {
+                tableOrView.put(CATALOG_NAME, rs.getString(CATALOG_NAME));
+                tableOrView.put(TABLE_NAMESPACE, 
rs.getString(TABLE_NAMESPACE));
+                tableOrView.put(TABLE_NAME, rs.getString(TABLE_NAME));
+                tableOrView.put(
+                    BaseMetastoreTableOperations.METADATA_LOCATION_PROP,
+                    
rs.getString(BaseMetastoreTableOperations.METADATA_LOCATION_PROP));
+                tableOrView.put(
+                    
BaseMetastoreTableOperations.PREVIOUS_METADATA_LOCATION_PROP,
+                    
rs.getString(BaseMetastoreTableOperations.PREVIOUS_METADATA_LOCATION_PROP));
+              }
             }
-
-            rs.close();
           }
 
           return tableOrView;

Reply via email to