Is there a way to close all connections in the pool for the SqlClient provider
I would like to close all idle connections in the SqlClient connec开发者_高级运维tion 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)
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.
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.
精彩评论