Skip to content

SQLite: failed multi-statement execute_sql leaves an explicit BEGIN transaction open on the shared connection #554

Backlog

Description

@MicroMilo

Summary

When an execute_sql call on a SQLite source starts a transaction (BEGIN) and a later statement in the same batch fails, SQLiteConnector.executeSQL does not roll back the open transaction on the shared DatabaseSync connection.

As a result, after returning an EXECUTION_ERROR:

  1. The uncommitted writes from the failed batch remain visible to subsequent independent execute_sql calls (and the SQLite rollback journal remains on disk).
  2. Any subsequent valid BEGIN call fails with cannot start a transaction within a transaction until a caller manually issues ROLLBACK.

Steps to Reproduce

Launch DBHub in stdio mode against a fresh SQLite database (--transport stdio --dsn sqlite:///tmp/repro.db, tested on @bytebase/dbhub@2.1.0, Node v22.14.0; src/connectors/sqlite/index.ts is unchanged on main 5128cf87) and invoke execute_sql with the following 4 sequential calls:

  1. Create baseline table and row:

    {"sql": "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT); INSERT INTO users (name) VALUES ('alice');"}

    -> Succeeds (success: true, 2 statements).

  2. Start a transaction, insert a row, then trigger an error in the same batch:

    {"sql": "BEGIN; INSERT INTO users (name) VALUES ('bob'); SELECT * FROM no_such_table;"}

    -> Returns isError: true:

    {"success": false, "error": "no such table: no_such_table", "code": "EXECUTION_ERROR"}

    A rollback journal (repro.db-journal) is left on disk because the transaction is still open.

  3. Run an independent read in a new tool call:

    {"sql": "SELECT id, name FROM users ORDER BY id"}

    -> Succeeds and returns 2 rows: [{"id": 1, "name": "alice"}, {"id": 2, "name": "bob"}] — the uncommitted 'bob' row from the failed call leaks across calls.

  4. Start a new transaction in a subsequent tool call:

    {"sql": "BEGIN"}

    -> Fails with isError: true:

    {"success": false, "error": "cannot start a transaction within a transaction", "code": "EXECUTION_ERROR"}

Cause

In src/connectors/sqlite/index.ts (SQLiteConnector.executeSQL, lines 510–582):

  • All execute_sql calls share a single persistent this.db (node:sqlite DatabaseSync) instance.
  • Statements in a batch are split and executed one by one in a for loop (lines 538–567).
  • If an error is thrown after a BEGIN statement has already run, the finally block (lines 570–581) only resets PRAGMA query_only = OFF when options.readonly is set, leaving the open transaction active on this.db.
  • Meanwhile, the execute_sql tool description ("Execute SQL queries on the sqlite database" / "SQL to execute (multiple statements separated by ;)") does not inform MCP callers that transactions persist across calls or require manual ROLLBACK after an error.

Suggested Fix

One of (or a combination of):

  1. Track whether the connection was already in a transaction before executeSQL ran (e.g., via this.db.isTransaction in node:sqlite), and in the error path execute a best-effort ROLLBACK if this call opened a transaction that was left uncommitted when the batch threw.
  2. If cross-call manual transactions are intentionally supported on SQLite, document the persistent connection / transaction lifecycle in the execute_sql tool description and still roll back transactions started inside a failed multi-statement batch so a single failing call cannot leave the shared connection stuck.

Environment

  • DBHub: @bytebase/dbhub@2.1.0 (src/connectors/sqlite/index.ts unchanged on main 5128cf87)
  • Runtime: Node.js v22.14.0 (node:sqlite), Ubuntu 22.04 (Docker)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions