diff --git a/beeline/src/java/org/apache/hive/beeline/BeeLineOpts.java b/beeline/src/java/org/apache/hive/beeline/BeeLineOpts.java index 7a6ee5f..d3aebc8 100644 --- a/beeline/src/java/org/apache/hive/beeline/BeeLineOpts.java +++ b/beeline/src/java/org/apache/hive/beeline/BeeLineOpts.java @@ -102,6 +102,8 @@ private Map hiveConfVariables = new HashMap(); private boolean helpAsked; + private String lastConnectedUrl = null; // stores the last successful connected-to url. + public BeeLineOpts(BeeLine beeLine, Properties props) { this.beeLine = beeLine; if (terminal.getWidth() > 0) { @@ -567,5 +569,14 @@ public void setHelpAsked(boolean helpAsked) { public boolean isHelpAsked() { return helpAsked; } + + public String getLastConnectedUrl() { + return lastConnectedUrl; + } + + public void setLastConnectedUrl(String lastConnectedUrl) { + this.lastConnectedUrl = lastConnectedUrl; + } + } diff --git a/beeline/src/java/org/apache/hive/beeline/Commands.java b/beeline/src/java/org/apache/hive/beeline/Commands.java index 0178333..4115ac7 100644 --- a/beeline/src/java/org/apache/hive/beeline/Commands.java +++ b/beeline/src/java/org/apache/hive/beeline/Commands.java @@ -310,7 +310,19 @@ public boolean dropall(String line) { public boolean reconnect(String line) { if (beeLine.getDatabaseConnection() == null || beeLine.getDatabaseConnection().getUrl() == null) { - return beeLine.error(beeLine.loc("no-current-connection")); + // First, let's try connecting using the last successful url - if that fails, then we error out. + String lastConnectedUrl = null; + if ((lastConnectedUrl = beeLine.getOpts().getLastConnectedUrl()) != null){ + Properties props = new Properties(); + props.setProperty("url",lastConnectedUrl); + try { + return connect(props); + } catch (IOException e) { + return beeLine.error(e); + } + } else { + return beeLine.error(beeLine.loc("no-current-connection")); + } } beeLine.info(beeLine.loc("reconnecting", beeLine.getDatabaseConnection().getUrl())); try { @@ -1398,6 +1410,7 @@ public boolean connect(Properties props) throws IOException { beeLine.runInit(); beeLine.setCompletions(); + beeLine.getOpts().setLastConnectedUrl(url); // save successful connected-to url return true; } catch (SQLException sqle) { beeLine.getDatabaseConnections().remove();