Number of open connections SQL server

Hi all,

Normally, when you’re coding on a website or a system you’re probably using connection to an SQL server database.

When you are the coder or you are a consultant coming to a company to check the code or the server or anything else for that matter, you sometimes need to check the number of connections that are open right now.

This can be done to check the quality of the code regarding connections. If the code does not close the connections, eventually the server will close the pool and the website will no longer work. I have seen people solving that by upping the number of allowed open connections. That of course is not the solution (not the best solution).

OK, so if you are like me and you are running 1-9 servers that are dedicated to you, each running dozens or hundreds of DB’s, this is an absolutely great method to catch a glimpse on whats going on with your connections.

Run this code on your server and see what happens:

REXML could not parse this XML/HTML: 
 1: SELECT DB_NAME(dbid) as 'DbNAme', COUNT(dbid) as 'Connections' from master.dbo.sysprocesses with (nolock)
 2: WHERE dbid > 0
 3: GROUP BY dbid
REXML could not parse this XML/HTML: 
REXML could not parse this XML/HTML: 

This code will display a list of all the databases on your server with the open connections on each of them.

For security reasons I can’t show the entire result set, but here’s a sneak peak:

6-22-2009 6-51-36 PM

** This was checked on SQL server 2005/2008 both express and full versions.

Good luck!