Repository: calcite-avatica
Updated Branches:
  refs/heads/master 4105a1861 -> 50d45f989


[CALCITE-1520] Implement isValid for AvaticaConnection

Closes #26

Signed-off-by: Josh Elser <els...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/calcite-avatica/repo
Commit: http://git-wip-us.apache.org/repos/asf/calcite-avatica/commit/50d45f98
Tree: http://git-wip-us.apache.org/repos/asf/calcite-avatica/tree/50d45f98
Diff: http://git-wip-us.apache.org/repos/asf/calcite-avatica/diff/50d45f98

Branch: refs/heads/master
Commit: 50d45f989ee7a8eae0456dee7a88c5fa4d9d8b5a
Parents: 4105a18
Author: Kevin Risden <kris...@apache.org>
Authored: Mon Mar 5 21:44:38 2018 -0600
Committer: Josh Elser <els...@apache.org>
Committed: Mon Mar 12 17:30:52 2018 -0400

----------------------------------------------------------------------
 .../calcite/avatica/AvaticaConnection.java      |  81 ++++----
 .../avatica/AvaticaDatabaseMetaData.java        |  22 +-
 .../avatica/AvaticaPreparedStatement.java       |   4 +-
 .../calcite/avatica/AvaticaResultSet.java       | 202 +++++++++----------
 .../avatica/AvaticaResultSetMetaData.java       |   2 +-
 .../calcite/avatica/AvaticaStatement.java       |  54 ++---
 .../calcite/avatica/AvaticaConnectionTest.java  |  32 ++-
 .../calcite/avatica/RemoteDriverTest.java       |  28 +++
 8 files changed, 237 insertions(+), 188 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/50d45f98/core/src/main/java/org/apache/calcite/avatica/AvaticaConnection.java
----------------------------------------------------------------------
diff --git 
a/core/src/main/java/org/apache/calcite/avatica/AvaticaConnection.java 
b/core/src/main/java/org/apache/calcite/avatica/AvaticaConnection.java
index 21720cc..e9d10e2 100644
--- a/core/src/main/java/org/apache/calcite/avatica/AvaticaConnection.java
+++ b/core/src/main/java/org/apache/calcite/avatica/AvaticaConnection.java
@@ -75,6 +75,8 @@ public abstract class AvaticaConnection implements Connection 
{
    * a useful place to define a suggested value. */
   public static final String PLAN_COLUMN_NAME = "PLAN";
 
+  public static final Helper HELPER = Helper.INSTANCE;
+
   protected int statementCount;
   private boolean closed;
   private int holdability;
@@ -90,10 +92,8 @@ public abstract class AvaticaConnection implements 
Connection {
   protected final Properties info;
   protected final Meta meta;
   protected final AvaticaSpecificDatabaseMetaData metaData;
-  public final Helper helper = Helper.INSTANCE;
   public final Map<InternalProperty, Object> properties = new HashMap<>();
-  public final Map<Integer, AvaticaStatement> statementMap =
-      new ConcurrentHashMap<>();
+  public final Map<Integer, AvaticaStatement> statementMap = new 
ConcurrentHashMap<>();
   final Map<Integer, AtomicBoolean> flagMap = new ConcurrentHashMap<>();
   protected final long maxRetriesPerExecute;
 
@@ -171,11 +171,11 @@ public abstract class AvaticaConnection implements 
Connection {
   }
 
   public CallableStatement prepareCall(String sql) throws SQLException {
-    throw helper.unsupported();
+    throw HELPER.unsupported();
   }
 
   public String nativeSQL(String sql) throws SQLException {
-    throw helper.unsupported();
+    throw HELPER.unsupported();
   }
 
   public void setAutoCommit(boolean autoCommit) throws SQLException {
@@ -207,7 +207,7 @@ public abstract class AvaticaConnection implements 
Connection {
           kerberosConnection.stopRenewalThread();
         }
       } catch (RuntimeException e) {
-        throw helper.createException("While closing connection", e);
+        throw HELPER.createException("While closing connection", e);
       }
     }
   }
@@ -272,15 +272,15 @@ public abstract class AvaticaConnection implements 
Connection {
       String sql,
       int resultSetType,
       int resultSetConcurrency) throws SQLException {
-    throw helper.unsupported();
+    throw HELPER.unsupported();
   }
 
   public Map<String, Class<?>> getTypeMap() throws SQLException {
-    throw helper.unsupported();
+    throw HELPER.unsupported();
   }
 
   public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
