javaconnectionduckdb

Duck DB Java API - Invalid Input Error: Attempting to execute an unsuccessful or closed pending query result


I'm using the Java Interface of Duck DB, and the code periodically throws the error:

Invalid Input Error: Attempting to execute an unsuccessful or closed pending query result

The overall flow of code is:

public class DuckDBStore {
   private Connection connection;
   private static final String DB_URL = "jdbc:duckdb:";

   // This method is called periodically.
   public void addMetric(String message) {
      try (Statement stmt = getConnection().createStatement()) {
         String query = "INSERT INTO my_table VALUES ('" + message + "')";
         stmt.execute(query);
      } catch (SQLException e) {
         System.out.println(e);
      }
   }

   public synchronized Connection getConnection() throws SQLException {
      if (connection == null) {
         connection = DriverManager.getConnection(DB_URL);
      } else if (!connection.isValid(5)) {
         connection.close();
         connection = DriverManager.getConnection(DB_URL);
      }

      return connection;
   }
}

The line throwing the error is !connection.isValid(5). The output of e.getCause() is null.

Any insight into fixing the error is much appreciated!

Currently, almost every call to addMetrics throws the error. We expect that the frequency be significantly less.

Things we have tried:


Solution

  • The fix was to remove the use of isValid and add a database name to the DB_URL so that the table is persisted across connections.