Skip to content
Permalink
Browse files

Ensure isValid() will not last more than timeout seconds (#1557)

* Ensure isValid() will not last more than timeout seconds
even if the underlying socket is broken (issue #1556)

* Fix check style

* Add a try/catch to succeed tests

* Manages simply and properly get/setNetworkTimeout() in isValid()
  • Loading branch information...
arobert-delfingen authored and davecramer committed Sep 5, 2019
1 parent 8495127 commit b2eaefef1787b8ab0b8fcb266d10be4945a4fe25
Showing with 16 additions and 10 deletions.
  1. +16 −10 pgjdbc/src/main/java/org/postgresql/jdbc/PgConnection.java
@@ -1379,18 +1379,24 @@ public boolean isValid(int timeout) throws SQLException {
return false;
}
try {
if (replicationConnection) {
Statement statement = createStatement();
statement.execute("IDENTIFY_SYSTEM");
statement.close();
} else {
if (checkConnectionQuery == null) {
checkConnectionQuery = prepareStatement("");
int savedNetworkTimeOut = getNetworkTimeout();
try {
setNetworkTimeout(null, timeout * 1000);
if (replicationConnection) {
Statement statement = createStatement();
statement.execute("IDENTIFY_SYSTEM");
statement.close();
} else {
if (checkConnectionQuery == null) {
checkConnectionQuery = prepareStatement("");
}
checkConnectionQuery.setQueryTimeout(timeout);
checkConnectionQuery.executeUpdate();
}
checkConnectionQuery.setQueryTimeout(timeout);
checkConnectionQuery.executeUpdate();
return true;
} finally {
setNetworkTimeout(null, savedNetworkTimeOut);
}
return true;
} catch (SQLException e) {
if (PSQLState.IN_FAILED_SQL_TRANSACTION.getState().equals(e.getSQLState())) {
// "current transaction aborted", assume the connection is up and running

0 comments on commit b2eaefe

Please sign in to comment.
You can’t perform that action at this time.