views:

27

answers:

1

I have an application which uses SqlConnection.ClearAllPools to close all connections before dropping a database.

There is a case where a connection is still there. This connection had been created in another application domain.

So I wonder which connections are closed by SqlConnection.ClearAllPools?

  • Only the connections opened by the calling process (or AppDomain)?
  • All the connections opened by this machine?
  • ...?
+2  A: 

It closes all the connections opened by the calling process only. It empties all the connection pools which are bound to the process. Quote:

Connection pool and connection string go hand in hand. Every connection pool is associated with a distinct connection string and that too, it is specific to the application. In turn, what it means is – a separate connection pool is maintained for every distinct process, app domain and connection string.

Darin Dimitrov
Thanks. The documentation is not clear about it at all. I know there is a pool for each configuration and each application. But ClearAllPools is plural and it clears several pools. But which ones? It can't rely on the configuration, because it is a static method without any arguments. But does it rely on the process / app domain?
Stefan Steinegger
@Stefan, yes the documentation is not clear about this method. In fact there could be multiple pools per application domain if it uses different connection strings. Once you pass a connection string to an `SqlConnection` inside an application domain this connection will either be drawn from the pool associated to this connection pool or a new pool will be created if it doesn't exist. That's why there's also `SqlConnection.ClearPool` method which allows you to clear the pool associated to a given connection only. So basically the pool is per connection string/per app domain.
Darin Dimitrov
Sure, that's the part I could find in the documentation. But I couldn't derive the "scope" of `ClearAllPools` from this.
Stefan Steinegger