I have a C# console application with the following code:
using (var cn = new SqlConnection())
{
cn.ConnectionString = "Data Source=localhost;Integrated Security=True;Persist Security Info=False";
cn.Open();
System.Console.WriteLine("past open");
}
Sys开发者_JAVA技巧tem.Console.ReadLine();
When running, I set two breakpoints. One at the WriteLine and one at the ReadLine. I also have Sql Server Management Studio (SSMS) opened and am watching the Activity Monitor's Processes list.
When I run the application and hit my first breakpoint (at the WriteLine), I soon see a Process, in the SSMS Processes list, whose application name is ".Net SqlClient Data Provider" and whose Database is "master".
When I F5 to the second breakpoint, my expectation is that the process will be removed from the Processes list since I've closed my SqlConnection by that point. However, the process remains in the list until the application closes.
My assumption is that the process remains in the list since my connection is not really closing. If my assumption is accurate, then what else do I need to do to close the connection? If my assumption is inaccurate, then why is my process still listed and how can I test if my connection was actually closed properly?
Thanks.
As i know - when you're using Close or Dispose method - you're just returning your connection to a connection pool to be reused (connection won't be really closed) - you can get more info here.
In addition to DanNsk's answer, it could be that the SPID is being reused if the connection is really closed and not returned to the connection pool
精彩评论