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

╄→尐↘猪︶ㄣ 提交于 2019-12-01 22:14:32

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?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!