This commit is contained in:
zyyang 2021-02-20 10:55:10 +08:00
parent 904116ef64
commit 6f5ed79457
4 changed files with 27 additions and 10 deletions

View File

@ -290,7 +290,21 @@ public abstract class AbstractConnection extends WrapperImpl implements Connecti
} }
@Override @Override
public abstract boolean isValid(int timeout) throws SQLException; public boolean isValid(int timeout) throws SQLException {
if (isClosed())
return false;
if (timeout < 0)
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_VARIABLE);
int status;
try (Statement stmt = createStatement()) {
ResultSet resultSet = stmt.executeQuery("select server_status()");
resultSet.next();
status = resultSet.getInt("server_status()");
resultSet.close();
}
return status == 1 ? true : false;
}
@Override @Override
public void setClientInfo(String name, String value) throws SQLClientInfoException { public void setClientInfo(String name, String value) throws SQLClientInfoException {

View File

@ -142,8 +142,9 @@ public class TSDBConnection extends AbstractConnection {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
} }
public boolean isValid(int timeout) throws SQLException { // @Override
return !this.isClosed(); // public boolean isValid(int timeout) throws SQLException {
} // return !this.isClosed();
// }
} }

View File

@ -357,9 +357,8 @@ public class RestfulConnection implements Connection {
public boolean isValid(int timeout) throws SQLException { public boolean isValid(int timeout) throws SQLException {
if (timeout < 0) if (timeout < 0)
throw new SQLException(TSDBConstants.INVALID_VARIABLES); throw new SQLException(TSDBConstants.INVALID_VARIABLES);
// TODO: // TODO: The driver shall submit a query on the connection or use some other mechanism
/* The driver shall submit a query on the connection or use some other mechanism that positively verifies // that positively verifies the connection is still valid when this method is called.
the connection is still valid when this method is called.*/
return !isClosed(); return !isClosed();
} }

View File

@ -187,12 +187,15 @@ public class TSDBConnectionTest {
public void createNClob() { public void createNClob() {
} }
@Test @Test(expected = SQLFeatureNotSupportedException.class)
public void createSQLXML() { public void createSQLXML() throws SQLException {
conn.createSQLXML();
} }
@Test @Test
public void isValid() { public void isValid() throws SQLException {
Assert.assertTrue(conn.isValid(1000));
conn.isValid(0);
} }
@Test @Test