Creating database connections from an application is an expensive operation.

A lot is happening here:

  1. Establishment of a connection to the database engine
  2. Authentication and authorization
  3. Session initialization between the server and client

To mitigate this, the ADO.NET provider (Microsoft.Data.SqlClient) leverages connection pools, which can be thought of as a cache of previously established connections that are kept available for re-use.

When a connection is through with a connection and closes it, rather than destroying the connection, it is returned to the pool, and the next time a connection is requested, one of these is reused.

Connection pools are maintained per connection string and per process, so every different connection string for each application will, subject to server resources, get allocated a different connection pool.

Generally, you do not need to manage the pool yourself.

But if you need to, you can clear all the existing connection pools using the static ClearAllPools method of the SqlConnection, like so:

SqlConnection.ClearAllPools();

If you want to clear the connection pool associated with an existing connection, you do it using the static ClearPool method of the SqlConnection, like this :

SqlConnection.ClearPool(cn);

Here, cn is an existing SqlConnection.

Why would you need to clear the pool? Certain classes of errors, such as transport-level errors.

TLDR

You can clear SQL Server ADO.NET connection pools using either SqlConnection.ClearAllPools() or SqlConnection.ClearPool()

Happy hacking!