Skip to content

Commit c106280

Browse files
authored
Fix #20: implement Connection.isValid() and track close state (#51)
1 parent c2310d0 commit c106280

2 files changed

Lines changed: 52 additions & 2 deletions

File tree

com.dbeaver.jdbc.driver.libsql/src/main/java/com/dbeaver/jdbc/driver/libsql/LibSqlConnection.java

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
import java.sql.SQLException;
2929
import java.sql.Statement;
3030
import java.util.Map;
31+
import java.util.concurrent.CompletableFuture;
32+
import java.util.concurrent.ExecutionException;
33+
import java.util.concurrent.TimeUnit;
34+
import java.util.concurrent.TimeoutException;
3135

3236
public class LibSqlConnection extends AbstractJdbcConnection {
3337

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

4449
public LibSqlConnection(
4550
@NotNull LibSqlDriver driver,
@@ -108,12 +113,47 @@ private LibSqlPreparedStatement prepareStatementImpl(String sql) throws SQLExcep
108113

109114
@Override
110115
public void close() throws SQLException {
111-
client.close();
116+
if (!closed) {
117+
client.close();
118+
closed = true;
119+
}
112120
}
113121

114122
@Override
115123
public boolean isClosed() {
116-
return false;
124+
return closed;
125+
}
126+
127+
@Override
128+
public boolean isValid(int timeout) throws SQLException {
129+
if (timeout < 0) {
130+
throw new SQLException("Invalid timeout value: " + timeout);
131+
}
132+
if (closed) {
133+
return false;
134+
}
135+
CompletableFuture<Boolean> ping = CompletableFuture.supplyAsync(() -> {
136+
try {
137+
LibSqlUtils.executeQuery(this, "SELECT 1");
138+
return Boolean.TRUE;
139+
} catch (Exception e) {
140+
return Boolean.FALSE;
141+
}
142+
});
143+
try {
144+
if (timeout == 0) {
145+
return ping.get();
146+
}
147+
return ping.get(timeout, TimeUnit.SECONDS);
148+
} catch (TimeoutException e) {
149+
ping.cancel(true);
150+
return false;
151+
} catch (InterruptedException e) {
152+
Thread.currentThread().interrupt();
153+
return false;
154+
} catch (ExecutionException e) {
155+
return false;
156+
}
117157
}
118158

119159
@Override

com.dbeaver.jdbc.driver.libsql/src/test/java/com/dbeaver/jdbc/upd/driver/test/LibSqlDriverTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ public static void main(String[] args) throws Exception {
3535
System.out.println("Driver: " + metaData.getDriverName());
3636
System.out.println("Database: " + metaData.getDatabaseProductName() + " " + metaData.getDatabaseProductVersion());
3737

38+
System.out.println("isValid(5) = " + connection.isValid(5));
39+
try {
40+
connection.isValid(-1);
41+
System.out.println("BUG: negative timeout did not throw");
42+
} catch (SQLException e) {
43+
System.out.println("isValid(-1) correctly threw: " + e.getMessage());
44+
}
45+
3846
System.out.println("Query:");
3947
try (Statement dbStat = connection.createStatement()) {
4048
try (ResultSet dbResult = dbStat.executeQuery("select * from testme")) {
@@ -61,6 +69,8 @@ public static void main(String[] args) throws Exception {
6169
}
6270
}
6371

72+
connection.close();
73+
System.out.println("isValid(5) after close = " + connection.isValid(5));
6474
}
6575
} finally {
6676
System.out.println("Finished (" + (System.currentTimeMillis() - startTime) + "ms)");

0 commit comments

Comments
 (0)