ASP.NET MVC project 4.5 and EntityFramework Database First.
I have some command, which will be triggered when the user clicks a button
using (TransactionScope scope = new TransactionScope())
{
using (DbContext context = new DbContext())
{
//update about 3 tables
scope.Complete();
}
}
In the other hand, I have another method which ONLY read (not updating anything) data from one of the previous tables, but it's working every 2 seconds (there is a timer which triggers this read process).
Problem: sometimes (not always) I am receiving the following exception from the reading process (not the updating process).
System.Data.SqlClient.SqlException: Transaction (Process ID 57) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
although the update process is working correctly and success and although the read process fails (sometimes not always), which is not a big deal because It will be requested again after 2 seconds and success at that time. I am afraid that I am not doing it the correct way, Is there any advice to get rid of this exception completely?
NOTE: I was not receiving this exception before, I started to receive this exception when I started to use the TransactionScope
.
Update (Possible Solution)
Actually, I tried to play with the IsolationLevel
as suggested in the comments, Actually, this caused a great reduction in this exception.
I created the Transaction like the following
new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions()
{
IsolationLevel = IsolationLevel.RepeatableRead
})
NOTE: the level SnapShot
did not work because the database does not support this level.
Update here is the Deadlock profile