I have encountered an error on executing UPDATE statement.
Error:
Snapshot isolation transaction aborted due to update conflict. You cannot use snapshot isolation to access table 'dbo.Companies' directly or indirectly in database 'myDatabase' to update, delete, or insert the row that has been modified or deleted by another transaction. Retry the transaction or change the isolation level for the update/delete statement.
Statement:
UPDATE [ActiveSession] SET [LastActionExecutedAt] = GETDATE() WHERE [SessionID]=@id
I ran above statement through ASP.NET application.
What is curious is that the statement is executed WITHOUT transaction. The code is like below:
using(var sqlconn = new SqlConnection("connection string")
using(var sqlcmd = sqlconn.CreateCommand())
{
sqlconn.Open();
sqlcmd.Parameters.Add("@id", SqlDbType.NVarChar).Value = id;
sqlcmd.CommandText = "SELECT * FROM [ActiveSession] WHERE @id=id";
using(var sqlreader = sqlcmd.ExecuteReader())
{
// read info
reader.Close();
}
sqlcmd.CommandText = "UPDATE ....";
Sqlcmd.ExecuteNonQuery();
}
I have googled and only find that the error occurs when I use SNAPSHOT isolation level transaction. BUT there is no transaction.
As described in this thread, INDEXING will help in some situations, but this does not help me.
Any idea about this error? Any help will be appreciated.
Thanks,