{"record":{"id":"5148e5cc335f4d8c","repo":"tursodatabase/turso","slug":"the-database-connection-is-not-open","errorCode":null,"errorMessage":"The database connection is not open","messagePattern":"The database connection is not open","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"bindings/javascript/packages/common/compat.ts","lineNumber":159,"sourceCode":"    this.db.connectSync();\n\n    Object.defineProperties(this, {\n      name: { get: () => this.db.path },\n      readonly: { get: () => this.db.readonly },\n      open: { get: () => this.db.open },\n      memory: { get: () => this.db.memory },\n      inTransaction: { get: () => this.db.inTransaction() },\n    });\n  }\n\n  /**\n   * Prepares a SQL statement for execution.\n   *\n   * @param {string} sql - The SQL statement string to prepare.\n   */\n  prepare(sql) {\n    if (!this.open) {\n      throw new TypeError(\"The database connection is not open\");\n    }\n    if (!sql) {\n      throw new RangeError(\"The supplied SQL string contains no statements\");\n    }\n\n    try {\n      return new Statement(this.db.prepare(sql), this.db);\n    } catch (err) {\n      throw convertError(err);\n    }\n  }\n\n  /**\n   * Returns a function that executes the given function in a transaction.\n   *\n   * @param {function} fn - The function to wrap in a transaction.\n   */\n  transaction(fn) {","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/tursodatabase/turso/blob/bad083fafbefdeae9a42ec19bdaaad8918dcf411/bindings/javascript/packages/common/compat.ts#L141-L177","documentation":"Thrown by Database.prepare() in the better-sqlite3 compatibility layer when you try to prepare a statement on a connection whose native handle is no longer open. The `open` property is a live getter over the native database, so once close() has been called (or the connection was never successfully opened), any subsequent prepare() is rejected immediately with this TypeError instead of crashing the native layer.","triggerScenarios":"Calling db.prepare(sql) after db.close(); calling prepare() during shutdown of a long-lived process (e.g. after a SIGTERM handler closed the DB but a queued query still runs); sharing a Database instance across modules where one path closes it; calling prepare() after an unrecoverable native error closed the connection.","commonSituations":"Unit tests that close the DB in afterEach but a stray test still queries it; hot-reload in dev servers that closes the old connection while in-flight requests still hold a reference to the old Database object; graceful-shutdown code that closes the DB before draining the request queue.","solutions":["Check `db.open` before preparing: if (db.open) stmt = db.prepare(sql)","Reorder shutdown: drain in-flight queries first, then close the connection (or use an async-dispose queue)","If the connection was closed unexpectedly, reopen it (recreate the Database) and retry the operation","Audit for double-close or premature close paths (SIGINT handlers, test teardown, connection-pool eviction)"],"exampleFix":"// before\nconst row = db.prepare('SELECT 1').get(); // db was closed earlier\n\n// after\nif (!db.open) db = new Database(...); // or skip/reopen\nconst row = db.prepare('SELECT 1').get();","handlingStrategy":"validation","validationCode":"if (!db.open) {\n  throw new Error('database is closed; reopen before preparing statements');\n}\nconst stmt = db.prepare(sql);","typeGuard":"function isOpen(db: { open: boolean }): boolean {\n  return db.open === true;\n}","tryCatchPattern":"try {\n  stmt = db.prepare(sql);\n} catch (err) {\n  if (err instanceof TypeError && err.message === 'The database connection is not open') {\n    db = new Database(path); // or mark connection stale and reschedule\n    stmt = db.prepare(sql);\n  } else {\n    throw err;\n  }\n}","preventionTips":["Own the connection in one place; never let unrelated modules call close()","Check db.open before any prepare/exec/batch in queue workers and timers","Drain in-flight work before closing during shutdown","In tests, create and close the Database within the same suite lifecycle"],"tags":["turso","javascript","sqlite","connection-lifecycle","better-sqlite3-compat"],"backgroundTag":"database-connection-closed","analyzedSha":"bad083fafbefdeae9a42ec19bdaaad8918dcf411","analyzedAt":"2026-08-16T23:12:11.798Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}