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?