views:

729

answers:

3

Hello everyone,

I am using SQL Server 2008 Enterprise + .Net 3.5 + C# + ADO.Net. I am using the following SQL statement to monitor connection number, is it correct? If yes, my confusion is, one connection from ADO.Net client maps to only one connection in the following statement? Or one ADO.Net connection could maps to multiple connections here?

SELECT  *  FROM sys.dm_os_performance_counters WHERE object_name = 'SQLServer:General Statistics'

(Monitor User Connections row)

thanks in advance, George

+1  A: 

Would this work for your needs? I'm confused if you're trying to count the number of connections. Your question seems to say no, where your comment implies yes to me.

Sp_who2 'Active'
Thanks! The sp_who2 command works cool! One more question, if I wrap each ADO.Net connection object into using block, then the connection number should not increase?
George2
+1  A: 

By default, the underlying SQL Server driver code uses a connection pool. You'll find that the number of physical connections "owned" by your application will grow over time to the current limit, but this is different from the number that are "in use".

This avoids renegotiating security, etc. on each link, speeding up your application's database access.

As mentioned by @sgmarshall, use the sp_who2 stored procedure to determine what each of the connections are currently doing.

devstuff
"the underlying SQL Server driver code uses a connection pool" -- at client side or server side?
George2
I'm pretty sure most of the support is on the client side (you can turn pooling off in the connection string), but the server would have a least-recently-used purging method when it hits its limits.
devstuff
I want to know whether there is any upper bound limitation at SQL Server side for the max # of client connection for a specific database? For a specific instance?
George2
I couldn't find any info on upper limits, but if your application is returning connections to the pool it shouldn't be an issue. I worked on a Java-based web application that regularly had ~300 connections (per web server), in an 8-way farm, and SQL server was quite happy with that.
devstuff
+2  A: 

Use SELECT * FROM sys.dm_exec_connections to find all the connections. The client_net_address has the client address so you can track down the origin of connections.

Use SELECT * FROM sys.dm_exec_sessions to find all the sessions (sessions in general map 1 to 1 with connections unless MARS is used). The program_name column will contain the value of the application name you passed in in the connection string and allows you to identify your own connections.

Use SELECT * FROM sys.dm_exec_requests to see all the current executing batches (requests).

The performance counter would only give you one value, the number of current connections:

SELECT  cntr_value
FROM sys.dm_os_performance_counters 
WHERE object_name = 'SQLServer:General Statistics'
  and counter_name = 'User Connections'
Remus Rusanu
BTW: note that the `SQLServer:` prefix in the `object_name` is specific to the server instance name; e.g. for a `.\SQLEXPRESS` instance the prefix is `MSSQL$SQLEXPRESS:`.
devstuff