I searched for a while here and elsewhere and cannot find a good answer to the question why Linq-TO-SQL with NOLOCK is not possible.
Each time I try to apply the with (NOLOCK) hint to the Linq-To-SQL context (applies to a 1 sql statement), people often respond to force the transaction (TransactionScope) to set IsolationLevel to ReadUncommitted. Well, they rarely say that this leads to the fact that the connection for opening a transaction (which I also read somewhere, must be closed manually).
Using ReadUncommitted in my application as is, is really not so good. I am currently using contextual operators for the same connection to each other. How:
using( var ctx1 = new Context()) { ... some code here ... using( var ctx2 = new Context()) { ... some code here ... using( var ctx3 = new Context()) { ... some code here ... } ... some code here ... } ... some code here ... }
With a total runtime of 1 sec and many users at the same time, changing the isolation level will cause the contexts to wait for each other to release the connection, because all connections in the connection pool are used.
Thus, one (for many reasons) to switch to "nolock" is to avoid deadlocks (we now have one client deadlock per day). The consequence of this is another kind of dead end and does not really solve my problem.
So I know what I can do:
- Avoid nested using the same connection
- Increase server connection pool size
But my problem is:
- This is not possible in the near future due to many lines of code re-factoring, and it will conflict with the architecture (without even thinking about whether it is good or bad).
- Although this, of course, will work, this is what I would call a “symptomatic treatment” - because I don’t know how much the application will grow, and if it is a reliable solution for the future (and then I could end up with an even worse situation when far more users are affected.
My thoughts:
- Could it be true that NoLock is impossible (for each operator without starting a transaction)?
- If 1 is true - is it true, no one else got this problem and solved it in the general linq to sql modification?
- If 2 is true - why is this not a problem for others?
- Is there any other workaround I couldn't look at?
- Is using the same connection (nested) many times so bad practice that no one has this problem?
source share