Whether it is advisable to use SemaphoreSlim in db transaction?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have this piece of code that I am using for updating a record.
I want to know how to use SemaphoreSlim if there are multiple await statement in one block of code.
Whether deadlock and recursive locks can occur for the below piece of code, and when it occurs,how to avoid Deadlock and recursive locks when we are using SemaphoreSlim?
SemaphoreSlim writelock = new SemaphoreSlim(1,1);
Public async task AddValueToDB(Valuedto value)
{
try{
await writelock.WaitAsync();
_db.tblAction.add(Valuedto);
await _db.SavechangesAsync();
}
catch(Exception ex){throw ex;}
finally{writelock.Release();}
}
}
Please let me know if you want any more information about my requirement.
c# multithreading async-await
|
show 9 more comments
I have this piece of code that I am using for updating a record.
I want to know how to use SemaphoreSlim if there are multiple await statement in one block of code.
Whether deadlock and recursive locks can occur for the below piece of code, and when it occurs,how to avoid Deadlock and recursive locks when we are using SemaphoreSlim?
SemaphoreSlim writelock = new SemaphoreSlim(1,1);
Public async task AddValueToDB(Valuedto value)
{
try{
await writelock.WaitAsync();
_db.tblAction.add(Valuedto);
await _db.SavechangesAsync();
}
catch(Exception ex){throw ex;}
finally{writelock.Release();}
}
}
Please let me know if you want any more information about my requirement.
c# multithreading async-await
2
Well you shouldn't be sharing Entity Framework contexts between threads so this question is perhaps moot. stackoverflow.com/questions/52027908/…
– MickyD
Nov 23 '18 at 4:46
2
...by the way you can delete thecatch(Exception ex){throw ex;}
line. It's perfectly fine just to do atry-finally
. There's nocatch
to it (pun intended)
– MickyD
Nov 23 '18 at 4:51
2
Andthrow ex
is not a rethrow. It's a new throw of a new exception. docs.microsoft.com/en-us/dotnet/csharp/language-reference/…
– Paulo Morgado
Nov 23 '18 at 7:34
When? Never. It's never needed but can easily cause trouble by holding database locks for longer, increasing blocking and possibly causing deadlocks. Why do you think you need any kind of locking in the first place? What are you trying to do?
– Panagiotis Kanavos
Nov 23 '18 at 7:44
If you want to insert records fast, useSqlBulkCopy
to insert data using the bulk copy mechanism of SQL Server with minimal locking and logging. Or just make allAdd
calls and callSaveChangesAsync()
only once at the end. EF will generate a batch of statements and execute them all at once.
– Panagiotis Kanavos
Nov 23 '18 at 7:46
|
show 9 more comments
I have this piece of code that I am using for updating a record.
I want to know how to use SemaphoreSlim if there are multiple await statement in one block of code.
Whether deadlock and recursive locks can occur for the below piece of code, and when it occurs,how to avoid Deadlock and recursive locks when we are using SemaphoreSlim?
SemaphoreSlim writelock = new SemaphoreSlim(1,1);
Public async task AddValueToDB(Valuedto value)
{
try{
await writelock.WaitAsync();
_db.tblAction.add(Valuedto);
await _db.SavechangesAsync();
}
catch(Exception ex){throw ex;}
finally{writelock.Release();}
}
}
Please let me know if you want any more information about my requirement.
c# multithreading async-await
I have this piece of code that I am using for updating a record.
I want to know how to use SemaphoreSlim if there are multiple await statement in one block of code.
Whether deadlock and recursive locks can occur for the below piece of code, and when it occurs,how to avoid Deadlock and recursive locks when we are using SemaphoreSlim?
SemaphoreSlim writelock = new SemaphoreSlim(1,1);
Public async task AddValueToDB(Valuedto value)
{
try{
await writelock.WaitAsync();
_db.tblAction.add(Valuedto);
await _db.SavechangesAsync();
}
catch(Exception ex){throw ex;}
finally{writelock.Release();}
}
}
Please let me know if you want any more information about my requirement.
c# multithreading async-await
c# multithreading async-await
edited Nov 23 '18 at 5:08
Mihail Stancescu
3,58011119
3,58011119
asked Nov 23 '18 at 4:34
Nilanjan SenNilanjan Sen
86
86
2
Well you shouldn't be sharing Entity Framework contexts between threads so this question is perhaps moot. stackoverflow.com/questions/52027908/…
– MickyD
Nov 23 '18 at 4:46
2
...by the way you can delete thecatch(Exception ex){throw ex;}
line. It's perfectly fine just to do atry-finally
. There's nocatch
to it (pun intended)
– MickyD
Nov 23 '18 at 4:51
2
Andthrow ex
is not a rethrow. It's a new throw of a new exception. docs.microsoft.com/en-us/dotnet/csharp/language-reference/…
– Paulo Morgado
Nov 23 '18 at 7:34
When? Never. It's never needed but can easily cause trouble by holding database locks for longer, increasing blocking and possibly causing deadlocks. Why do you think you need any kind of locking in the first place? What are you trying to do?
– Panagiotis Kanavos
Nov 23 '18 at 7:44
If you want to insert records fast, useSqlBulkCopy
to insert data using the bulk copy mechanism of SQL Server with minimal locking and logging. Or just make allAdd
calls and callSaveChangesAsync()
only once at the end. EF will generate a batch of statements and execute them all at once.
– Panagiotis Kanavos
Nov 23 '18 at 7:46
|
show 9 more comments
2
Well you shouldn't be sharing Entity Framework contexts between threads so this question is perhaps moot. stackoverflow.com/questions/52027908/…
– MickyD
Nov 23 '18 at 4:46
2
...by the way you can delete thecatch(Exception ex){throw ex;}
line. It's perfectly fine just to do atry-finally
. There's nocatch
to it (pun intended)
– MickyD
Nov 23 '18 at 4:51
2
Andthrow ex
is not a rethrow. It's a new throw of a new exception. docs.microsoft.com/en-us/dotnet/csharp/language-reference/…
– Paulo Morgado
Nov 23 '18 at 7:34
When? Never. It's never needed but can easily cause trouble by holding database locks for longer, increasing blocking and possibly causing deadlocks. Why do you think you need any kind of locking in the first place? What are you trying to do?
– Panagiotis Kanavos
Nov 23 '18 at 7:44
If you want to insert records fast, useSqlBulkCopy
to insert data using the bulk copy mechanism of SQL Server with minimal locking and logging. Or just make allAdd
calls and callSaveChangesAsync()
only once at the end. EF will generate a batch of statements and execute them all at once.
– Panagiotis Kanavos
Nov 23 '18 at 7:46
2
2
Well you shouldn't be sharing Entity Framework contexts between threads so this question is perhaps moot. stackoverflow.com/questions/52027908/…
– MickyD
Nov 23 '18 at 4:46
Well you shouldn't be sharing Entity Framework contexts between threads so this question is perhaps moot. stackoverflow.com/questions/52027908/…
– MickyD
Nov 23 '18 at 4:46
2
2
...by the way you can delete the
catch(Exception ex){throw ex;}
line. It's perfectly fine just to do a try-finally
. There's no catch
to it (pun intended)– MickyD
Nov 23 '18 at 4:51
...by the way you can delete the
catch(Exception ex){throw ex;}
line. It's perfectly fine just to do a try-finally
. There's no catch
to it (pun intended)– MickyD
Nov 23 '18 at 4:51
2
2
And
throw ex
is not a rethrow. It's a new throw of a new exception. docs.microsoft.com/en-us/dotnet/csharp/language-reference/…– Paulo Morgado
Nov 23 '18 at 7:34
And
throw ex
is not a rethrow. It's a new throw of a new exception. docs.microsoft.com/en-us/dotnet/csharp/language-reference/…– Paulo Morgado
Nov 23 '18 at 7:34
When? Never. It's never needed but can easily cause trouble by holding database locks for longer, increasing blocking and possibly causing deadlocks. Why do you think you need any kind of locking in the first place? What are you trying to do?
– Panagiotis Kanavos
Nov 23 '18 at 7:44
When? Never. It's never needed but can easily cause trouble by holding database locks for longer, increasing blocking and possibly causing deadlocks. Why do you think you need any kind of locking in the first place? What are you trying to do?
– Panagiotis Kanavos
Nov 23 '18 at 7:44
If you want to insert records fast, use
SqlBulkCopy
to insert data using the bulk copy mechanism of SQL Server with minimal locking and logging. Or just make all Add
calls and call SaveChangesAsync()
only once at the end. EF will generate a batch of statements and execute them all at once.– Panagiotis Kanavos
Nov 23 '18 at 7:46
If you want to insert records fast, use
SqlBulkCopy
to insert data using the bulk copy mechanism of SQL Server with minimal locking and logging. Or just make all Add
calls and call SaveChangesAsync()
only once at the end. EF will generate a batch of statements and execute them all at once.– Panagiotis Kanavos
Nov 23 '18 at 7:46
|
show 9 more comments
0
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',
autoActivateHeartbeat: false,
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440695%2fwhether-it-is-advisable-to-use-semaphoreslim-in-db-transaction%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440695%2fwhether-it-is-advisable-to-use-semaphoreslim-in-db-transaction%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
2
Well you shouldn't be sharing Entity Framework contexts between threads so this question is perhaps moot. stackoverflow.com/questions/52027908/…
– MickyD
Nov 23 '18 at 4:46
2
...by the way you can delete the
catch(Exception ex){throw ex;}
line. It's perfectly fine just to do atry-finally
. There's nocatch
to it (pun intended)– MickyD
Nov 23 '18 at 4:51
2
And
throw ex
is not a rethrow. It's a new throw of a new exception. docs.microsoft.com/en-us/dotnet/csharp/language-reference/…– Paulo Morgado
Nov 23 '18 at 7:34
When? Never. It's never needed but can easily cause trouble by holding database locks for longer, increasing blocking and possibly causing deadlocks. Why do you think you need any kind of locking in the first place? What are you trying to do?
– Panagiotis Kanavos
Nov 23 '18 at 7:44
If you want to insert records fast, use
SqlBulkCopy
to insert data using the bulk copy mechanism of SQL Server with minimal locking and logging. Or just make allAdd
calls and callSaveChangesAsync()
only once at the end. EF will generate a batch of statements and execute them all at once.– Panagiotis Kanavos
Nov 23 '18 at 7:46