METAMODEL-1208: Fixed closing of JDBC ResultSets when getting metadata.
authorlixiaobao <lixiaobao0202@gmail.com>
Mon, 7 Jan 2019 17:04:40 +0000 (09:04 -0800)
committerKasper Sørensen <i.am.kasper.sorensen@gmail.com>
Mon, 7 Jan 2019 17:04:40 +0000 (09:04 -0800)
CHANGES.md
jdbc/src/main/java/org/apache/metamodel/jdbc/JdbcDataContext.java

index 2cf3249..4c12dbf 100644 (file)
@@ -1,3 +1,7 @@
+### Apache MetaModel 5.2.1 (WIP)
+
+ * [METAMODEL-1208] - Fixed closing of JDBC ResultSets when getting certain metadata.
+
 ### Apache MetaModel 5.2.0
 
  * [METAMODEL-1205] - Support for Java / JDK versions 8, 9, 10 and 11.
index f49c0ba..1668438 100644 (file)
@@ -289,7 +289,7 @@ public class JdbcDataContext extends AbstractDataContext implements UpdateableDa
         } catch (SQLException e) {
             throw JdbcUtils.wrapException(e, "retrieve schema and catalog metadata", JdbcActionType.METADATA);
         } finally {
-            close(null);
+            FileHelper.safeClose(rs);
         }
         return result;
     }
@@ -580,6 +580,7 @@ public class JdbcDataContext extends AbstractDataContext implements UpdateableDa
         } catch (SQLException e) {
             logger.error("Error retrieving catalog metadata", e);
         } finally {
+            FileHelper.safeClose(rs);
             close(connection);
             logger.debug("Retrieved {} catalogs", catalogs.size());
         }
@@ -752,9 +753,14 @@ public class JdbcDataContext extends AbstractDataContext implements UpdateableDa
         // Distinct schema names. metaData.getTables() is a denormalized
         // resultset
         Set<String> schemas = new HashSet<>();
-        ResultSet rs = metaData.getTables(_catalogName, null, null, JdbcUtils.getTableTypesAsStrings(_tableTypes));
-        while (rs.next()) {
-            schemas.add(rs.getString("TABLE_SCHEM"));
+        ResultSet rs = null;
+        try {
+            rs = metaData.getTables(_catalogName, null, null, JdbcUtils.getTableTypesAsStrings(_tableTypes));
+            while (rs.next()) {
+                schemas.add(rs.getString("TABLE_SCHEM"));
+            }
+        } finally {
+            FileHelper.safeClose(rs);
         }
         return schemas;
     }
@@ -791,13 +797,17 @@ public class JdbcDataContext extends AbstractDataContext implements UpdateableDa
                     result.add(name);
                 }
             } else {
-                ResultSet rs = metaData.getSchemas();
-                while (rs.next()) {
-                    String schemaName = rs.getString(1);
-                    logger.debug("Found schemaName: {}", schemaName);
-                    result.add(schemaName);
+                ResultSet rs = null;
+                try {
+                    rs = metaData.getSchemas();
+                    while (rs.next()) {
+                        String schemaName = rs.getString(1);
+                        logger.debug("Found schemaName: {}", schemaName);
+                        result.add(schemaName);
+                    }
+                } finally {
+                    FileHelper.safeClose(rs);
                 }
-                rs.close();
             }
 
             if (DATABASE_PRODUCT_MYSQL.equals(_databaseProductName)) {