开发者

More than 100 connections to sql server 2008 in "sleeping" status

开发者 https://www.devze.com 2023-04-07 12:59 出处:网络
I have a big trouble here, well at my server. I have an ASP .net web (framework 4.x) running on my server, all the transactions/select/update/insert are made with ADO.NET.

I have a big trouble here, well at my server.

I have an ASP .net web (framework 4.x) running on my server, all the transactions/select/update/insert are made with ADO.NET.

The problem is that after being using for a while (a couple of updates/selects/inserts) sometimes I got more than 100 connections on "sleeping" status when check for the connections on sql server with this query:

SELECT 
 spid,
 a.status,
 hostname,  
 program_name,
 cmd,
 cpu,
  physical_io,
  blocked,
  b.name,
  loginame
FROM   
  master.dbo.sysprocesses  a INNER JOIN
  master.dbo.sysdatabases b开发者_如何学JAVA  ON
    a.dbid = b.dbid where program_name like '%TMS%'
ORDER BY spid 

I've been checking my code and closing every time I make a connection, I'm gonna test the new class, but I'm afraid the problem doesn't be fixed.

It suppose that the connection pooling, keep the connections to re-use them, but until I see don't re-use them always.

Any idea besides check for close all the connections open after use them?

SOLVED(now I have just one and beautiful connection on "sleeping" status):

Besides the anwser of David Stratton, I would like to share this link that help explain really well how the connection pool it works: http://dinesql.blogspot.com/2010/07/sql-server-sleeping-status-and.html

Just to be short, you need to close every connection (sql connection objects) in order that the connection pool can re-use the connection and use the same connectinos string, to ensure this is highly recommended use one of the webConfig.

Be careful with dataReaders you should close its connection to (that was what make me mad for while).


It sounds like it is connection pooling.

From here: http://msdn.microsoft.com/en-us/library/8xx3tyca.aspx

A connection pool is created for each unique connection string. When a pool is created, multiple connection objects are created and added to the pool so that the minimum pool size requirement is satisfied. Connections are added to the pool as needed, up to the maximum pool size specified (100 is the default). Connections are released back into the pool when they are closed or disposed.

To ensure you're not creating unnecessary pools, ensure that the exact same connection string is used each time you connect - store it in the .config file.

You can also reduce the Maximum Pool Size if you like.

Actually, I'd recommend just reading the entire article linked to above. It talks about clearing the pools, and gives you the best practices for using pooling properly.

Edit - added the next day

The pools on your server are there because of how Connection pooling works. Per the documentation linked to above:

The connection pooler removes a connection from the pool after it has been idle for a long time, or if the pooler detects that the connection with the server has been severed. Note that a severed connection can be detected only after attempting to communicate with the server. If a connection is found that is no longer connected to the server, it is marked as invalid. Invalid connections are removed from the connection pool only when they are closed or reclaimed.

This means that the server itself will clean up those pools eventually, if they remain unused. If the are NOT cleaned up,l that means that the server believes that the connections are still in use, and is hanging on to them to increase your performance.

In other words, I wouldn't worry about it unless you see a problem. Connection Pooling is happening exactly as it should be.

If you REALLY want to clear the pools, again, per the documentation:

Clearing the Pool

ADO.NET 2.0 introduced two new methods to clear the pool: ClearAllPools and ClearPool. ClearAllPools clears the connection pools for a given provider, and ClearPool clears the connection pool that is associated with a specific connection. If there are connections being used at the time of the call, they are marked appropriately. When they are closed, they are discarded instead of being returned to the pool.

However, if you want to adjust pooling, the Connection String can be modified. See this page, and search for the word "pool":

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring.aspx

Or you can enlist a DBA to assist and set pooling at the server-level. That's off-topic here, but ServerFault.com might have people to assist there.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号