10

My web application is in asp.net 2.0,c#2.0 and sql server 208 how can i find number of open connections on my sql server 2008 database.and is there any way to clear connection pool.because my site is hosted on shared hosting and they have provided limited connections. In my codding i have closed all the connection after use, but still i am getting warning for suspending database.

Can any one tell me how to find number open connections on database and and how to clear connection pool.

i used using statements for connections and closed all connections after used in finally block. so though there is error it closes oped connections.

Thanks in advance.

R.D.
  • 7,153
  • 7
  • 22
  • 26
  • 1
    What RDBMS? AFAIK, the only way to do this is by querying the database service directly and this differs widely for each flavor. – lc. Aug 09 '12 at 03:07
  • If you're just debugging, try `sp_who2` – Andre Calil Aug 09 '12 at 03:20
  • sp_who2 gives details about login user,but as its a web application same user can open more connections on database.i want number of opened connections on database using my web application and it is asp.net. – R.D. Aug 09 '12 at 03:36
  • @R.D. - in your web app's connection string, add an "Application Name" part. e.g. "YourConnectionString;Application Name=YourAppName" - replace YourAppName with some unique name. Then when you call sp_who2, check where "ProgramName" column equals YourAppName to find connections which were only opened by your app. – Moe Sisko Aug 09 '12 at 05:56

3 Answers3

13

This shows the number of connections per each DB:

SELECT 
    DB_NAME(dbid) as DBName, 
    COUNT(dbid) as NoOfConnections,
    loginame as LoginName
FROM
    sys.sysprocesses
WHERE 
    dbid > 0
GROUP BY 
    dbid, loginame

And this gives total connections:

SELECT 
    COUNT(dbid) as TotalConnections
FROM
    sys.sysprocesses
WHERE 
    dbid > 0

From c#, you can follow :
http://www.c-sharpcorner.com/UploadFile/dsdaf/ConnPooling07262006093645AM/ConnPooling.aspx Another good reference can be found at :
http://www.wduffy.co.uk/blog/monitoring-database-connections/

Call the static method ReleaseObjectPool on the the OleDbConnection - see http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbconnection.releaseobjectpool.aspx

NG.
  • 5,695
  • 2
  • 19
  • 30
  • Hi Nitin, thanks for reply, but SELECT COUNT(dbid) as TotalConnections FROM sys.sysprocesses WHERE dbid > 0 returns one but there are 25 connections opened on database. SELECT DB_NAME(dbid) as DBName, COUNT(dbid) as NoOfConnections, loginame as LoginName FROM sys.sysprocesses WHERE dbid > 0 GROUP BY dbid, loginame and this also gives opened connections as 1 – R.D. Aug 09 '12 at 05:39
  • 1
    @R.D. Try to log in as SysAdmin – Zsmaster Oct 03 '17 at 05:40
2

Sql query to get the current active connection

SELECT DB_NAME(dbid) as 'DbNAme', COUNT(dbid) as 'Connections' from master.dbo.sysprocesses with (nolock) WHERE dbid > 0 GROUP BY dbid

you can define dbid if you want connection specific to database

Buzz
  • 6,030
  • 4
  • 33
  • 47
1

You might want to read up on connection pooling: http://msdn.microsoft.com/en-us/library/8xx3tyca.aspx

A separate connection pool is created for each distinct connect string. Further, if you are connecting via integraged security and your web site is using Basic or Windows authentication (rather than anonymous), a separate connection pool will be created for each user of the web site.

To clear connection pools, the SqlConnection object provides the methods ClearPool() and ClearAllPool()`. However, an individual connection won't be closed and removed from the pool until it is closed or disposed.

All the various objects involved in the execution of the sql query that implement IDisposable should be wrapped in a using statement to guaranteed proper disposal. Something along these lines:

IEnumerable<BusinessObject> list = new List<BusinessObject>() ;

using ( SqlConnection  connection = new SqlConnection( credentials ) )
using ( SqlCommand     command    = connection.CreateCommand() )
using ( SqlDataAdapter adapter    = new SqlDataAdapter( command ) )
using ( DataSet        results    = new DataSet() )
{

  command.CommandType = CommandType.StoredProcedure ;
  command.CommandText = @"someStoredProcedure" ;

  try
  {
    connection.Open() ;
    adapter.Fill( results ) ;
    connection.Close() ;

    list = TransformResults( results ) ;

  }
  catch
  {
    command.Cancel() ;
    throw
  }

}

return list ;

You can examine what SPIDs are open in Sql Server either by executing the stored procedure sp_who (must have the appropriate admin permissions in the SQL Server). You can also use perfmon.

Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
  • hi Nicholas, thanks for the reply. Yes, i used using statements for connection,command and also closed and disposed connections in finally block, i have also allowed min pool size as 0 and max pool size as 10 but still there are more than 200 connections opened on database and hosting people are complaining about this. Can you please tell me what will happened if i use SqlConnection.ClearAllPool() and where this to be used. – R.D. Aug 09 '12 at 04:20