[...]
My doubt is about disposing dbContext. For example, if I get an exception such as null reference in my code, becuase I try to access to a null variable. Will the dbContext dipose in both cases? or in some of the cases will not?
I think that the dbContext will be disposed in both of the cases, according to the purpose of using.
However, when you use the try–using variant, if an exception occurs inside the using, such as your concurrency issue, and then another one occurs inside the auto-disposing part provided by using, then the second exception will mask the first one. Consequently, the catch statements will not be able to react properly to concurrency exception, since the exception was suppressed. (That’s why throwing exception from Dispose is considered a contestable practice).
Therefore, consider the other variant too: using–try. In this case the inner try–catch will be able to catch the concurrency exception.
No comments:
Post a Comment