Repository navigation
Description
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:
- The uncommitted writes from the failed batch remain visible to subsequent independent
execute_sqlcalls (and the SQLite rollback journal remains on disk). - Any subsequent valid
BEGINcall fails withcannot start a transaction within a transactionuntil a caller manually issuesROLLBACK.
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:
-
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). -
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. -
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. -
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_sqlcalls share a single persistentthis.db(node:sqliteDatabaseSync) instance. - Statements in a batch are split and executed one by one in a
forloop (lines 538–567). - If an error is thrown after a
BEGINstatement has already run, thefinallyblock (lines 570–581) only resetsPRAGMA query_only = OFFwhenoptions.readonlyis set, leaving the open transaction active onthis.db. - Meanwhile, the
execute_sqltool 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 manualROLLBACKafter an error.
Suggested Fix
One of (or a combination of):
- Track whether the connection was already in a transaction before
executeSQLran (e.g., viathis.db.isTransactioninnode:sqlite), and in the error path execute a best-effortROLLBACKif this call opened a transaction that was left uncommitted when the batch threw. - If cross-call manual transactions are intentionally supported on SQLite, document the persistent connection / transaction lifecycle in the
execute_sqltool 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.tsunchanged onmain5128cf87) - Runtime: Node.js v22.14.0 (
node:sqlite), Ubuntu 22.04 (Docker)
Activity