views:

49

answers:

2

I would like to close all idle connections in the SqlClient connection pool. Is there a documented method for doing this?

The issue is that as part of the normal processing of this application (A testing harness) I need to drop the database and recreate it at the start of each test scenerio. Obviously I can't drop the database with active connections (active from Sql Server's point of view not the client)

+1  A: 

In general, no. Specific database providers may provide a way of doing this. SqlClient provides a similar feature via SqlConnection.ClearPool and SqlConnection.ClearAllPools -- but these do not close any open connections, they just clear out connections that have been returned to the pool.

itowlson
That's exactly what I wanted --- it's the idle connections still open in the pool that was causing the bother. Thanks!
Ralph Shillington
A: 

There is no real reason do to this. If you have a need to do this, it probably indicates that you have a leak somewhere else in the application and should try to find out where you aren't disposing of your connections properly.

FWIW, I don't believe there is a way to do this for all providers consistently, but based on what I mentioned above, there should be no need to do this.

casperOne
You're right in nearly all situations, the connection pool can be left alone, however my issue is an edge case in that I need to be able as part of a long running test, I need to be able to drop the database and recreate it. Can't do that with active connections.
Ralph Shillington
@Ralph interesting point about droping a DB, you should add this to your question.
Hassan Syed