HIVE-10895: ObjectStore does not close Query objects in some calls, causing a 
potential leak in some metastore db resources (Aihua Xu reviewed by Chaoyu 
Tang, Sergey Shelukhin, Vaibhav Gumashta)


Project: http://git-wip-us.apache.org/repos/asf/hive/repo
Commit: http://git-wip-us.apache.org/repos/asf/hive/commit/08595ffa
Tree: http://git-wip-us.apache.org/repos/asf/hive/tree/08595ffa
Diff: http://git-wip-us.apache.org/repos/asf/hive/diff/08595ffa

Branch: refs/heads/beeline-cli
Commit: 08595ffa33d4985b43249be9b7c5a081cece2e6a
Parents: 68eab64
Author: Vaibhav Gumashta <vgumas...@apache.org>
Authored: Thu Jul 9 22:53:21 2015 +0530
Committer: Vaibhav Gumashta <vgumas...@apache.org>
Committed: Thu Jul 9 22:53:21 2015 +0530

----------------------------------------------------------------------
 .../hive/metastore/MetaStoreDirectSql.java      |   56 +-
 .../hadoop/hive/metastore/ObjectStore.java      | 1838 ++++++++++--------
 .../hive/metastore/tools/HiveMetaTool.java      |   23 +-
 .../hadoop/hive/metastore/TestObjectStore.java  |  230 +++
 4 files changed, 1330 insertions(+), 817 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hive/blob/08595ffa/metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreDirectSql.java
----------------------------------------------------------------------
diff --git 
a/metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreDirectSql.java 
b/metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreDirectSql.java
index 1c21c8b..5776ec6 100644
--- 
a/metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreDirectSql.java
+++ 
b/metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreDirectSql.java
@@ -178,25 +178,44 @@ class MetaStoreDirectSql {
 
   private boolean ensureDbInit() {
     Transaction tx = pm.currentTransaction();
+    Query dbQuery = null, tblColumnQuery = null, partColumnQuery = null;
     try {
       // Force the underlying db to initialize.
-      pm.newQuery(MDatabase.class, "name == ''").execute();
-      pm.newQuery(MTableColumnStatistics.class, "dbName == ''").execute();
-      pm.newQuery(MPartitionColumnStatistics.class, "dbName == ''").execute();
+      dbQuery = pm.newQuery(MDatabase.class, "name == ''");
+      dbQuery.execute();
+
+      tblColumnQuery = pm.newQuery(MTableColumnStatistics.class, "dbName == 
''");
+      tblColumnQuery.execute();
+
+      partColumnQuery = pm.newQuery(MPartitionColumnStatistics.class, "dbName 
== ''");
+      partColumnQuery.execute();
+
       return true;
     } catch (Exception ex) {
       LOG.warn("Database initialization failed; direct SQL is disabled", ex);
       tx.rollback();
       return false;
+    } finally {
+      if (dbQuery != null) {
+        dbQuery.closeAll();
+      }
+      if (tblColumnQuery != null) {
+        tblColumnQuery.closeAll();
+      }
+      if (partColumnQuery != null) {
+        partColumnQuery.closeAll();
+      }
     }
   }
 
   private boolean runTestQuery() {
     Transaction tx = pm.currentTransaction();
+    Query query = null;
     // Run a self-test query. If it doesn't work, we will self-disable. What a 
PITA...
     String selfTestQuery = "select \"DB_ID\" from \"DBS\"";
     try {
-      pm.newQuery("javax.jdo.query.SQL", selfTestQuery).execute();
+      query = pm.newQuery("javax.jdo.query.SQL", selfTestQuery);
+      query.execute();
       tx.commit();
       return true;
     } catch (Exception ex) {
@@ -204,6 +223,11 @@ class MetaStoreDirectSql {
       tx.rollback();
       return false;
     }
+    finally {
+      if (query != null) {
+        query.closeAll();
+      }
+    }
   }
 
   public boolean isCompatibleDatastore() {
@@ -393,14 +417,21 @@ class MetaStoreDirectSql {
   }
 
   private boolean isViewTable(String dbName, String tblName) throws 
MetaException {
-    String queryText = "select \"TBL_TYPE\" from \"TBLS\"" +
-        " inner join \"DBS\" on \"TBLS\".\"DB_ID\" = \"DBS\".\"DB_ID\" " +
-        " where \"TBLS\".\"TBL_NAME\" = ? and \"DBS\".\"NAME\" = ?";
-    Object[] params = new Object[] { tblName, dbName };
-    Query query = pm.newQuery("javax.jdo.query.SQL", queryText);
-    query.setUnique(true);
-    Object result = executeWithArray(query, params, queryText);
-    return (result != null) && 
result.toString().equals(TableType.VIRTUAL_VIEW.toString());
+    Query query = null;
+    try {
+      String queryText = "select \"TBL_TYPE\" from \"TBLS\"" +
+          " inner join \"DBS\" on \"TBLS\".\"DB_ID\" = \"DBS\".\"DB_ID\" " +
+          " where \"TBLS\".\"TBL_NAME\" = ? and \"DBS\".\"NAME\" = ?";
+      Object[] params = new Object[] { tblName, dbName };
+      query = pm.newQuery("javax.jdo.query.SQL", queryText);
+      query.setUnique(true);
+      Object result = executeWithArray(query, params, queryText);
+      return (result != null) && 
result.toString().equals(TableType.VIRTUAL_VIEW.toString());
+    } finally {
+      if (query != null) {
+        query.closeAll();
+      }
+    }
   }
 
   /**
@@ -1190,6 +1221,7 @@ class MetaStoreDirectSql {
         partsFound++;
       }
     }
+    query.closeAll();
     return partsFound;
   }
 

Reply via email to