Repository: hive
Updated Branches:
  refs/heads/master 401b14ac7 -> 7000a5fbe


HIVE-15858 : Beeline ^C doesn't close the session in HTTP mode (Sankar 
Hariappan  via Thejas Nair)


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

Branch: refs/heads/master
Commit: 7000a5fbebb7c05ce8def3e823816d83d2d75f02
Parents: 401b14a
Author: Sankar Hariappan <[email protected]>
Authored: Mon Feb 13 12:23:25 2017 -0800
Committer: Thejas M Nair <[email protected]>
Committed: Mon Feb 13 12:24:01 2017 -0800

----------------------------------------------------------------------
 .../java/org/apache/hive/beeline/BeeLine.java   | 39 ++++++++++++-----
 .../apache/hive/beeline/TestShutdownHook.java   | 46 ++++++++++++++++++++
 2 files changed, 73 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hive/blob/7000a5fb/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 abeff02..3c8fccc 100644
--- a/beeline/src/java/org/apache/hive/beeline/BeeLine.java
+++ b/beeline/src/java/org/apache/hive/beeline/BeeLine.java
@@ -124,6 +124,7 @@ public class BeeLine implements Closeable {
   private static final ResourceBundle resourceBundle =
       ResourceBundle.getBundle(BeeLine.class.getSimpleName());
   private final BeeLineSignalHandler signalHandler;
+  private final Runnable shutdownHook;
   private static final String separator = System.getProperty("line.separator");
   private boolean exit = false;
   private final DatabaseConnections connections = new DatabaseConnections();
@@ -532,13 +533,26 @@ public class BeeLine implements Closeable {
   public BeeLine(boolean isBeeLine) {
     this.isBeeLine = isBeeLine;
     this.signalHandler = new SunSignalHandler(this);
+    this.shutdownHook = new Runnable() {
+      @Override
+      public void run() {
+        try {
+          if (history != null) {
+            history.flush();
+          }
+        } catch (IOException e) {
+          error(e);
+        } finally {
+          close();
+        }
+      }
+    };
   }
 
   DatabaseConnection getDatabaseConnection() {
     return getDatabaseConnections().current();
   }
 
-
   Connection getConnection() throws SQLException {
     if (getDatabaseConnections().current() == null
         || getDatabaseConnections().current().getConnection() == null) {
@@ -985,6 +999,9 @@ public class BeeLine implements Closeable {
 
     setupHistory();
 
+    //add shutdown hook to cleanup the beeline for smooth exit
+    addBeelineShutdownHook();
+
     //this method also initializes the consoleReader which is
     //needed by initArgs for certain execution paths
     ConsoleReader reader = initializeConsoleReader(inputStream);
@@ -1184,17 +1201,11 @@ public class BeeLine implements Closeable {
     }
 
     this.history = new FileHistory(new File(getOpts().getHistoryFile()));
-    // add shutdown hook to flush the history to history file
-    ShutdownHookManager.addShutdownHook(new Runnable() {
-      @Override
-      public void run() {
-        try {
-          history.flush();
-        } catch (IOException e) {
-          error(e);
-        }
-      }
-    });
+  }
+
+  private void addBeelineShutdownHook() throws IOException {
+    // add shutdown hook to flush the history to history file and it also 
close all open connections
+    ShutdownHookManager.addShutdownHook(getShutdownHook());
   }
 
   public ConsoleReader initializeConsoleReader(InputStream inputStream) throws 
IOException {
@@ -2272,6 +2283,10 @@ public class BeeLine implements Closeable {
     return connections;
   }
 
+  Runnable getShutdownHook() {
+    return shutdownHook;
+  }
+
   Completer getCommandCompletor() {
     return beeLineCommandCompleter;
   }

http://git-wip-us.apache.org/repos/asf/hive/blob/7000a5fb/beeline/src/test/org/apache/hive/beeline/TestShutdownHook.java
----------------------------------------------------------------------
diff --git a/beeline/src/test/org/apache/hive/beeline/TestShutdownHook.java 
b/beeline/src/test/org/apache/hive/beeline/TestShutdownHook.java
new file mode 100644
index 0000000..9927ef9
--- /dev/null
+++ b/beeline/src/test/org/apache/hive/beeline/TestShutdownHook.java
@@ -0,0 +1,46 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hive.beeline;
+
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.PrintStream;
+import java.io.PrintWriter;
+import java.lang.reflect.Method;
+
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class TestShutdownHook {
+  @Test
+  public void testShutdownHook() throws Exception {
+    ByteArrayOutputStream os = new ByteArrayOutputStream();
+    PrintStream ops = new PrintStream(os);
+    BeeLine beeline = new BeeLine();
+    DatabaseConnections dbConnections = beeline.getDatabaseConnections();
+    dbConnections.setConnection(new DatabaseConnection(beeline,null,null, 
null));
+    dbConnections.setConnection(new DatabaseConnection(beeline,null,null, 
null));
+    Assert.assertEquals(2, dbConnections.size());
+    beeline.setOutputStream(ops);
+    beeline.getShutdownHook().run();
+    Assert.assertEquals(0, dbConnections.size());
+  }
+}
\ No newline at end of file

Reply via email to