diff --git a/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java b/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java index 92a0bbe806..0337ffb33b 100644 --- a/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java +++ b/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java @@ -3248,4 +3248,41 @@ public void testResultNextAcidTable() throws Exception { public void testConnectInvalidDatabase() throws SQLException { DriverManager.getConnection("jdbc:hive2:///databasedoesnotexist", "", ""); } + + @Test + public void testStatementCloseOnCompletion() throws SQLException { + Statement stmt = con.createStatement(); + stmt.closeOnCompletion(); + ResultSet res = stmt.executeQuery("select under_col from " + tableName + " limit 1"); + assertTrue(res.next()); + assertFalse(stmt.isClosed()); + assertFalse(res.next()); + assertFalse(stmt.isClosed()); + res.close(); + assertTrue(stmt.isClosed()); + } + + @Test + public void testPreparedStatementCloseOnCompletion() throws SQLException { + PreparedStatement stmt = con.prepareStatement("select under_col from " + tableName + " limit 1"); + stmt.closeOnCompletion(); + ResultSet res = stmt.executeQuery(); + assertTrue(res.next()); + assertFalse(stmt.isClosed()); + assertFalse(res.next()); + assertFalse(stmt.isClosed()); + res.close(); + assertTrue(stmt.isClosed()); + } + + @Test + public void testCloseOnAlreadyOpenedResultSetCompletion() throws Exception { + PreparedStatement stmt = con.prepareStatement("select under_col from " + tableName + " limit 1"); + ResultSet res = stmt.executeQuery(); + assertTrue(res.next()); + stmt.closeOnCompletion(); + assertFalse(stmt.isClosed()); + res.close(); + assertTrue(stmt.isClosed()); + } } diff --git a/jdbc/src/java/org/apache/hive/jdbc/HiveQueryResultSet.java b/jdbc/src/java/org/apache/hive/jdbc/HiveQueryResultSet.java index 8563cee299..df31a25ee0 100644 --- a/jdbc/src/java/org/apache/hive/jdbc/HiveQueryResultSet.java +++ b/jdbc/src/java/org/apache/hive/jdbc/HiveQueryResultSet.java @@ -276,6 +276,7 @@ public void close() throws SQLException { if (this.statement != null && (this.statement instanceof HiveStatement)) { HiveStatement s = (HiveStatement) this.statement; s.closeClientOperation(); + s.closeOnResultSetCompletion(); } else { // for those stmtHandle passed from HiveDatabaseMetaData instead of Statement closeOperationHandle(stmtHandle); diff --git a/jdbc/src/java/org/apache/hive/jdbc/HiveStatement.java b/jdbc/src/java/org/apache/hive/jdbc/HiveStatement.java index c6ac79373f..1f3687d5bc 100644 --- a/jdbc/src/java/org/apache/hive/jdbc/HiveStatement.java +++ b/jdbc/src/java/org/apache/hive/jdbc/HiveStatement.java @@ -69,6 +69,7 @@ private int fetchSize = DEFAULT_FETCH_SIZE; private boolean isScrollableResultset = false; private boolean isOperationComplete = false; + private boolean closeOnResultSetCompletion; /** * We need to keep a reference to the result set to support the following: * @@ -230,6 +231,13 @@ void closeClientOperation() throws SQLException { stmtHandle = null; } + void closeOnResultSetCompletion() throws SQLException { + if (closeOnResultSetCompletion) { + resultSet = null; + close(); + } + } + /* * (non-Javadoc) * @@ -251,7 +259,7 @@ public void close() throws SQLException { // JDK 1.7 public void closeOnCompletion() throws SQLException { - throw new SQLFeatureNotSupportedException("Method not supported"); + closeOnResultSetCompletion = true; } /* @@ -749,7 +757,7 @@ public boolean isClosed() throws SQLException { // JDK 1.7 public boolean isCloseOnCompletion() throws SQLException { - return false; + return closeOnResultSetCompletion; } /*