DonBoitnott
DonBoitnott

Reputation: 11025

Is there a way to clear the connection pool for DbConnection?

When I use System.Data.SqlClient.SqlConnection, there is a static method available:

System.Data.SqlClient.SqlConnection.ClearPool(connection);

Even OleDbConnection has (or had) a method: ReleaseObjectPool(), that seemed to do about the same thing.

But I am using the base DbConnection and I cannot find an equivalent way to free connections/resources. Is there a way?

Upvotes: 1

Views: 9434

Answers (1)

Jeroen Mostert
Jeroen Mostert

Reputation: 28759

There is no provider-agnostic connection pooling, although the ADO.NET providers supplied with the .NET platform itself do use shared code for it internal to System.Data (DbConnectionPool). Given a DbConnection, you have no way of knowing whether it comes from a pool at all. ADO.NET providers aren't required to pool connections, though of course most do for performance reasons. So the literal answer to your question is "no, you can't clear the pool for any given DbConnection, since it's not required to come from a pool".

You can, of course, test for the specific type of your DbConnection and invoke the provider-specific method, if there is one. Since you already know the provider you're having a problem with (Pervasive.SQL) and you know you have a provider-specific problem that requires (or seems to require) clearing the pool (which is definitely not something you want to do in general) this requirement to test the type doesn't seem too onerous.

If your client code already uses a generic DbConnection (or better yet IDbConnection) you could write wrappers that return connections with the required behavior, but that's potentially quite a bit of work due to the hierarchies involved (you may end up having to wrap things like IDbCommand as well just to make Command.Connection = myConnection work, that sort of thing). Just separating your data layer is probably easier.

Upvotes: 2

Related Questions