views:

51

answers:

2

Hello friends,

I have a website with pages having GridView in it. I am using SQL Data Source control to bind with the GridView. After running the site i am checking the no of active connections with SQL Server using -

SELECT db_name(dbid) as DatabaseName, count(dbid) as NoOfConnections, loginame as LoginName FROM sys.sysprocesses WHERE dbid > 0 GROUP BY dbid, loginame

I found that even after exiting my web application the connections remains open. So is there any way to close these connections. This is very important for me because as the number of users increases the no. of active connections are also increasing causing the sql server max pool issue. Although i have increased the user connections on sql server but i need the solution to explicitly close those connections as soon as user logs off.

Thanks for sharing your valuable time.

+1  A: 

Have you tried calling the datasource's .Dispose() after you have finished with it?

Personally, in situations where the number of connections to a SQL Server need to be managed, I would stay away from the SqlDataSource. I instead would perform all operations in the code behind, wrapped in a 'Using' block. Especially when dealing with opening and closing connections (it is very easy to forget to close 1 connection when multiple are open).

'Using' provides a shortcut to the Dispose pattern - Using

Mick Walker
Thanks Mick! I could do that, but isn't there a way to close these connections?
IrfanRaza
Not explicitly, - there will always be conditions where connections will not be closed when using a SqlDataSource (for example the user could simply close the browser).
Mick Walker
As treaschf has said, you could turn connection pooling off in your web.config file; however as with the Max Pool Size (limiting the number of connections) I really wouldn't recommend you play with it unless you know what you are doing, especially in a web application, as a wrong setting will result in clients having to wait until a connection is dropped before they can see any data driven page - which will more than likely result in a time out.
Mick Walker
Thank you again Mick for having a nice explanation. So in your opinion there is no explicit way....
IrfanRaza
The only thing you can explicitly assume is users will never act the way you wish them to. So it is always better not to rely on the users.
Mick Walker
+2  A: 

Your connections remain "open" because the use of connection pooling. Turn off connection pooling, if that would be the desired behaviour, or limit the number of connections which can be opened at a time. These can be set from the connection string.

treaschf
Thanks buddy! Have u personally checked that? Because after setting pool off does not solve my problem.
IrfanRaza
I did not. However I'm sure, that the SqlDataSource closes the connection after using it, latest when your ASP.Net page gets unloaded. I never had problems with it.However closing the connection does not actually closes it, when connection pooling is used. It just puts it back into the pool, to reuse it next time, when a connection is needed.
treaschf