You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hive.apache.org by se...@apache.org on 2015/08/25 23:29:15 UTC

[45/50] [abbrv] hive git commit: HIVE-11450 : Resources are not cleaned up properly at multiple places (Nezih Yigitbasi via Ashutosh Chauhan)

HIVE-11450 : Resources are not cleaned up properly at multiple places (Nezih Yigitbasi via Ashutosh Chauhan)


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

Branch: refs/heads/llap
Commit: e2a1764f07d6fa242a29c25c6cc3d1a13405d038
Parents: 9d3d3d0
Author: Nezih Yigitbasi <ny...@netflix.com>
Authored: Sun Aug 23 22:40:00 2015 -0800
Committer: Ashutosh Chauhan <ha...@apache.org>
Committed: Mon Aug 24 10:20:10 2015 -0700

----------------------------------------------------------------------
 beeline/src/java/org/apache/hive/beeline/BeeLine.java | 13 ++++++-------
 .../src/java/org/apache/hive/beeline/BeeLineOpts.java | 12 ++++++------
 .../java/org/apache/hive/beeline/HiveSchemaTool.java  | 14 ++++++++++----
 .../src/java/org/apache/hive/jdbc/HiveConnection.java | 14 ++++++--------
 4 files changed, 28 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hive/blob/e2a1764f/beeline/src/java/org/apache/hive/beeline/BeeLine.java
----------------------------------------------------------------------
diff --git a/beeline/src/java/org/apache/hive/beeline/BeeLine.java b/beeline/src/java/org/apache/hive/beeline/BeeLine.java
index 1519619..3cd2a8b 100644
--- a/beeline/src/java/org/apache/hive/beeline/BeeLine.java
+++ b/beeline/src/java/org/apache/hive/beeline/BeeLine.java
@@ -844,7 +844,7 @@ public class BeeLine implements Closeable {
     consoleReader.setExpandEvents(false);
 
     // setup history
-    ByteArrayOutputStream hist = null;
+    ByteArrayOutputStream hist = new ByteArrayOutputStream();
     if (new File(getOpts().getHistoryFile()).isFile()) {
       try {
         // save the current contents of the history buffer. This gets
@@ -852,13 +852,12 @@ public class BeeLine implements Closeable {
         // input will clobber the history input, but setting the
         // input before the output will cause the previous commands
         // to not be saved to the buffer.
-        FileInputStream historyIn = new FileInputStream(getOpts().getHistoryFile());
-        hist = new ByteArrayOutputStream();
-        int n;
-        while ((n = historyIn.read()) != -1) {
-          hist.write(n);
+        try (FileInputStream historyIn = new FileInputStream(getOpts().getHistoryFile())) {
+          int n;
+          while ((n = historyIn.read()) != -1) {
+            hist.write(n);
+          }
         }
-        historyIn.close();
       } catch (Exception e) {
         handleException(e);
       }

http://git-wip-us.apache.org/repos/asf/hive/blob/e2a1764f/beeline/src/java/org/apache/hive/beeline/BeeLineOpts.java
----------------------------------------------------------------------
diff --git a/beeline/src/java/org/apache/hive/beeline/BeeLineOpts.java b/beeline/src/java/org/apache/hive/beeline/BeeLineOpts.java
index a31c49c..3388391 100644
--- a/beeline/src/java/org/apache/hive/beeline/BeeLineOpts.java
+++ b/beeline/src/java/org/apache/hive/beeline/BeeLineOpts.java
@@ -154,9 +154,9 @@ class BeeLineOpts implements Completer {
 
 
   public void save() throws IOException {
-    OutputStream out = new FileOutputStream(rcFile);
-    save(out);
-    out.close();
+    try (OutputStream out = new FileOutputStream(rcFile)) {
+      save(out);
+    }
   }
 
   public void save(OutputStream out) throws IOException {
@@ -208,9 +208,9 @@ class BeeLineOpts implements Completer {
 
 
   public void load() throws IOException {
-    InputStream in = new FileInputStream(rcFile);
-    load(in);
-    in.close();
+    try (InputStream in = new FileInputStream(rcFile)) {
+      load(in);
+    }
   }
 
 

http://git-wip-us.apache.org/repos/asf/hive/blob/e2a1764f/beeline/src/java/org/apache/hive/beeline/HiveSchemaTool.java
----------------------------------------------------------------------
diff --git a/beeline/src/java/org/apache/hive/beeline/HiveSchemaTool.java b/beeline/src/java/org/apache/hive/beeline/HiveSchemaTool.java
index 2477e5f..d5d635a 100644
--- a/beeline/src/java/org/apache/hive/beeline/HiveSchemaTool.java
+++ b/beeline/src/java/org/apache/hive/beeline/HiveSchemaTool.java
@@ -140,18 +140,24 @@ public class HiveSchemaTool {
     } else {
       versionQuery = "select t.SCHEMA_VERSION from VERSION t";
     }
-    try {
-      Statement stmt = metastoreConn.createStatement();
-      ResultSet res = stmt.executeQuery(versionQuery);
+    try(Statement stmt = metastoreConn.createStatement();
+        ResultSet res = stmt.executeQuery(versionQuery)) {
       if (!res.next()) {
         throw new HiveMetaException("Didn't find version data in metastore");
       }
       String currentSchemaVersion = res.getString(1);
-      metastoreConn.close();
       return currentSchemaVersion;
     } catch (SQLException e) {
       throw new HiveMetaException("Failed to get schema version.", e);
     }
+    finally {
+      try {
+        metastoreConn.close();
+      } catch (SQLException e) {
+        System.err.println("Failed to close the metastore connection");
+        e.printStackTrace(System.err);
+      }
+    }
   }
 
   // test the connection metastore using the config property

http://git-wip-us.apache.org/repos/asf/hive/blob/e2a1764f/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java
----------------------------------------------------------------------
diff --git a/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java b/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java
index 3b7e004..a9dac03 100644
--- a/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java
+++ b/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java
@@ -959,15 +959,13 @@ public class HiveConnection implements java.sql.Connection {
     if (isClosed) {
       throw new SQLException("Connection is closed");
     }
-    Statement stmt = createStatement();
-    ResultSet res = stmt.executeQuery("SELECT current_database()");
-    if (!res.next()) {
-      throw new SQLException("Failed to get schema information");
+    try (Statement stmt = createStatement();
+         ResultSet res = stmt.executeQuery("SELECT current_database()")) {
+      if (!res.next()) {
+        throw new SQLException("Failed to get schema information");
+      }
+      return res.getString(1);
     }
-    String schemaName = res.getString(1);
-    res.close();
-    stmt.close();
-    return schemaName;
   }
 
   /*