What is the relationship between open SqlConnections in the client app and processes in SQL Server?

后端 未结 1 444
一整个雨季
一整个雨季 2021-01-19 23:13

I just tried to make a simple schema change to a table in a SQL Server database (by using the Design tool in SMSS). Whenever I tried to save the change, it kept timing out.

相关标签:
1条回答
  • 2021-01-19 23:38

    If pooling=false in the connection string

    SqlConnection.Open() and Close() will exactly correlate to spids being created and destroyed. This results in very slow performance :)

    If pooling=true in the connection string

    Calling SqlConnection.Open() will either use an existing physical connection from the pool, or create a new one if none are available in the pool.

    Creating a new physical connection will create a new spid, which will show up as a new row in sys.sysprocesses and sys.dm_exec_connections.

    Reusing an existing pooled physical connection will just reuse an existing spid so you SqlConnection.Open() will not make any visible change in those tables on the server side. However it can be detected by using SQL Profiler or XEvent by looking for sp_reset_connection, which is a stored procedure called by SqlClient that tells the server to clear the connection state (e.g. make sure there is no transaction, etc).

    SqlConnection.Close() will usually return the physical connection to the pool, so it will not disappear from the server. Physical connections are actually closed in various ways under the hood, such as by being killed by the server such as kill @spid and SqlConnection.ClearAllPools().

    Hope that is enough detail, is there anything else you'd like to know?

    0 讨论(0)
提交回复
热议问题