{"record":{"id":"7dae94aff748fc7a","repo":"tursodatabase/turso","slug":"the-transaction-connection-does-not-match-the-comm","errorCode":null,"errorMessage":"The transaction connection does not match the command connection.","messagePattern":"The transaction connection does not match the command connection\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"bindings/dotnet/src/Turso.Data.Sqlite/SqliteCommand.cs","lineNumber":591,"sourceCode":"        }\n    }\n\n    private void ThrowIfReaderOpen(string property)\n    {\n        if (_hasOpenReader)\n            throw new InvalidOperationException(Properties.Resources.SetRequiresNoOpenReader(property));\n    }\n\n    private void EnsureExecutable(string method)\n    {\n        if (_hasOpenReader)\n            throw new InvalidOperationException(Properties.Resources.DataReaderOpen);\n        if (Connection is null || Connection.State != ConnectionState.Open)\n            throw new InvalidOperationException(Properties.Resources.CallRequiresOpenConnection(method));\n        if (Transaction is { IsCompleted: true } or { WasRolledBackExternally: true })\n            throw new InvalidOperationException(Properties.Resources.TransactionCompleted);\n        if (Transaction is not null && !ReferenceEquals(Transaction.Connection, Connection))\n            throw new InvalidOperationException(Properties.Resources.TransactionConnectionMismatch);\n\n        var connectionTransaction = Connection.Transaction;\n        if (connectionTransaction is null || ReferenceEquals(Transaction, connectionTransaction))\n            return;\n        if (connectionTransaction.IsCompleted)\n            throw new InvalidOperationException(Properties.Resources.TransactionCompleted);\n        if (!IsTransactionControlCommand(CommandText))\n            throw new InvalidOperationException(Properties.Resources.TransactionRequired);\n    }\n\n    private void CloseReader()\n    {\n        _hasOpenReader = false;\n        Connection?.ReaderClosed();\n    }\n\n    internal Action OwnBufferedReader()\n    {","sourceCodeStart":573,"sourceCodeEnd":609,"githubUrl":"https://github.com/tursodatabase/turso/blob/6c7252267988c76e632af00a671e4b9788dfae13/bindings/dotnet/src/Turso.Data.Sqlite/SqliteCommand.cs#L573-L609","documentation":"EnsureExecutable runs before every Execute and requires that, when the command has a transaction, it was begun on the exact same SqliteConnection instance bound to the command (ReferenceEquals check). Mismatched pairs throw InvalidOperationException, because transaction state is scoped per connection object - a foreign pairing could not be honored by the engine.","triggerScenarios":"cmd.Connection set to connA while cmd.Transaction is a transaction begun on connB (two connections to the same file); pooled/reused command objects carrying an old Transaction after Connection is reassigned; reconnect logic replacing the connection but not the transaction.","commonSituations":"Request-scoped DI handing different connection instances to data and transaction paths; reconnect-after-failure code paths; parallel workers each holding a connection while sharing command templates.","solutions":["Set cmd.Transaction to the transaction begun on the very connection assigned to cmd.Connection.","Create commands via conn.CreateCommand() after BeginTransaction and assign both together.","After any reconnect, re-begin the transaction and re-assign it to the commands."],"exampleFix":"// before\ncmd.Connection = connA;\ncmd.Transaction = connB.BeginTransaction(); // wrong connection -> mismatch\n\n// after\nusing var tx = connA.BeginTransaction();\ncmd.Connection = connA;\ncmd.Transaction = tx;","handlingStrategy":"validation","validationCode":"static void Bind(SqliteCommand cmd, SqliteConnection conn, SqliteTransaction tx) {\n    cmd.Connection = conn;\n    cmd.Transaction = ReferenceEquals(tx.Connection, conn)\n        ? tx\n        : throw new InvalidOperationException(\"transaction belongs to another connection\");\n}","typeGuard":null,"tryCatchPattern":"catch (InvalidOperationException) when (cmd.Transaction is not null &&\n        !ReferenceEquals(cmd.Transaction.Connection, cmd.Connection)) {\n    // re-pair: move the command to the transaction's own connection, then retry\n}","preventionTips":["Always create commands after BeginTransaction on the same connection.","Never share command objects across connections.","Re-assign both Connection and Transaction after reconnects."],"tags":["dotnet","ado-net","transaction","connection-mismatch"],"backgroundTag":"transaction-connection-mismatch","analyzedSha":"6c7252267988c76e632af00a671e4b9788dfae13","analyzedAt":"2026-08-20T07:02:18.389Z","contentChangedAt":"2026-08-20T07:02:18.389Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}