SQLite Not transaction is active on this connection - Stack Overflow

admin2025-04-19  0

I am using sqlite and my code looks like below:

using(var trans = conn.BeginTransaction())
{
    var cmd = new SQLiteCommand(conn);
    cmd.CommandText = "Insert ...";
    cmd.Prepare();
    ...
    trans.Commit() // this line throws exception: No connection associated with this transaction
}

I trace into the System.Data.SQLite and I see the following code:


    internal bool IsValid(bool throwError)
    {
      if (this._cnn == null)
      {
        if (throwError)
          throw new ArgumentNullException("No connection associated with this transaction");
        return false;
      }
      if (this._cnn._version != this._version)
      {
        if (throwError)
          throw new SQLiteException("The connection was closed and re-opened, changes were already rolled back");
        return false;
      }
      if (this._cnn.State != ConnectionState.Open)
      {
        if (throwError)
          throw new SQLiteException("Connection was closed");
        return false;
      }
      if (this._cnn._transactionLevel != 0 && !this._cnn._sql.AutoCommit)
        return true;
      this._cnn._transactionLevel = 0;
      if (throwError)
        throw new SQLiteException("No transaction is active on this connection");
      return false;
    }

This phenomenon is not repeatable each time!

Anybody knows why?

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745058215a282534.html

最新回复(0)