Skip to content

Support Connection.isValid method #156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
34 changes: 33 additions & 1 deletion src/main/java/org/tarantool/jdbc/SQLConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
public class SQLConnection implements Connection {

private static final int UNSET_HOLDABILITY = 0;
private static final String PING_QUERY = "SELECT 1";

private final TarantoolConnection connection;

Expand Down Expand Up @@ -382,9 +383,40 @@ public SQLXML createSQLXML() throws SQLException {
throw new SQLFeatureNotSupportedException();
}

/**
* {@inheritDoc}
*
* @param timeout temporally ignored param
*
* @return connection activity status
*/
@Override
public boolean isValid(int timeout) throws SQLException {
return true;
if (timeout < 0) {
throw new SQLNonTransientException(
"Timeout cannot be negative",
SQLStates.INVALID_PARAMETER_VALUE.getSqlState()
);
}
if (isClosed()) {
return false;
}
return checkConnection(timeout);
}

private boolean checkConnection(int timeout) {
ResultSet resultSet = null;
try (Statement pingStatement = createStatement()) {
// todo: before use timeout we need to provide query timeout per statement

resultSet = pingStatement.executeQuery(PING_QUERY);
boolean isValid = resultSet.next() && resultSet.getInt(1) == 1;
resultSet.close();

return isValid;
} catch (SQLException e) {
return false;
}
}

@Override
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/org/tarantool/jdbc/JdbcConnectionIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@ public void execute() throws Throwable {
assertEquals(5, i);
}

@Test
void testIsValidCheck() throws SQLException {
assertTrue(conn.isValid(2000));
assertThrows(SQLException.class, () -> conn.isValid(-1000));

conn.close();
assertFalse(conn.isValid(2000));
}

@Test
public void testConnectionUnwrap() throws SQLException {
assertEquals(conn, conn.unwrap(SQLConnection.class));
Expand Down