Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class LibSqlConnection extends AbstractJdbcConnection {

Expand All @@ -40,6 +44,7 @@ public class LibSqlConnection extends AbstractJdbcConnection {
@NotNull
private final Map<String, Object> driverProperties;
private LibSqlDatabaseMetaData databaseMetaData;
private volatile boolean closed = false;

public LibSqlConnection(
@NotNull LibSqlDriver driver,
Expand Down Expand Up @@ -108,12 +113,47 @@ private LibSqlPreparedStatement prepareStatementImpl(String sql) throws SQLExcep

@Override
public void close() throws SQLException {
client.close();
if (!closed) {
client.close();
closed = true;
}
}

@Override
public boolean isClosed() {
return false;
return closed;
}

@Override
public boolean isValid(int timeout) throws SQLException {
if (timeout < 0) {
throw new SQLException("Invalid timeout value: " + timeout);
}
if (closed) {
return false;
}
CompletableFuture<Boolean> ping = CompletableFuture.supplyAsync(() -> {
try {
LibSqlUtils.executeQuery(this, "SELECT 1");
return Boolean.TRUE;
} catch (Exception e) {
return Boolean.FALSE;
}
});
try {
if (timeout == 0) {
return ping.get();
}
return ping.get(timeout, TimeUnit.SECONDS);
} catch (TimeoutException e) {
ping.cancel(true);
return false;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return false;
} catch (ExecutionException e) {
return false;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ public static void main(String[] args) throws Exception {
System.out.println("Driver: " + metaData.getDriverName());
System.out.println("Database: " + metaData.getDatabaseProductName() + " " + metaData.getDatabaseProductVersion());

System.out.println("isValid(5) = " + connection.isValid(5));
try {
connection.isValid(-1);
System.out.println("BUG: negative timeout did not throw");
} catch (SQLException e) {
System.out.println("isValid(-1) correctly threw: " + e.getMessage());
}

System.out.println("Query:");
try (Statement dbStat = connection.createStatement()) {
try (ResultSet dbResult = dbStat.executeQuery("select * from testme")) {
Expand All @@ -61,6 +69,8 @@ public static void main(String[] args) throws Exception {
}
}

connection.close();
System.out.println("isValid(5) after close = " + connection.isValid(5));
}
} finally {
System.out.println("Finished (" + (System.currentTimeMillis() - startTime) + "ms)");
Expand Down