Skip to content

Commit b2eaefe

Browse files
arobert-delfingendavecramer
authored andcommitted
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()
1 parent 8495127 commit b2eaefe

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

pgjdbc/src/main/java/org/postgresql/jdbc/PgConnection.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,18 +1379,24 @@ public boolean isValid(int timeout) throws SQLException {
13791379
return false;
13801380
}
13811381
try {
1382-
if (replicationConnection) {
1383-
Statement statement = createStatement();
1384-
statement.execute("IDENTIFY_SYSTEM");
1385-
statement.close();
1386-
} else {
1387-
if (checkConnectionQuery == null) {
1388-
checkConnectionQuery = prepareStatement("");
1382+
int savedNetworkTimeOut = getNetworkTimeout();
1383+
try {
1384+
setNetworkTimeout(null, timeout * 1000);
1385+
if (replicationConnection) {
1386+
Statement statement = createStatement();
1387+
statement.execute("IDENTIFY_SYSTEM");
1388+
statement.close();
1389+
} else {
1390+
if (checkConnectionQuery == null) {
1391+
checkConnectionQuery = prepareStatement("");
1392+
}
1393+
checkConnectionQuery.setQueryTimeout(timeout);
1394+
checkConnectionQuery.executeUpdate();
13891395
}
1390-
checkConnectionQuery.setQueryTimeout(timeout);
1391-
checkConnectionQuery.executeUpdate();
1396+
return true;
1397+
} finally {
1398+
setNetworkTimeout(null, savedNetworkTimeOut);
13921399
}
1393-
return true;
13941400
} catch (SQLException e) {
13951401
if (PSQLState.IN_FAILED_SQL_TRANSACTION.getState().equals(e.getSQLState())) {
13961402
// "current transaction aborted", assume the connection is up and running

0 commit comments

Comments
 (0)