Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions jdbc-v2/src/main/java/com/clickhouse/jdbc/ConnectionImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.clickhouse.jdbc.internal.ParsedPreparedStatement;
import com.clickhouse.jdbc.internal.SqlParser;
import com.clickhouse.jdbc.metadata.DatabaseMetaDataImpl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -35,6 +36,7 @@
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.time.Duration;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
Expand Down Expand Up @@ -487,13 +489,15 @@ public SQLXML createSQLXML() throws SQLException {

@Override
public boolean isValid(int timeout) throws SQLException {
checkOpen();
if (timeout < 0) {
throw new SQLException("Timeout must be >= 0", ExceptionUtils.SQL_STATE_CLIENT_ERROR);
}

//TODO: This is a placeholder implementation
return true;
if (isClosed()) {
return false;
}
return timeout == 0
? client.ping()
: client.ping(Duration.ofSeconds(timeout).toMillis());
}

@Override
Expand Down
28 changes: 26 additions & 2 deletions jdbc-v2/src/test/java/com/clickhouse/jdbc/ConnectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.common.ConsoleNotifier;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;

import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
Expand All @@ -34,7 +35,6 @@
import static org.testng.Assert.assertThrows;
import static org.testng.Assert.fail;


public class ConnectionTest extends JdbcIntegrationTest {

@Test(groups = { "integration" }, enabled = false)
Expand Down Expand Up @@ -569,8 +569,32 @@ public void testConnectionWithValidDatabaseName(String dbName) throws Exception
connCheck.close();
}

@Test(groups = { "integration" })
public void closedConnectionIsInvalid() throws Exception {
if (isCloud()) {
return;
}
Connection connection = this.getJdbcConnection();
Assert.assertTrue(connection.isValid(3));
connection.close();
Assert.assertFalse(connection.isValid(3));
}

@Test(groups = { "integration" })
public void connectionWithWrongCredentialsIsInvalid() throws Exception {
if (isCloud()) {
return;
}
Connection connection = this.getJdbcConnection();
Assert.assertTrue(connection.isValid(3));
Properties properties = new Properties();
properties.put("password", "invalid");
connection = this.getJdbcConnection(properties);
Assert.assertFalse(connection.isValid(3));
}

@DataProvider(name = "validDatabaseNames")
public Object[][] createValidDatabaseNames() {
private static Object[][] createValidDatabaseNames() {
return new Object[][] {
{ "foo" },
{ "with-dashes" },
Expand Down
Loading