diff --git beeline/src/java/org/apache/hive/beeline/BeeLine.java beeline/src/java/org/apache/hive/beeline/BeeLine.java index 1519619..3cd2a8b 100644 --- beeline/src/java/org/apache/hive/beeline/BeeLine.java +++ beeline/src/java/org/apache/hive/beeline/BeeLine.java @@ -844,7 +844,7 @@ public ConsoleReader getConsoleReader(InputStream inputStream) throws IOExceptio 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 ConsoleReader getConsoleReader(InputStream inputStream) throws IOExceptio // 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); } diff --git beeline/src/java/org/apache/hive/beeline/BeeLineOpts.java beeline/src/java/org/apache/hive/beeline/BeeLineOpts.java index a31c49c..3388391 100644 --- beeline/src/java/org/apache/hive/beeline/BeeLineOpts.java +++ beeline/src/java/org/apache/hive/beeline/BeeLineOpts.java @@ -154,9 +154,9 @@ public int complete(String buf, int pos, List cand) { 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 @@ public Properties toProperties() public void load() throws IOException { - InputStream in = new FileInputStream(rcFile); - load(in); - in.close(); + try (InputStream in = new FileInputStream(rcFile)) { + load(in); + } } diff --git beeline/src/java/org/apache/hive/beeline/HiveSchemaTool.java beeline/src/java/org/apache/hive/beeline/HiveSchemaTool.java index 2477e5f..d5d635a 100644 --- beeline/src/java/org/apache/hive/beeline/HiveSchemaTool.java +++ beeline/src/java/org/apache/hive/beeline/HiveSchemaTool.java @@ -140,18 +140,24 @@ private String getMetaStoreSchemaVersion(Connection metastoreConn) } 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 diff --git jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java index bb2b695..e0248f1 100644 --- jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java +++ jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java @@ -959,15 +959,13 @@ public String getSchema() throws SQLException { 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; } /*