Skip to content
Permalink
Browse files
fix: actually use milliseconds instead of microseconds for timeouts (#…
…1653)

* fix: actually use milliseconds instead of microseconds for timeouts

* Use TimeUnit to convert to millis
  • Loading branch information
davecramer committed Jan 24, 2020
1 parent 39c73a6 commit 3dd5dff2b095ed7665fbed386a7cbad7c3254fd1
Showing with 6 additions and 4 deletions.
  1. +3 −2 pgjdbc/src/main/java/org/postgresql/Driver.java
  2. +3 −2 pgjdbc/src/main/java/org/postgresql/core/v3/QueryExecutorImpl.java
@@ -31,6 +31,7 @@
import java.util.Enumeration;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -397,7 +398,7 @@ public void run() {
* @throws SQLException if a connection error occurs or the timeout is reached
*/
public Connection getResult(long timeout) throws SQLException {
long expiry = System.nanoTime() / 1000 + timeout;
long expiry = TimeUnit.NANOSECONDS.toMillis(System.nanoTime()) + timeout;
synchronized (this) {
while (true) {
if (result != null) {
@@ -416,7 +417,7 @@ public Connection getResult(long timeout) throws SQLException {
}
}

long delay = expiry - System.nanoTime() / 1000;
long delay = expiry - TimeUnit.NANOSECONDS.toMillis(System.nanoTime());
if (delay <= 0) {
abandoned = true;
throw new PSQLException(GT.tr("Connection attempt timed out."),
@@ -61,6 +61,7 @@
import java.util.Properties;
import java.util.Set;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -720,7 +721,7 @@ public synchronized void processNotifies(int timeoutMillis) throws SQLException
long startTime = 0;
int oldTimeout = 0;
if (useTimeout) {
startTime = System.nanoTime() / 1000;
startTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime());
try {
oldTimeout = pgStream.getSocket().getSoTimeout();
} catch (SocketException e) {
@@ -750,7 +751,7 @@ public synchronized void processNotifies(int timeoutMillis) throws SQLException
SQLWarning warning = receiveNoticeResponse();
addWarning(warning);
if (useTimeout) {
long newTimeMillis = System.nanoTime() / 1000;
long newTimeMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime());
timeoutMillis += startTime - newTimeMillis; // Overflows after 49 days, ignore that
startTime = newTimeMillis;
if (timeoutMillis == 0) {

0 comments on commit 3dd5dff

Please sign in to comment.