tursodatabase/turso · error · SQLException

database connection closed

Error message

database connection closed

What it means

checkOpen() throws this SQLException from every public operation on TursoConnection once close() has run (close() rolls back any pending transaction, calls the native _close, and sets the closed flag). It is the standard JDBC guard against use-after-close.

Solutions

  1. Guard usage: `if (conn.isClosed()) reopenOrSkip();` before executing
  2. Fix ownership: only the component that owns the connection closes it; pools must return, not close
  3. If using a pool, enable test-on-borrow so closed connections are evicted
  4. Check shutdown ordering so background workers stop before connections close

Example fix

// before
try (Connection conn = DriverManager.getConnection(url)) {
  scheduleLater(() -> conn.createStatement()); // throws later: closed
}

// after
Connection conn = DriverManager.getConnection(url);
try {
  use(conn);
} finally {
  if (!conn.isClosed()) conn.close();
}
Defensive patterns

Strategy: validation

Validate before calling

// guard any connection handoff
if (conn.isClosed()) {
  conn = DriverManager.getConnection(url, props); // or skip the task
}

Try / catch

try {
  return conn.createStatement();
} catch (SQLException e) {
  if ("database connection closed".equals(e.getMessage())) { reopen(); retry(); }
  throw e;
}

Prevention

When it happens

Trigger: Calling createStatement/prepareStatement/commit/rollback/setAutoCommit/setTransactionIsolation on a connection after close(); a connection pool handing out a connection another thread closed; cleanup code closing a shared connection that other components still use.

Common situations: Application shutdown ordering: a daemon thread still executing queries while the main thread closes connections; pooled connections closed by a validation sweep; forgetting that try-with-resources on the Connection closes it for later users.

Understand the failure class

Related errors


AI-assisted analysis of tursodatabase/turso@244cde92a7 (2026-08-20). Data as JSON: /api/errors/708bbfc8867597cc. Report an issue: GitHub.

Appendix: source

Thrown at bindings/java/src/main/java/tech/turso/core/TursoConnection.java:64

   * Creates a connection using an existing TursoDB instance. This is useful for encrypted databases
   * created with TursoDB.createWithEncryption().
   *
   * @param url e.g. "jdbc:turso:fileName"
   * @param database an existing TursoDB instance
   */
  public TursoConnection(String url, TursoDB database) throws SQLException {
    this.url = url;
    this.database = database;
    this.connectionPtr = this.database.connect();
  }

  private static TursoDB open(String url, String filePath, Properties properties)
      throws SQLException {
    return TursoDB.create(url, filePath);
  }

  public void checkOpen() throws SQLException {
    if (isClosed()) throw new SQLException("database connection closed");
  }

  public String getUrl() {
    return url;
  }

  public void close() throws SQLException {
    if (isClosed()) {
      return;
    }

    // Roll back any pending transaction before closing
    synchronized (transactionLock) {
      if (inTransaction) {
        try {
          executeInternal("ROLLBACK");
        } catch (SQLException e) {
          // Log but don't throw - we're closing anyway

View on GitHub (pinned to 244cde92a7)