Transaction was deadlocked on lock resources with another process and has been chosen as the deadlock victim....











up vote
0
down vote

favorite












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



enter image description here










share|improve this question
























  • why not use the newer, Database.BeginTransaction also, take a look at isolation level
    – TheGeneral
    yesterday








  • 1




    May be you already read this article - Transaction (Process ID) was deadlocked on lock resources with another process and has been chosen as the deadlock victim (Msg 1205)
    – ikram
    yesterday










  • @TheGeneral Actually I am considering that but there are some technical problems by using this, I mean no problem, I can apply this, but it will take some time (about an hour to apply it), Are you sure it could solve the problem? if so I can invest in that
    – Hakam Fostok
    yesterday










  • @ikram Thank you, I read that article, it is kind of helpful, but It did not give the cure I need.
    – Hakam Fostok
    yesterday










  • BeginTransaction wont solve your problem, and db deadlocks can be caused by very subtle and sometimes non obvious situations, however, from your description, i would try to play around with isolation levels if the transaction has provoked the problem.
    – TheGeneral
    yesterday















up vote
0
down vote

favorite












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



enter image description here










share|improve this question
























  • why not use the newer, Database.BeginTransaction also, take a look at isolation level
    – TheGeneral
    yesterday








  • 1




    May be you already read this article - Transaction (Process ID) was deadlocked on lock resources with another process and has been chosen as the deadlock victim (Msg 1205)
    – ikram
    yesterday










  • @TheGeneral Actually I am considering that but there are some technical problems by using this, I mean no problem, I can apply this, but it will take some time (about an hour to apply it), Are you sure it could solve the problem? if so I can invest in that
    – Hakam Fostok
    yesterday










  • @ikram Thank you, I read that article, it is kind of helpful, but It did not give the cure I need.
    – Hakam Fostok
    yesterday










  • BeginTransaction wont solve your problem, and db deadlocks can be caused by very subtle and sometimes non obvious situations, however, from your description, i would try to play around with isolation levels if the transaction has provoked the problem.
    – TheGeneral
    yesterday













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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



enter image description here










share|improve this question















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



enter image description here







c# entity-framework deadlock transactionscope






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday

























asked yesterday









Hakam Fostok

5,05583663




5,05583663












  • why not use the newer, Database.BeginTransaction also, take a look at isolation level
    – TheGeneral
    yesterday








  • 1




    May be you already read this article - Transaction (Process ID) was deadlocked on lock resources with another process and has been chosen as the deadlock victim (Msg 1205)
    – ikram
    yesterday










  • @TheGeneral Actually I am considering that but there are some technical problems by using this, I mean no problem, I can apply this, but it will take some time (about an hour to apply it), Are you sure it could solve the problem? if so I can invest in that
    – Hakam Fostok
    yesterday










  • @ikram Thank you, I read that article, it is kind of helpful, but It did not give the cure I need.
    – Hakam Fostok
    yesterday










  • BeginTransaction wont solve your problem, and db deadlocks can be caused by very subtle and sometimes non obvious situations, however, from your description, i would try to play around with isolation levels if the transaction has provoked the problem.
    – TheGeneral
    yesterday


















  • why not use the newer, Database.BeginTransaction also, take a look at isolation level
    – TheGeneral
    yesterday








  • 1




    May be you already read this article - Transaction (Process ID) was deadlocked on lock resources with another process and has been chosen as the deadlock victim (Msg 1205)
    – ikram
    yesterday










  • @TheGeneral Actually I am considering that but there are some technical problems by using this, I mean no problem, I can apply this, but it will take some time (about an hour to apply it), Are you sure it could solve the problem? if so I can invest in that
    – Hakam Fostok
    yesterday










  • @ikram Thank you, I read that article, it is kind of helpful, but It did not give the cure I need.
    – Hakam Fostok
    yesterday










  • BeginTransaction wont solve your problem, and db deadlocks can be caused by very subtle and sometimes non obvious situations, however, from your description, i would try to play around with isolation levels if the transaction has provoked the problem.
    – TheGeneral
    yesterday
















why not use the newer, Database.BeginTransaction also, take a look at isolation level
– TheGeneral
yesterday






why not use the newer, Database.BeginTransaction also, take a look at isolation level
– TheGeneral
yesterday






1




1




May be you already read this article - Transaction (Process ID) was deadlocked on lock resources with another process and has been chosen as the deadlock victim (Msg 1205)
– ikram
yesterday




May be you already read this article - Transaction (Process ID) was deadlocked on lock resources with another process and has been chosen as the deadlock victim (Msg 1205)
– ikram
yesterday












@TheGeneral Actually I am considering that but there are some technical problems by using this, I mean no problem, I can apply this, but it will take some time (about an hour to apply it), Are you sure it could solve the problem? if so I can invest in that
– Hakam Fostok
yesterday




@TheGeneral Actually I am considering that but there are some technical problems by using this, I mean no problem, I can apply this, but it will take some time (about an hour to apply it), Are you sure it could solve the problem? if so I can invest in that
– Hakam Fostok
yesterday












@ikram Thank you, I read that article, it is kind of helpful, but It did not give the cure I need.
– Hakam Fostok
yesterday




@ikram Thank you, I read that article, it is kind of helpful, but It did not give the cure I need.
– Hakam Fostok
yesterday












BeginTransaction wont solve your problem, and db deadlocks can be caused by very subtle and sometimes non obvious situations, however, from your description, i would try to play around with isolation levels if the transaction has provoked the problem.
– TheGeneral
yesterday




BeginTransaction wont solve your problem, and db deadlocks can be caused by very subtle and sometimes non obvious situations, however, from your description, i would try to play around with isolation levels if the transaction has provoked the problem.
– TheGeneral
yesterday

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53370900%2ftransaction-was-deadlocked-on-lock-resources-with-another-process-and-has-been-c%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53370900%2ftransaction-was-deadlocked-on-lock-resources-with-another-process-and-has-been-c%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

A Topological Invariant for $pi_3(U(n))$