Skip to content

Commit 79609b8

Browse files
committed
[CONJ-1276] Connection#isValid(int timeout) does not obey the passed in timeout if the network is down.
1 parent fac4146 commit 79609b8

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/main/java/org/mariadb/jdbc/Connection.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,9 @@ public boolean isValid(int timeout) throws SQLException {
701701
if (timeout < 0) {
702702
throw exceptionFactory.create("the value supplied for timeout is negative");
703703
}
704+
int initialSocketTimeout = client.getSocketTimeout();
704705
try (ClosableLock ignore = lock.closeableLock()) {
706+
if (initialSocketTimeout != timeout * 1000) client.setSocketTimeout(timeout * 1000);
705707
client.execute(PingPacket.INSTANCE, true);
706708
return true;
707709
} catch (SQLException sqle) {
@@ -711,6 +713,14 @@ public boolean isValid(int timeout) throws SQLException {
711713
poolConnection.close();
712714
}
713715
return false;
716+
} finally {
717+
if (initialSocketTimeout != timeout * 1000) {
718+
try {
719+
client.setSocketTimeout(initialSocketTimeout);
720+
} catch (SQLException sqle) {
721+
// eat
722+
}
723+
}
714724
}
715725
}
716726

src/test/java/org/mariadb/jdbc/integration/ConnectionTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@
88
import java.sql.*;
99
import java.sql.Connection;
1010
import java.sql.Statement;
11+
import java.time.Duration;
12+
import java.time.Instant;
1113
import java.util.*;
1214
import java.util.concurrent.Executor;
1315
import java.util.concurrent.ExecutorService;
1416
import java.util.concurrent.Executors;
1517
import org.junit.jupiter.api.*;
1618
import org.mariadb.jdbc.*;
19+
import org.mariadb.jdbc.export.HaMode;
1720
import org.mariadb.jdbc.integration.util.SocketFactoryBasicTest;
1821
import org.mariadb.jdbc.integration.util.SocketFactoryTest;
1922
import org.mariadb.jdbc.util.constants.Capabilities;
@@ -1510,4 +1513,23 @@ public void isClosed() throws SQLException {
15101513
assertTrue(stmt.isClosed());
15111514
assertTrue(preparedStatement.isClosed());
15121515
}
1516+
1517+
@Test
1518+
public void isValidTimeoutValidation() throws SQLException {
1519+
try (org.mariadb.jdbc.Connection con = createProxyCon(HaMode.NONE, "")) {
1520+
proxy.setDelay(2000);
1521+
Instant start = Instant.now();
1522+
try {
1523+
con.isValid(1);
1524+
} catch (SQLException e) {
1525+
// eat
1526+
}
1527+
Instant end = Instant.now();
1528+
Duration duration = Duration.between(start, end);
1529+
assertTrue(duration.toMillis() > 950);
1530+
assertTrue(duration.toMillis() < 1500);
1531+
} finally {
1532+
proxy.forceClose();
1533+
}
1534+
}
15131535
}

0 commit comments

Comments
 (0)