Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how long must a sql server connection be idle before it is closed by the connection pool?

I have a client-server app that uses .NET SqlClient Data Provider to connect to sql server - pretty standard stuff. By default how long must connections be idle before the connection pooling manager will close the database connection and remove it from the pool? What setting if any controls this?

This MSDN document only says

The connection pooler removes a connection from the pool after it has been idle for a long time, or if the pooler detects that the connection with the server has been severed.

like image 616
Rory Avatar asked Jul 16 '09 10:07

Rory


People also ask

What is idle connection in pool?

A colleague at work insists that a DBCP idle connection is a connection that has lain unused for 30 minutes. I believe a dbcp idle connection is a connection that is in the pool available to be borrowed, and an active connection is one that is borrowed.

When should a database connection be closed?

If you have a valid reason to keep it open, then do so. If not, close it as soon as you're done with it. But it's better to be in the good habit of closing the connections so that you don't just leave them open when you don't intend to.

What is idle session in SQL Server?

An idle session with an open transaction is a session that has no request currently running, is not active, but contains uncommitted work. Such transactions can increase the possibility of blocking and performance degradation.

How do I close a connection pool in SQL Server?

You can do this using either the Close or Dispose methods of the Connection object, or by opening all connections inside a using statement in C#, or a Using statement in Visual Basic. Connections that are not explicitly closed might not be added or returned to the pool.


1 Answers

A few years ago the answer beneath was the situation, but now it's changed so you can refer to the source and write up a summary :)


Old answer

This excellent article tells us what we need to know, using reflection to reveal the inner workings of connection pooling.

From how I understand it, 'closed' connections are cleaned up periodically on a semi-random interval. The cleanup process runs somewhere between every 2min and 3min 50s, but it needs to run twice before a 'closed' connection will be properly closed. Therefore after 7min 40s of being 'closed' the underlying sql connection should be properly closed, but it could be as short as 2min. At the time of writing the first connection pool created in a process would always have a timer interval of 3min 10s, so you'd normally see sql connections being closed somewhere between 3min 10s and 6min 20s after you call Close() on the ADO object.

Obviously this uses undocumented code so could change in future - or could even have changed since that article was written.

like image 115
Rory Avatar answered Sep 29 '22 07:09

Rory