Monday 18 November 2013

Number of open connections in SQL server

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:
 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

This code will display a list of all the databases on your server with the open connections on each of them.
  If you want details you can run  exec sp_who2 'Active' to get more details.

No comments:

Post a Comment

First Database In Sql Server