Skip to content

Commit

Permalink
Fix per wendigo's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
xsgao-github committed Dec 31, 2024
1 parent 880e9f3 commit d8f28d3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -199,18 +199,6 @@ public void testInvalidUrls()
"Provided connection properties are invalid:\n" +
"Connection property assumeLiteralNamesInMetadataCallsForNonConformingClients cannot be set if assumeLiteralUnderscoreInMetadataCallsForNonConformingClients is enabled\n" +
"Connection property assumeLiteralUnderscoreInMetadataCallsForNonConformingClients cannot be set if assumeLiteralNamesInMetadataCallsForNonConformingClients is enabled");

// invalid validateConnection
assertInvalid("trino://localhost:8080?validateConnection=0", "Connection property validateConnection value is invalid: 0");
assertInvalid("trino://localhost:8080?validateConnection=1", "Connection property validateConnection value is invalid: 1");
assertInvalid("trino://localhost:8080?validateConnection=-1", "Connection property validateConnection value is invalid: -1");
assertInvalid("trino://localhost:8080?validateConnection=yes", "Connection property validateConnection value is invalid: yes");
assertInvalid("trino://localhost:8080?validateConnection=no", "Connection property validateConnection value is invalid: no");
assertInvalid("trino://localhost:8080?validateConnection=on", "Connection property validateConnection value is invalid: on");
assertInvalid("trino://localhost:8080?validateConnection=off", "Connection property validateConnection value is invalid: off");
assertInvalid("trino://localhost:8080?validateConnection=T", "Connection property validateConnection value is invalid: T");
assertInvalid("trino://localhost:8080?validateConnection=F", "Connection property validateConnection value is invalid: F");
assertInvalid("trino://localhost:8080?validateConnection=abc", "Connection property validateConnection value is invalid: abc");
}

@Test
Expand Down
21 changes: 10 additions & 11 deletions client/trino-jdbc/src/main/java/io/trino/jdbc/TrinoConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public class TrinoConnection
uri.getExplicitPrepare().ifPresent(value -> this.useExplicitPrepare = value);
uri.getAssumeNullCatalogMeansCurrentCatalog().ifPresent(value -> this.assumeNullCatalogMeansCurrentCatalog = value);

validateConnection = uri.isValidateConnection();
this.validateConnection = uri.isValidateConnection();
if (validateConnection) {
try {
if (!validateCredentials(CONNECTION_TIMEOUT_SECONDS)) {
Expand All @@ -186,7 +186,7 @@ public class TrinoConnection
}

private boolean validateCredentials(int timeout)
throws IOException
throws IOException, UnsupportedOperationException
{
HttpUrl url = HttpUrl.get(httpUri);
url = url.newBuilder().encodedPath("/v1/statement").build();
Expand Down Expand Up @@ -216,18 +216,18 @@ private boolean validateCredentials(int timeout)
case HTTP_BAD_METHOD:
throw new UnsupportedOperationException("Target server does not support HEAD /v1/statement");
default:
throw new IOException(format("Unexpected HTTP status code %d returned from HEAD " +
throw new IOException(format("Unexpected HTTP status code %d returned for HEAD " +
"/v1/statement", response.code()));
}
}
catch (IOException ioe) {
if (getCausalChain(ioe).stream()
.anyMatch(e -> (e instanceof InterruptedIOException
|| e instanceof ProtocolException))) {
lastException = ioe;
catch (IOException e) {
if (getCausalChain(e).stream()
.anyMatch(th -> th instanceof InterruptedIOException && th.getMessage().equals("timeout")
|| th instanceof ProtocolException)) {
lastException = e;
}
else {
throw ioe;
throw e;
}
}

Expand Down Expand Up @@ -624,9 +624,8 @@ public boolean isValid(int timeout)
return validateCredentials(timeout);
}
catch (UnsupportedOperationException e) {
// Backward compatible with older Trino server
logger.log(Level.FINE, "Remote server does not support validating connection.", e);
return true;
return false;
}
catch (IOException e) {
logger.log(Level.FINE, "Validating connection failed.", e);
Expand Down

0 comments on commit d8f28d3

Please sign in to comment.