jbogard / ContosoUniversityCore

MIT License
590 stars 150 forks source link

SQL Server deadlocks when using DbContextTransactionFilter? #21

Closed pdeffendol closed 7 years ago

pdeffendol commented 7 years ago

We used the DbContextTransactionFilter in one of our projects (ASP.Net Core 1, EF6, full .Net framework) to ensure that each request was wrapped up in a SQL Server transaction. We had to remove it because SQL Server was killing queries due to deadlocks and causing the app to crash, once the number of users ramped up. (We're talking handfuls of concurrent users, not hundreds.) Is this something anyone has come across before, and is there another way to deal with it other than turning off the per-request transaction?

jbogard commented 7 years ago

You've had to remove transactions? Or just transactions in the filter?

pdeffendol commented 7 years ago

Sorry, I should read through again before submitting - we removed the filter to address the problem. We made sure that our commands, etc., explicitly called SaveChanges(Async) at the end, which wraps up the changes in a BD transaction anyway.

jbogard commented 7 years ago

Gotcha. We haven't seen it, but we've also usually had that inside of a MediatR pipeline component, as opposed to the filter.

pdeffendol commented 7 years ago

Hmm, interesting, so something like:

Validation -- > Authorization --> Start transaction --> Command --> Save/Commit?

jbogard commented 7 years ago

Something like that, but I gotta be careful about validation, if it his the DB and it's in the transaction then I can get dirty/stale reads.

pdeffendol commented 7 years ago

Thanks for the feedback!