From 3469b3cb1063da4f79e7bb2d7d80ac1299de29a0 Mon Sep 17 00:00:00 2001 From: luoyunpeng <956941328@qq.com> Date: Sun, 20 Aug 2017 19:42:55 +0800 Subject: [PATCH] implement isValid function --- .../org/trafodion/jdbc/t4/InputOutput.java | 4 ++ .../trafodion/jdbc/t4/TrafT4Connection.java | 56 +++++++++++++++++-- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/core/conn/jdbcT4/src/main/java/org/trafodion/jdbc/t4/InputOutput.java b/core/conn/jdbcT4/src/main/java/org/trafodion/jdbc/t4/InputOutput.java index 56111e9e6e..46ef3f7238 100644 --- a/core/conn/jdbcT4/src/main/java/org/trafodion/jdbc/t4/InputOutput.java +++ b/core/conn/jdbcT4/src/main/java/org/trafodion/jdbc/t4/InputOutput.java @@ -679,4 +679,8 @@ void startConnectionIdleTimeout() { // need be - see class comments. } } + + public boolean isSocketClosed() { + return m_socket.isClosed(); + } } // end class InputOutput diff --git a/core/conn/jdbcT4/src/main/java/org/trafodion/jdbc/t4/TrafT4Connection.java b/core/conn/jdbcT4/src/main/java/org/trafodion/jdbc/t4/TrafT4Connection.java index 2a8e381db0..027a499673 100644 --- a/core/conn/jdbcT4/src/main/java/org/trafodion/jdbc/t4/TrafT4Connection.java +++ b/core/conn/jdbcT4/src/main/java/org/trafodion/jdbc/t4/TrafT4Connection.java @@ -1876,10 +1876,58 @@ public SQLXML createSQLXML() throws SQLException { return null; } - public boolean isValid(int timeout) throws SQLException { - // TODO Auto-generated method stub - return false; - } + public boolean isValid(int timeout) throws SQLException { + if (props_.t4Logger_.isLoggable(Level.FINE) == true) { + Object p[] = T4LoggingUtilities.makeParams(props_); + props_.t4Logger_.logp(Level.FINE, "TrafT4Connection", "isValid", "iValid", p); + } + if (props_.getLogWriter() != null) { + LogRecord lr = new LogRecord(Level.FINE, ""); + Object p[] = T4LoggingUtilities.makeParams(props_); + lr.setParameters(p); + lr.setSourceClassName("TrafT4Connection"); + lr.setSourceMethodName("isValid"); + T4LogFormatter lf = new T4LogFormatter(); + String temp = lf.format(lr); + props_.getLogWriter().println(temp); + } + + Statement verifyStmt = null; + ResultSet rs = null; + + try { + if (this._isClosed() || this.ic_.getNCSAddress().m_io.isSocketClosed()) { + return false; + } + + verifyStmt = createStatement(); + rs = verifyStmt.executeQuery("values(1)"); + if (rs.next() && rs.getInt(1) == 1) { + return true; + } + } catch (SQLException e) { + return false; + } finally { + try { + if (rs != null) { + rs.close(); + rs = null; + } + } catch (SQLException e) { + // do nothing + } + try { + if (verifyStmt != null) { + verifyStmt.close(); + verifyStmt = null; + } + } catch (SQLException e) { + // do nothing + } + } + + return false; + } public void setClientInfo(String name, String value) throws SQLClientInfoException {