提交 df3d2681 编写于 作者: Z zyyang

change

上级 6f5ed794
...@@ -4,7 +4,7 @@ import java.sql.*; ...@@ -4,7 +4,7 @@ import java.sql.*;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.concurrent.Executor; import java.util.concurrent.*;
public abstract class AbstractConnection extends WrapperImpl implements Connection { public abstract class AbstractConnection extends WrapperImpl implements Connection {
...@@ -291,19 +291,37 @@ public abstract class AbstractConnection extends WrapperImpl implements Connecti ...@@ -291,19 +291,37 @@ public abstract class AbstractConnection extends WrapperImpl implements Connecti
@Override @Override
public boolean isValid(int timeout) throws SQLException { public boolean isValid(int timeout) throws SQLException {
//true if the connection is valid, false otherwise
if (isClosed()) if (isClosed())
return false; return false;
if (timeout < 0) if (timeout < 0) //SQLException - if the value supplied for timeout is less then 0
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_VARIABLE); throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_VARIABLE);
int status; ExecutorService executor = Executors.newCachedThreadPool();
try (Statement stmt = createStatement()) { Future<Boolean> future = executor.submit(() -> {
ResultSet resultSet = stmt.executeQuery("select server_status()"); int status;
resultSet.next(); try (Statement stmt = createStatement()) {
status = resultSet.getInt("server_status()"); ResultSet resultSet = stmt.executeQuery("select server_status()");
resultSet.close(); resultSet.next();
status = resultSet.getInt("server_status()");
resultSet.close();
}
return status == 1 ? true : false;
});
boolean status = false;
try {
status = future.get(timeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (TimeoutException e) {
future.cancel(true);
} finally {
executor.shutdownNow();
} }
return status == 1 ? true : false; return status;
} }
@Override @Override
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册