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
    21 hours ago








  • 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
    21 hours ago










  • @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
    21 hours ago










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










  • 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
    21 hours ago















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
    21 hours ago








  • 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
    21 hours ago










  • @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
    21 hours ago










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










  • 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
    21 hours ago













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 18 hours ago

























asked 21 hours ago









Hakam Fostok

5,02583663




5,02583663












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








  • 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
    21 hours ago










  • @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
    21 hours ago










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










  • 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
    21 hours ago


















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








  • 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
    21 hours ago










  • @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
    21 hours ago










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










  • 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
    21 hours ago
















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






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






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
21 hours ago




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
21 hours ago












@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
21 hours ago




@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
21 hours ago












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




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












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
21 hours ago




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
21 hours ago

















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

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python