tursodatabase/turso · error · TypeError
The database connection is not open
Error message
The database connection is not open
What it means
`Connection.prepare()` throws a TypeError once the connection has been closed: `close()` flips the internal `isOpen` flag, and every public entry point re-checks it before touching the session. A closed connection cannot describe or prepare statements, so the check fires before any I/O.
Solutions
- Create a new Connection for further work - closed connections are not reopened by prepare
- Sequence teardown so close() runs after all prepares and executions
- Track the lifecycle yourself and skip or reconnect before calling prepare
Example fix
// before
await conn.close();
const stmt = await conn.prepare('SELECT 1');
// after
const stmt = await conn.prepare('SELECT 1');
await conn.close(); Defensive patterns
Strategy: try-catch
Validate before calling
// Connection exposes no public open-state getter; mirror it in a wrapper.
let isClosed = false;
const safeClose = async () => { isClosed = true; await conn.close(); };
if (!isClosed) {
const stmt = await conn.prepare(sql);
} Try / catch
try {
const stmt = await conn.prepare(sql);
} catch (e) {
if (e instanceof TypeError && /not open/.test(e.message)) {
conn = new Connection(config); // recreate, then optionally retry once
} else {
throw e;
}
} Prevention
- Close connections exactly once, at the end of the lifecycle
- Never fire-and-forget operations that may outlive teardown
- In tests, create a fresh Connection per case instead of sharing one fixture
When it happens
Trigger: `await conn.prepare(sql)` after `await conn.close()`; sharing a module-level Connection and closing it on one request path while another path still prepares statements.
Common situations: Server shutdown or test teardown closing a shared connection; retry wrappers that close on error and then continue; singletons replaced during config reload while old references linger.
Related errors
- Cannot operate on a closed connection
- Cannot operate on a closed connection
- Cannot operate on a closed cursor
- Cannot operate on a closed cursor
- Command was not prepared.
AI-assisted analysis of tursodatabase/turso@c1e5928725 (2026-08-20).
Data as JSON: /api/errors/b6cd5e5be17df387.
Report an issue: GitHub.
Appendix: source
Thrown at serverless/javascript/src/connection.ts:173
/**
* Prepare a SQL statement for execution.
*
* Prepared statements created from a Connection use the same underlying session so transaction boundaries are preserved.
* This method fetches column metadata using the describe functionality.
*
* @param sql - The SQL statement to prepare
* @returns A Promise that resolves to a Statement object with column metadata
*
* @example
* ```typescript
* const stmt = await client.prepare("SELECT * FROM users WHERE id = ?");
* const columns = stmt.columns();
* const user = await stmt.get([123]);
* ```
*/
async prepare(sql: string): Promise<Statement> {
if (!this.isOpen) {
throw new TypeError("The database connection is not open");
}
// Describe on the existing session so it sees uncommitted DDL
// (e.g. CREATE TABLE in the same transaction).
await this.execLock.acquire();
let description;
try {
description = await this.session.describe(sql);
} finally {
this.execLock.release();
}
const stmt = Statement.fromSession(this.session, sql, description.cols, this.execLock);
if (this.defaultSafeIntegerMode) {
stmt.safeIntegers(true);
}
return stmt;
}View on GitHub (pinned to c1e5928725)