diff --git jdbc/src/java/org/apache/hive/jdbc/HiveStatement.java jdbc/src/java/org/apache/hive/jdbc/HiveStatement.java index 952fcde..75f3e24 100644 --- jdbc/src/java/org/apache/hive/jdbc/HiveStatement.java +++ jdbc/src/java/org/apache/hive/jdbc/HiveStatement.java @@ -111,26 +111,22 @@ public void addBatch(String sql) throws SQLException { @Override public void cancel() throws SQLException { - if (isClosed) { - throw new SQLException("Can't cancel after statement has been closed"); - } + checkConnection("cancel"); if (stmtHandle == null) { return; } - TCancelOperationReq cancelReq = new TCancelOperationReq(); - cancelReq.setOperationHandle(stmtHandle); + transportLock.lock(); try { - transportLock.lock(); + TCancelOperationReq cancelReq = new TCancelOperationReq(stmtHandle); TCancelOperationResp cancelResp = client.CancelOperation(cancelReq); Utils.verifySuccessWithInfo(cancelResp.getStatus()); } catch (SQLException e) { throw e; } catch (Exception e) { throw new SQLException(e.toString(), "08S01", e); - } - finally { + } finally { transportLock.unlock(); } } @@ -158,20 +154,19 @@ public void clearWarnings() throws SQLException { } void closeClientOperation() throws SQLException { + if (stmtHandle == null) { + return; + } + transportLock.lock(); try { - if (stmtHandle != null) { - TCloseOperationReq closeReq = new TCloseOperationReq(); - closeReq.setOperationHandle(stmtHandle); - transportLock.lock(); - TCloseOperationResp closeResp = client.CloseOperation(closeReq); - Utils.verifySuccessWithInfo(closeResp.getStatus()); - } + TCloseOperationReq closeReq = new TCloseOperationReq(stmtHandle); + TCloseOperationResp closeResp = client.CloseOperation(closeReq); + Utils.verifySuccessWithInfo(closeResp.getStatus()); } catch (SQLException e) { throw e; } catch (Exception e) { throw new SQLException(e.toString(), "08S01", e); - } - finally { + } finally { transportLock.unlock(); } stmtHandle = null; @@ -208,25 +203,24 @@ public void closeOnCompletion() throws SQLException { @Override public boolean execute(String sql) throws SQLException { - if (isClosed) { - throw new SQLException("Can't execute after statement has been closed"); + checkConnection("execute"); + + if (stmtHandle != null) { + closeClientOperation(); } + TExecuteStatementReq execReq = new TExecuteStatementReq(sessHandle, sql); + /** + * Run asynchronously whenever possible + * Currently only a SQLOperation can be run asynchronously, + * in a background operation thread + * Compilation is synchronous and execution is asynchronous + */ + execReq.setRunAsync(true); + execReq.setConfOverlay(sessConf); + + transportLock.lock(); try { - if (stmtHandle != null) { - closeClientOperation(); - } - - TExecuteStatementReq execReq = new TExecuteStatementReq(sessHandle, sql); - /** - * Run asynchronously whenever possible - * Currently only a SQLOperation can be run asynchronously, - * in a background operation thread - * Compilation is synchronous and execution is asynchronous - */ - execReq.setRunAsync(true); - execReq.setConfOverlay(sessConf); - transportLock.lock(); TExecuteStatementResp execResp = client.ExecuteStatement(execReq); Utils.verifySuccessWithInfo(execResp.getStatus()); stmtHandle = execResp.getOperationHandle(); @@ -234,8 +228,7 @@ public boolean execute(String sql) throws SQLException { throw eS; } catch (Exception ex) { throw new SQLException(ex.toString(), "08S01", ex); - } - finally { + } finally { transportLock.unlock(); } @@ -253,11 +246,7 @@ public boolean execute(String sql) throws SQLException { transportLock.lock(); try { statusResp = client.GetOperationStatus(statusReq); - } - catch (Exception e) { - throw e; - } - finally { + } finally { transportLock.unlock(); } Utils.verifySuccessWithInfo(statusResp.getStatus()); @@ -300,6 +289,12 @@ public boolean execute(String sql) throws SQLException { return true; } + private void checkConnection(String action) throws SQLException { + if (isClosed) { + throw new SQLException("Can't " + action + " after statement has been closed"); + } + } + /* * (non-Javadoc) * @@ -422,7 +417,8 @@ public Connection getConnection() throws SQLException { @Override public int getFetchDirection() throws SQLException { - throw new SQLException("Method not supported"); + checkConnection("getFetchDirection"); + return ResultSet.FETCH_FORWARD; } /* @@ -433,6 +429,7 @@ public int getFetchDirection() throws SQLException { @Override public int getFetchSize() throws SQLException { + checkConnection("getFetchSize"); return fetchSize; } @@ -466,6 +463,7 @@ public int getMaxFieldSize() throws SQLException { @Override public int getMaxRows() throws SQLException { + checkConnection("getMaxRows"); return maxRows; } @@ -510,6 +508,7 @@ public int getQueryTimeout() throws SQLException { @Override public ResultSet getResultSet() throws SQLException { + checkConnection("getResultSet"); return resultSet; } @@ -554,6 +553,7 @@ public int getResultSetType() throws SQLException { @Override public int getUpdateCount() throws SQLException { + checkConnection("getUpdateCount"); return 0; } @@ -565,6 +565,7 @@ public int getUpdateCount() throws SQLException { @Override public SQLWarning getWarnings() throws SQLException { + checkConnection("getWarnings"); return warningChain; } @@ -625,7 +626,10 @@ public void setEscapeProcessing(boolean enable) throws SQLException { @Override public void setFetchDirection(int direction) throws SQLException { - throw new SQLException("Method not supported"); + checkConnection("setFetchDirection"); + if (direction != ResultSet.FETCH_FORWARD) { + throw new SQLException("Not supported direction " + direction); + } } /* @@ -636,6 +640,7 @@ public void setFetchDirection(int direction) throws SQLException { @Override public void setFetchSize(int rows) throws SQLException { + checkConnection("setFetchSize"); fetchSize = rows; } @@ -658,6 +663,7 @@ public void setMaxFieldSize(int max) throws SQLException { @Override public void setMaxRows(int max) throws SQLException { + checkConnection("setMaxRows"); if (max < 0) { throw new SQLException("max must be >= 0"); } @@ -694,7 +700,10 @@ public void setQueryTimeout(int seconds) throws SQLException { @Override public boolean isWrapperFor(Class iface) throws SQLException { - throw new SQLException("Method not supported"); + if (iface == TCLIService.Iface.class) { + return true; + } + return false; } /* @@ -705,7 +714,10 @@ public boolean isWrapperFor(Class iface) throws SQLException { @Override public T unwrap(Class iface) throws SQLException { - throw new SQLException("Method not supported"); + if (iface == TCLIService.Iface.class) { + return (T) client; + } + throw new SQLException("Cannot unwrap to " + iface); } }