문제

After I use

using (TestDatabaseEntitites ctx = new TestDatabaseEntitites())
{
    ....
}

I want all my sql connections to close after the using statement and I think that is what's expected. But it doesn't happen. Even after ctx.Dispose().

The only way I can close all the open connections is clearAllPools method. Am I doing something wrong? Thanks.

도움이 되었습니까?

해결책

Connection pool connections in .Net stay open behind the scenes by design. The reason for this is that opening a connection to the database is generally a very expensive operation which would take literally seconds.

For this reason, closing a connection will only reset it, so that a future Open will simply be passed the old reset connection instead of opening a new one.

다른 팁

This answer is referred from here

DbContext is a very light-weight object.

It doesn't matter whether your DbContext stays alive or you instantiate it just before making the call because the actual DB Connection only opens when you SubmitChanges or Enumerate the query (in that case it is closed on end of enumeration).

In your specific case. It doesn't matter at all.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top