Allow configuring network timeout for JDBC driver
@@ -18,6 +18,7 @@ import com.facebook.presto.client.ServerInfo;
import com.facebook.presto.client.StatementClient;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
+import com.google.common.primitives.Ints;
import io.airlift.units.Duration;
import java.net.URI;
@@ -56,6 +57,8 @@ import static com.google.common.collect.Maps.fromProperties;
import static java.lang.String.format;
import static java.nio.charset.StandardCharsets.US_ASCII;
import static java.util.Objects.requireNonNull;
+import static java.util.concurrent.TimeUnit.DAYS;
+import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.MINUTES;
public class PrestoConnection
@@ -69,6 +72,7 @@ public class PrestoConnection
private final AtomicReference<String> schema = new AtomicReference<>();
private final AtomicReference<String> timeZoneId = new AtomicReference<>();
private final AtomicReference<Locale> locale = new AtomicReference<>();
+ private final AtomicReference<Integer> networkTimeoutMillis = new AtomicReference<>(Ints.saturatedCast(MINUTES.toMillis(2)));
private final AtomicReference<ServerInfo> serverInfo = new AtomicReference<>();
private final URI jdbcUri;
@@ -553,14 +557,19 @@ public class PrestoConnection
public void setNetworkTimeout(Executor executor, int milliseconds)
throws SQLException
{
- throw new SQLFeatureNotSupportedException("setNetworkTimeout");
+ checkOpen();
+ if (milliseconds < 0) {
+ throw new SQLException("Timeout is negative");
+ }
+ networkTimeoutMillis.set(milliseconds);
}
@Override
public int getNetworkTimeout()
throws SQLException
{
- throw new SQLFeatureNotSupportedException("getNetworkTimeout");
+ checkOpen();
+ return networkTimeoutMillis.get();
}
@SuppressWarnings("unchecked")
@@ -626,6 +635,10 @@ public class PrestoConnection
Map<String, String> allProperties = new HashMap<>(sessionProperties);
allProperties.putAll(sessionPropertiesOverride);
+ // zero means no timeout, so use a huge value that is effectively unlimited
+ int millis = networkTimeoutMillis.get();
+ Duration timeout = (millis > 0) ? new Duration(millis, MILLISECONDS) : new Duration(999, DAYS);
+
ClientSession session = new ClientSession(
httpUri,
user,
@@ -640,7 +653,7 @@ public class PrestoConnection
ImmutableMap.copyOf(preparedStatements),
transactionId.get(),
false,
- new Duration(2, MINUTES));
+ timeout);
return queryExecutor.startQuery(session, sql);
}