-    throw helper.unsupported();
+    throw HELPER.unsupported();
   }
 
   public void setHoldability(int holdability) throws SQLException {
@@ -296,19 +296,19 @@ public abstract class AvaticaConnection implements 
Connection {
   }
 
   public Savepoint setSavepoint() throws SQLException {
-    throw helper.unsupported();
+    throw HELPER.unsupported();
   }
 
   public Savepoint setSavepoint(String name) throws SQLException {
-    throw helper.unsupported();
+    throw HELPER.unsupported();
   }
 
   public void rollback(Savepoint savepoint) throws SQLException {
-    throw helper.unsupported();
+    throw HELPER.unsupported();
   }
 
   public void releaseSavepoint(Savepoint savepoint) throws SQLException {
-    throw helper.unsupported();
+    throw HELPER.unsupported();
   }
 
   public AvaticaStatement createStatement(
@@ -329,7 +329,7 @@ public abstract class AvaticaConnection implements 
Connection {
       return factory.newPreparedStatement(this, h, h.signature, resultSetType,
           resultSetConcurrency, resultSetHoldability);
     } catch (RuntimeException e) {
-      throw helper.createException("while preparing SQL: " + sql, e);
+      throw HELPER.createException("while preparing SQL: " + sql, e);
     }
   }
 
@@ -338,60 +338,65 @@ public abstract class AvaticaConnection implements 
Connection {
       int resultSetType,
       int resultSetConcurrency,
       int resultSetHoldability) throws SQLException {
-    throw helper.unsupported();
+    throw HELPER.unsupported();
   }
 
   public PreparedStatement prepareStatement(
       String sql, int autoGeneratedKeys) throws SQLException {
-    throw helper.unsupported();
+    throw HELPER.unsupported();
   }
 
   public PreparedStatement prepareStatement(
       String sql, int[] columnIndexes) throws SQLException {
-    throw helper.unsupported();
+    throw HELPER.unsupported();
   }
 
   public PreparedStatement prepareStatement(
       String sql, String[] columnNames) throws SQLException {
-    throw helper.unsupported();
+    throw HELPER.unsupported();
   }
 
   public Clob createClob() throws SQLException {
-    throw helper.unsupported();
+    throw HELPER.unsupported();
   }
 
   public Blob createBlob() throws SQLException {
-    throw helper.unsupported();
+    throw HELPER.unsupported();
   }
 
   public NClob createNClob() throws SQLException {
-    throw helper.unsupported();
+    throw HELPER.unsupported();
   }
 
   public SQLXML createSQLXML() throws SQLException {
-    throw helper.unsupported();
+    throw HELPER.unsupported();
   }
 
   public boolean isValid(int timeout) throws SQLException {
-    throw helper.unsupported();
+    if (timeout < 0) {
+      throw HELPER.createException("timeout is less than 0");
+    }
+
+    // TODO check if connection is actually alive using timeout
+    return !isClosed();
   }
 
   public void setClientInfo(String name, String value)
       throws SQLClientInfoException {
-    throw helper.clientInfo();
+    throw HELPER.clientInfo();
   }
 
   public void setClientInfo(Properties properties)
       throws SQLClientInfoException {
-    throw helper.clientInfo();
+    throw HELPER.clientInfo();
   }
 
   public String getClientInfo(String name) throws SQLException {
-    throw helper.unsupported();
+    throw HELPER.unsupported();
   }
 
   public Properties getClientInfo() throws SQLException {
-    throw helper.unsupported();
+    throw HELPER.unsupported();
   }
 
   public Array createArrayOf(String typeName, Object[] elements) throws 
SQLException {
@@ -407,10 +412,10 @@ public abstract class AvaticaConnection implements 
Connection {
     switch (type) {
     case ARRAY:
       // TODO: Nested ARRAYs
-      throw helper.createException("Cannot create an ARRAY of ARRAY's");
+      throw HELPER.createException("Cannot create an ARRAY of ARRAY's");
     case STRUCT:
       // TODO: ARRAYs of STRUCTs
-      throw helper.createException("Cannot create an ARRAY of STRUCT's");
+      throw HELPER.createException("Cannot create an ARRAY of STRUCT's");
     default:
       // This is an ARRAY, we need to use Objects, not primitives (nullable).
       avaticaType = ColumnMetaData.scalar(type.id, typeName, 
Rep.nonPrimitiveRepOf(type));
@@ -421,7 +426,7 @@ public abstract class AvaticaConnection implements 
Connection {
 
   public Struct createStruct(String typeName, Object[] attributes)
       throws SQLException {
-    throw helper.unsupported();
+    throw HELPER.unsupported();
   }
 
   public void setSchema(String schema) throws SQLException {
@@ -433,7 +438,7 @@ public abstract class AvaticaConnection implements 
Connection {
   }
 
   public void abort(Executor executor) throws SQLException {
-    throw helper.unsupported();
+    throw HELPER.unsupported();
   }
 
   public void setNetworkTimeout(
@@ -449,7 +454,7 @@ public abstract class AvaticaConnection implements 
Connection {
     if (iface.isInstance(this)) {
       return iface.cast(this);
     }
-    throw helper.createException(
+    throw HELPER.createException(
         "does not implement '" + iface + "'");
   }
 
@@ -492,7 +497,7 @@ public abstract class AvaticaConnection implements 
Connection {
         try {
           rs.close();
         } catch (Exception e) {
-          throw helper.createException(
+          throw HELPER.createException(
               "Error while closing previous result set", e);
         }
       }
@@ -517,7 +522,7 @@ public abstract class AvaticaConnection implements 
Connection {
         }
       } catch (Exception e) {
         e.printStackTrace();
-        throw helper.createException(e.getMessage(), e);
+        throw HELPER.createException(e.getMessage(), e);
       }
 
       final TimeZone timeZone = getTimeZone();
@@ -537,7 +542,7 @@ public abstract class AvaticaConnection implements 
Connection {
         isUpdateCapable(statement);
       }
     } catch (Exception e) {
-      throw helper.createException(
+      throw HELPER.createException(
           "exception while executing query: " + e.getMessage(), e);
     }
     return statement.openResultSet;
@@ -555,7 +560,7 @@ public abstract class AvaticaConnection implements 
Connection {
       // Execute it against meta
       return meta.executeBatch(handle, 
pstmt.getParameterValueBatch()).updateCounts;
     } catch (Exception e) {
-      throw helper.createException(e.getMessage(), e);
+      throw HELPER.createException(e.getMessage(), e);
     }
   }
 
@@ -584,7 +589,7 @@ public abstract class AvaticaConnection implements 
Connection {
         final List<Number> numbers = (List<Number>) obj;
         statement.updateCount = numbers.get(0).intValue();
       } else {
-        throw helper.createException("Not a valid return result.");
+        throw HELPER.createException("Not a valid return result.");
       }
       statement.openResultSet = null;
     }
@@ -606,7 +611,7 @@ public abstract class AvaticaConnection implements 
Connection {
               try {
                 rs.close();
               } catch (Exception e) {
-                throw helper.createException(
+                throw HELPER.createException(
                     "Error while closing previous result set", e);
               }
             }

http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/50d45f98/core/src/main/java/org/apache/calcite/avatica/AvaticaDatabaseMetaData.java
----------------------------------------------------------------------
diff --git 
a/core/src/main/java/org/apache/calcite/avatica/AvaticaDatabaseMetaData.java 
b/core/src/main/java/org/apache/calcite/avatica/AvaticaDatabaseMetaData.java
index 7182968..69f2c6f 100644
--- a/core/src/main/java/org/apache/calcite/avatica/AvaticaDatabaseMetaData.java
+++ b/core/src/main/java/org/apache/calcite/avatica/AvaticaDatabaseMetaData.java
@@ -1064,39 +1064,39 @@ public class AvaticaDatabaseMetaData implements 
AvaticaSpecificDatabaseMetaData
   }
 
   public boolean ownUpdatesAreVisible(int type) throws SQLException {
-    throw connection.helper.todo();
+    throw connection.HELPER.todo();
   }
 
   public boolean ownDeletesAreVisible(int type) throws SQLException {
-    throw connection.helper.todo();
+    throw connection.HELPER.todo();
   }
 
   public boolean ownInsertsAreVisible(int type) throws SQLException {
-    throw connection.helper.todo();
+    throw connection.HELPER.todo();
   }
 
   public boolean othersUpdatesAreVisible(int type) throws SQLException {
-    throw connection.helper.todo();
+    throw connection.HELPER.todo();
   }
 
   public boolean othersDeletesAreVisible(int type) throws SQLException {
-    throw connection.helper.todo();
+    throw connection.HELPER.todo();
   }
 
   public boolean othersInsertsAreVisible(int type) throws SQLException {
-    throw connection.helper.todo();
+    throw connection.HELPER.todo();
   }
 
   public boolean updatesAreDetected(int type) throws SQLException {
-    throw connection.helper.todo();
+    throw connection.HELPER.todo();
   }
 
   public boolean deletesAreDetected(int type) throws SQLException {
-    throw connection.helper.todo();
+    throw connection.HELPER.todo();
   }
 
   public boolean insertsAreDetected(int type) throws SQLException {
-    throw connection.helper.todo();
+    throw connection.HELPER.todo();
   }
 
   public boolean supportsBatchUpdates() throws SQLException {
@@ -1239,7 +1239,7 @@ public class AvaticaDatabaseMetaData implements 
AvaticaSpecificDatabaseMetaData
 
   public boolean supportsResultSetHoldability(int holdability)
       throws SQLException {
-    throw connection.helper.todo();
+    throw connection.HELPER.todo();
   }
 
   public int getResultSetHoldability() {
@@ -1411,7 +1411,7 @@ public class AvaticaDatabaseMetaData implements 
AvaticaSpecificDatabaseMetaData
       return iface.cast(getRemoteAvaticaProperties());
     }
 
-    throw connection.helper.createException(
+    throw connection.HELPER.createException(
         "does not implement '" + iface + "'");
   }
 

http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/50d45f98/core/src/main/java/org/apache/calcite/avatica/AvaticaPreparedStatement.java
----------------------------------------------------------------------
diff --git 
a/core/src/main/java/org/apache/calcite/avatica/AvaticaPreparedStatement.java 
b/core/src/main/java/org/apache/calcite/avatica/AvaticaPreparedStatement.java
index e611c9a..edeef49 100644
--- 
a/core/src/main/java/org/apache/calcite/avatica/AvaticaPreparedStatement.java
+++ 
b/core/src/main/java/org/apache/calcite/avatica/AvaticaPreparedStatement.java
@@ -334,8 +334,8 @@ public abstract class AvaticaPreparedStatement
       return getSignature().parameters.get(param - 1);
     } catch (IndexOutOfBoundsException e) {
       //noinspection ThrowableResultOfMethodCallIgnored
-      throw connection.helper.toSQLException(
-          connection.helper.createException(
+      throw connection.HELPER.toSQLException(
+          connection.HELPER.createException(
               "parameter ordinal " + param + " out of range"));
     }
   }

http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/50d45f98/core/src/main/java/org/apache/calcite/avatica/AvaticaResultSet.java
----------------------------------------------------------------------
diff --git 
a/core/src/main/java/org/apache/calcite/avatica/AvaticaResultSet.java 
b/core/src/main/java/org/apache/calcite/avatica/AvaticaResultSet.java
index adfd6ba..21d7300 100644
--- a/core/src/main/java/org/apache/calcite/avatica/AvaticaResultSet.java
+++ b/core/src/main/java/org/apache/calcite/avatica/AvaticaResultSet.java
@@ -364,7 +364,7 @@ public class AvaticaResultSet extends ArrayFactoryImpl 
implements ResultSet {
   }
 
   public String getCursorName() throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public ResultSetMetaData getMetaData() throws SQLException {
@@ -410,23 +410,23 @@ public class AvaticaResultSet extends ArrayFactoryImpl 
implements ResultSet {
   }
 
   public boolean isLast() throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void beforeFirst() throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void afterLast() throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public boolean first() throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public boolean last() throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public int getRow() throws SQLException {
@@ -434,15 +434,15 @@ public class AvaticaResultSet extends ArrayFactoryImpl 
implements ResultSet {
   }
 
   public boolean absolute(int row) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public boolean relative(int rows) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public boolean previous() throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void setFetchDirection(int direction) throws SQLException {
@@ -482,196 +482,196 @@ public class AvaticaResultSet extends ArrayFactoryImpl 
implements ResultSet {
   }
 
   public void updateNull(int columnIndex) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateBoolean(int columnIndex, boolean x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateByte(int columnIndex, byte x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateShort(int columnIndex, short x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateInt(int columnIndex, int x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateLong(int columnIndex, long x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateFloat(int columnIndex, float x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateDouble(int columnIndex, double x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateBigDecimal(
       int columnIndex, BigDecimal x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateString(int columnIndex, String x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateBytes(int columnIndex, byte[] x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateDate(int columnIndex, Date x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateTime(int columnIndex, Time x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateTimestamp(
       int columnIndex, Timestamp x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateAsciiStream(
       int columnIndex, InputStream x, int length) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateBinaryStream(
       int columnIndex, InputStream x, int length) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateCharacterStream(
       int columnIndex, Reader x, int length) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateObject(
       int columnIndex, Object x, int scaleOrLength) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateObject(int columnIndex, Object x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateNull(String columnLabel) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateBoolean(
       String columnLabel, boolean x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateByte(String columnLabel, byte x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateShort(String columnLabel, short x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateInt(String columnLabel, int x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateLong(String columnLabel, long x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateFloat(String columnLabel, float x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateDouble(String columnLabel, double x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateBigDecimal(
       String columnLabel, BigDecimal x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateString(String columnLabel, String x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateBytes(String columnLabel, byte[] x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateDate(String columnLabel, Date x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateTime(String columnLabel, Time x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateTimestamp(
       String columnLabel, Timestamp x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateAsciiStream(
       String columnLabel, InputStream x, int length) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateBinaryStream(
       String columnLabel, InputStream x, int length) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateCharacterStream(
       String columnLabel, Reader reader, int length) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateObject(
       String columnLabel, Object x, int scaleOrLength) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateObject(String columnLabel, Object x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void insertRow() throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateRow() throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void deleteRow() throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void refreshRow() throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void cancelRowUpdates() throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void moveToInsertRow() throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void moveToCurrentRow() throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public AvaticaStatement getStatement() throws SQLException {
@@ -755,51 +755,51 @@ public class AvaticaResultSet extends ArrayFactoryImpl 
implements ResultSet {
   }
 
   public void updateRef(int columnIndex, Ref x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateRef(String columnLabel, Ref x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateBlob(int columnIndex, Blob x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateBlob(String columnLabel, Blob x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateClob(int columnIndex, Clob x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateClob(String columnLabel, Clob x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateArray(int columnIndex, Array x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateArray(String columnLabel, Array x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public RowId getRowId(int columnIndex) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public RowId getRowId(String columnLabel) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateRowId(int columnIndex, RowId x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateRowId(String columnLabel, RowId x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public int getHoldability() throws SQLException {
@@ -812,21 +812,21 @@ public class AvaticaResultSet extends ArrayFactoryImpl 
implements ResultSet {
 
   public void updateNString(
       int columnIndex, String nString) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateNString(
       String columnLabel, String nString) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateNClob(int columnIndex, NClob nClob) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateNClob(
       String columnLabel, NClob nClob) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public NClob getNClob(int columnIndex) throws SQLException {
@@ -847,12 +847,12 @@ public class AvaticaResultSet extends ArrayFactoryImpl 
implements ResultSet {
 
   public void updateSQLXML(
       int columnIndex, SQLXML xmlObject) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateSQLXML(
       String columnLabel, SQLXML xmlObject) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public String getNString(int columnIndex) throws SQLException {
@@ -873,145 +873,145 @@ public class AvaticaResultSet extends ArrayFactoryImpl 
implements ResultSet {
 
   public void updateNCharacterStream(
       int columnIndex, Reader x, long length) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateNCharacterStream(
       String columnLabel, Reader reader, long length) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateAsciiStream(
       int columnIndex, InputStream x, long length) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateBinaryStream(
       int columnIndex, InputStream x, long length) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateCharacterStream(
       int columnIndex, Reader x, long length) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateAsciiStream(
       String columnLabel, InputStream x, long length) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateBinaryStream(
       String columnLabel, InputStream x, long length) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateCharacterStream(
       String columnLabel, Reader reader, long length) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateBlob(
       int columnIndex,
       InputStream inputStream,
       long length) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateBlob(
       String columnLabel,
       InputStream inputStream,
       long length) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateClob(
       int columnIndex, Reader reader, long length) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateClob(
       String columnLabel, Reader reader, long length) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateNClob(
       int columnIndex, Reader reader, long length) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateNClob(
       String columnLabel, Reader reader, long length) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateNCharacterStream(
       int columnIndex, Reader x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateNCharacterStream(
       String columnLabel, Reader reader) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateAsciiStream(
       int columnIndex, InputStream x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateBinaryStream(
       int columnIndex, InputStream x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateCharacterStream(
       int columnIndex, Reader x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateAsciiStream(
       String columnLabel, InputStream x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateBinaryStream(
       String columnLabel, InputStream x) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateCharacterStream(
       String columnLabel, Reader reader) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateBlob(
       int columnIndex, InputStream inputStream) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateBlob(
       String columnLabel, InputStream inputStream) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateClob(int columnIndex, Reader reader) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateClob(
       String columnLabel, Reader reader) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateNClob(
       int columnIndex, Reader reader) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public void updateNClob(
       String columnLabel, Reader reader) throws SQLException {
-    throw statement.connection.helper.unsupported();
+    throw statement.connection.HELPER.unsupported();
   }
 
   public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
@@ -1027,7 +1027,7 @@ public class AvaticaResultSet extends ArrayFactoryImpl 
implements ResultSet {
     if (iface.isInstance(this)) {
       return iface.cast(this);
     }
-    throw statement.connection.helper.createException(
+    throw statement.connection.HELPER.createException(
         "does not implement '" + iface + "'");
   }
 

http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/50d45f98/core/src/main/java/org/apache/calcite/avatica/AvaticaResultSetMetaData.java
----------------------------------------------------------------------
diff --git 
a/core/src/main/java/org/apache/calcite/avatica/AvaticaResultSetMetaData.java 
b/core/src/main/java/org/apache/calcite/avatica/AvaticaResultSetMetaData.java
index b4e8892..7c4c274 100644
--- 
a/core/src/main/java/org/apache/calcite/avatica/AvaticaResultSetMetaData.java
+++ 
b/core/src/main/java/org/apache/calcite/avatica/AvaticaResultSetMetaData.java
@@ -133,7 +133,7 @@ public class AvaticaResultSetMetaData implements 
ResultSetMetaData {
     if (iface.isInstance(this)) {
       return iface.cast(this);
     }
-    throw statement.connection.helper.createException(
+    throw statement.connection.HELPER.createException(
         "does not implement '" + iface + "'");
   }
 

http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/50d45f98/core/src/main/java/org/apache/calcite/avatica/AvaticaStatement.java
----------------------------------------------------------------------
diff --git 
a/core/src/main/java/org/apache/calcite/avatica/AvaticaStatement.java 
b/core/src/main/java/org/apache/calcite/avatica/AvaticaStatement.java
index dcb2188..0676c5d 100644
--- a/core/src/main/java/org/apache/calcite/avatica/AvaticaStatement.java
+++ b/core/src/main/java/org/apache/calcite/avatica/AvaticaStatement.java
@@ -132,7 +132,7 @@ public abstract class AvaticaStatement
   private void checkNotPreparedOrCallable(String s) throws SQLException {
     if (this instanceof PreparedStatement
         || this instanceof CallableStatement) {
-      throw connection.helper.createException("Cannot call " + s
+      throw connection.HELPER.createException("Cannot call " + s
           + " on prepared or callable statement");
     }
   }
@@ -153,7 +153,7 @@ public abstract class AvaticaStatement
         }
       }
     } catch (RuntimeException e) {
-      throw connection.helper.createException("Error while executing SQL \"" + 
sql + "\": "
+      throw connection.HELPER.createException("Error while executing SQL \"" + 
sql + "\": "
           + e.getMessage(), e);
     }
 
@@ -217,12 +217,12 @@ public abstract class AvaticaStatement
     try {
       executeInternal(sql);
       if (openResultSet == null) {
-        throw connection.helper.createException(
+        throw connection.HELPER.createException(
             "Statement did not return a result set");
       }
       return openResultSet;
     } catch (RuntimeException e) {
-      throw connection.helper.createException("Error while executing SQL \"" + 
sql + "\": "
+      throw connection.HELPER.createException("Error while executing SQL \"" + 
sql + "\": "
           + e.getMessage(), e);
     }
   }
@@ -241,7 +241,7 @@ public abstract class AvaticaStatement
     try {
       close_();
     } catch (RuntimeException e) {
-      throw connection.helper.createException("While closing statement", e);
+      throw connection.HELPER.createException("While closing statement", e);
     }
   }
 
@@ -268,11 +268,11 @@ public abstract class AvaticaStatement
   }
 
   public int getMaxFieldSize() throws SQLException {
-    throw connection.helper.unsupported();
+    throw connection.HELPER.unsupported();
   }
 
   public void setMaxFieldSize(int max) throws SQLException {
-    throw connection.helper.unsupported();
+    throw connection.HELPER.unsupported();
   }
 
   public final int getMaxRows() {
@@ -289,14 +289,14 @@ public abstract class AvaticaStatement
 
   public void setLargeMaxRows(long maxRowCount) throws SQLException {
     if (maxRowCount < 0) {
-      throw connection.helper.createException(
+      throw connection.HELPER.createException(
           "illegal maxRows value: " + maxRowCount);
     }
     this.maxRowCount = maxRowCount;
   }
 
   public void setEscapeProcessing(boolean enable) throws SQLException {
-    throw connection.helper.unsupported();
+    throw connection.HELPER.unsupported();
   }
 
   public int getQueryTimeout() throws SQLException {
@@ -317,7 +317,7 @@ public abstract class AvaticaStatement
 
   public void setQueryTimeout(int seconds) throws SQLException {
     if (seconds < 0) {
-      throw connection.helper.createException(
+      throw connection.HELPER.createException(
           "illegal timeout value " + seconds);
     }
     setQueryTimeoutMillis(seconds * 1000);
@@ -344,7 +344,7 @@ public abstract class AvaticaStatement
   }
 
   public void setCursorName(String name) throws SQLException {
-    throw connection.helper.unsupported();
+    throw connection.HELPER.unsupported();
   }
 
   public ResultSet getResultSet() throws SQLException {
@@ -386,11 +386,11 @@ public abstract class AvaticaStatement
   }
 
   public int getResultSetConcurrency() throws SQLException {
-    throw connection.helper.unsupported();
+    throw connection.HELPER.unsupported();
   }
 
   public int getResultSetType() throws SQLException {
-    throw connection.helper.unsupported();
+    throw connection.HELPER.unsupported();
   }
 
   public void addBatch(String sql) throws SQLException {
@@ -421,18 +421,18 @@ public abstract class AvaticaStatement
 
   public boolean getMoreResults(int current) throws SQLException {
     if (closed) {
-      throw connection.helper.closed();
+      throw connection.HELPER.closed();
     }
     switch (current) {
     case KEEP_CURRENT_RESULT:
     case CLOSE_ALL_RESULTS:
-      throw connection.helper.unsupported();
+      throw connection.HELPER.unsupported();
 
     case CLOSE_CURRENT_RESULT:
       break;
 
     default:
-      throw connection.helper.createException("value " + current
+      throw connection.HELPER.createException("value " + current
           + " is not one of CLOSE_CURRENT_RESULT, KEEP_CURRENT_RESULT or 
CLOSE_ALL_RESULTS");
     }
 
@@ -443,41 +443,41 @@ public abstract class AvaticaStatement
   }
 
   public ResultSet getGeneratedKeys() throws SQLException {
-    throw connection.helper.unsupported();
+    throw connection.HELPER.unsupported();
   }
 
   public int executeUpdate(
       String sql, int autoGeneratedKeys) throws SQLException {
-    throw connection.helper.unsupported();
+    throw connection.HELPER.unsupported();
   }
 
   public int executeUpdate(
       String sql, int[] columnIndexes) throws SQLException {
-    throw connection.helper.unsupported();
+    throw connection.HELPER.unsupported();
   }
 
   public int executeUpdate(
       String sql, String[] columnNames) throws SQLException {
-    throw connection.helper.unsupported();
+    throw connection.HELPER.unsupported();
   }
 
   public boolean execute(
       String sql, int autoGeneratedKeys) throws SQLException {
-    throw connection.helper.unsupported();
+    throw connection.HELPER.unsupported();
   }
 
   public boolean execute(
       String sql, int[] columnIndexes) throws SQLException {
-    throw connection.helper.unsupported();
+    throw connection.HELPER.unsupported();
   }
 
   public boolean execute(
       String sql, String[] columnNames) throws SQLException {
-    throw connection.helper.unsupported();
+    throw connection.HELPER.unsupported();
   }
 
   public int getResultSetHoldability() throws SQLException {
-    throw connection.helper.unsupported();
+    throw connection.HELPER.unsupported();
   }
 
   public boolean isClosed() throws SQLException {
@@ -485,11 +485,11 @@ public abstract class AvaticaStatement
   }
 
   public void setPoolable(boolean poolable) throws SQLException {
-    throw connection.helper.unsupported();
+    throw connection.HELPER.unsupported();
   }
 
   public boolean isPoolable() throws SQLException {
-    throw connection.helper.unsupported();
+    throw connection.HELPER.unsupported();
   }
 
   // implements java.sql.Statement.closeOnCompletion (added in JDK 1.7)
@@ -508,7 +508,7 @@ public abstract class AvaticaStatement
     if (iface.isInstance(this)) {
       return iface.cast(this);
     }
-    throw connection.helper.createException(
+    throw connection.HELPER.createException(
         "does not implement '" + iface + "'");
   }
 

http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/50d45f98/core/src/test/java/org/apache/calcite/avatica/AvaticaConnectionTest.java
----------------------------------------------------------------------
diff --git 
a/core/src/test/java/org/apache/calcite/avatica/AvaticaConnectionTest.java 
b/core/src/test/java/org/apache/calcite/avatica/AvaticaConnectionTest.java
index b1c003f..9482eba 100644
--- a/core/src/test/java/org/apache/calcite/avatica/AvaticaConnectionTest.java
+++ b/core/src/test/java/org/apache/calcite/avatica/AvaticaConnectionTest.java
@@ -20,23 +20,39 @@ import org.junit.Assert;
 import org.junit.Test;
 import org.mockito.Mockito;
 
+import java.sql.SQLException;
 import java.util.Properties;
 
 /**
  * Tests for AvaticaConnection
  */
 public class AvaticaConnectionTest {
+  @Test
+  public void testIsValid() throws SQLException {
+    AvaticaConnection connection = Mockito.mock(AvaticaConnection.class,
+        Mockito.CALLS_REAL_METHODS);
+    try {
+      connection.isValid(-1);
+      Assert.fail("Connection isValid should throw SQLException on negative 
timeout");
+    } catch (SQLException expected) {
+      Assert.assertEquals("timeout is less than 0", expected.getMessage());
+    }
+
+    Mockito.when(connection.isClosed()).thenReturn(false);
+    Assert.assertTrue(connection.isValid(0));
+
+    Mockito.when(connection.isClosed()).thenReturn(true);
+    Assert.assertFalse(connection.isValid(0));
+  }
 
   @Test
   public void testNumExecuteRetries() {
-    AvaticaConnection statement = Mockito.mock(AvaticaConnection.class);
-
-    
Mockito.when(statement.getNumStatementRetries(Mockito.nullable(Properties.class)))
-      .thenCallRealMethod();
+    AvaticaConnection connection = Mockito.mock(AvaticaConnection.class,
+        Mockito.CALLS_REAL_METHODS);
 
     // Bad argument should throw an exception
     try {
-      statement.getNumStatementRetries(null);
+      connection.getNumStatementRetries(null);
       Assert.fail("Calling getNumStatementRetries with a null object should 
throw an exception");
     } catch (NullPointerException e) {
       // Pass
@@ -45,14 +61,14 @@ public class AvaticaConnectionTest {
     Properties props = new Properties();
 
     // Verify the default value
-    
Assert.assertEquals(Long.valueOf(AvaticaConnection.NUM_EXECUTE_RETRIES_DEFAULT).longValue(),
-        statement.getNumStatementRetries(props));
+    
Assert.assertEquals(Long.parseLong(AvaticaConnection.NUM_EXECUTE_RETRIES_DEFAULT),
+        connection.getNumStatementRetries(props));
 
     // Set a non-default value
     props.setProperty(AvaticaConnection.NUM_EXECUTE_RETRIES_KEY, "10");
 
     // Verify that we observe that value
-    Assert.assertEquals(10, statement.getNumStatementRetries(props));
+    Assert.assertEquals(10, connection.getNumStatementRetries(props));
   }
 
 }

http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/50d45f98/server/src/test/java/org/apache/calcite/avatica/RemoteDriverTest.java
----------------------------------------------------------------------
diff --git 
a/server/src/test/java/org/apache/calcite/avatica/RemoteDriverTest.java 
b/server/src/test/java/org/apache/calcite/avatica/RemoteDriverTest.java
index fa5371f..6af67ff 100644
--- a/server/src/test/java/org/apache/calcite/avatica/RemoteDriverTest.java
+++ b/server/src/test/java/org/apache/calcite/avatica/RemoteDriverTest.java
@@ -242,6 +242,34 @@ public class RemoteDriverTest {
     assertThat(connection.isClosed(), is(true));
   }
 
+  @Test public void testIsValid() throws Exception {
+    ConnectionSpec.getDatabaseLock().lock();
+    try {
+      final Connection connection = getLocalConnection();
+      try {
+        connection.isValid(-1);
+        fail("Connection isValid should throw SQLException on negative 
timeout");
+      } catch (SQLException expected) {
+        assertEquals("timeout is less than 0", expected.getMessage());
+      }
+
+      // Check that connection isValid before and during use
+      assertThat(connection.isValid(1), is(true));
+      Statement statement = connection.createStatement();
+      assertTrue(statement.execute("VALUES 1"));
+      assertNotNull(statement.getResultSet());
+      assertThat(connection.isValid(1), is(true));
+      statement.close();
+      assertThat(connection.isValid(1), is(true));
+
+      // Connection should be invalid after being closed
+      connection.close();
+      assertThat(connection.isValid(1), is(false));
+    } finally {
+      ConnectionSpec.getDatabaseLock().unlock();
+    }
+  }
+
   @Test public void testDatabaseProperties() throws Exception {
     ConnectionSpec.getDatabaseLock().lock();
     try {

Reply via email to