{"record":{"id":"708bbfc8867597cc","repo":"tursodatabase/turso","slug":"database-connection-closed","errorCode":null,"errorMessage":"database connection closed","messagePattern":"database connection closed","errorType":"exception","errorClass":"SQLException","httpStatus":null,"severity":"error","filePath":"bindings/java/src/main/java/tech/turso/core/TursoConnection.java","lineNumber":64,"sourceCode":"   * Creates a connection using an existing TursoDB instance. This is useful for encrypted databases\n   * created with TursoDB.createWithEncryption().\n   *\n   * @param url e.g. \"jdbc:turso:fileName\"\n   * @param database an existing TursoDB instance\n   */\n  public TursoConnection(String url, TursoDB database) throws SQLException {\n    this.url = url;\n    this.database = database;\n    this.connectionPtr = this.database.connect();\n  }\n\n  private static TursoDB open(String url, String filePath, Properties properties)\n      throws SQLException {\n    return TursoDB.create(url, filePath);\n  }\n\n  public void checkOpen() throws SQLException {\n    if (isClosed()) throw new SQLException(\"database connection closed\");\n  }\n\n  public String getUrl() {\n    return url;\n  }\n\n  public void close() throws SQLException {\n    if (isClosed()) {\n      return;\n    }\n\n    // Roll back any pending transaction before closing\n    synchronized (transactionLock) {\n      if (inTransaction) {\n        try {\n          executeInternal(\"ROLLBACK\");\n        } catch (SQLException e) {\n          // Log but don't throw - we're closing anyway","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/tursodatabase/turso/blob/244cde92a7df7f9b8b8b7a4075c35a12977e303e/bindings/java/src/main/java/tech/turso/core/TursoConnection.java#L46-L82","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Guard usage: `if (conn.isClosed()) reopenOrSkip();` before executing","Fix ownership: only the component that owns the connection closes it; pools must return, not close","If using a pool, enable test-on-borrow so closed connections are evicted","Check shutdown ordering so background workers stop before connections close"],"exampleFix":"// before\ntry (Connection conn = DriverManager.getConnection(url)) {\n  scheduleLater(() -> conn.createStatement()); // throws later: closed\n}\n\n// after\nConnection conn = DriverManager.getConnection(url);\ntry {\n  use(conn);\n} finally {\n  if (!conn.isClosed()) conn.close();\n}","handlingStrategy":"validation","validationCode":"// guard any connection handoff\nif (conn.isClosed()) {\n  conn = DriverManager.getConnection(url, props); // or skip the task\n}","typeGuard":null,"tryCatchPattern":"try {\n  return conn.createStatement();\n} catch (SQLException e) {\n  if (\"database connection closed\".equals(e.getMessage())) { reopen(); retry(); }\n  throw e;\n}","preventionTips":["Establish single ownership: exactly one component is responsible for closing each connection","With pools, return connections instead of closing them, and enable test-on-borrow/validation queries","Order application shutdown so background workers stop before connection cleanup runs","Remember try-with-resources on a Connection closes it — do not let escaped statements/lambdas outlive the block"],"tags":["connection","lifecycle","java","jdbc"],"backgroundTag":"connection-already-closed","analyzedSha":"244cde92a7df7f9b8b8b7a4075c35a12977e303e","analyzedAt":"2026-08-20T07:02:18.389Z","contentChangedAt":"2026-08-20T07:02:18.389Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}