Connection.Dispose and the connection pool

F

Frank Rizzo

I am researching some errors with "The timeout period elapsed prior to
obtaining a connection from the pool. This may have occurred because
all pooled connections were in use and max pool size was reached."
message. I am assuming that the connection pool ran out of connections
(though it could certainly be something else).

So I've looked through the code and it does close Connections (in the
finally clause), but doesn't .Dispose of them. Do I have to dispose of
the connections? Do connections not get released unless you dispose of
them?

Thanks
 
N

Nicholas Paldino [.NET/C# MVP]

Frank,

For most connections, Close and Dispose are the same thing. The reason
I say most is that the base class for connections, DbConnection (or
DbDataConnection, I forget which one it is), the Close method and Dispose
method pretty much share the same code path. However, certain providers can
override that code path.

I would advise not doing it yourself in the finally statement, rather,
just wrapping everything in a using statement.
 
F

Frank Rizzo

Nicholas said:
Frank,

For most connections, Close and Dispose are the same thing. The reason
I say most is that the base class for connections, DbConnection (or
DbDataConnection, I forget which one it is), the Close method and Dispose
method pretty much share the same code path. However, certain providers can
override that code path.

I would advise not doing it yourself in the finally statement, rather,
just wrapping everything in a using statement.

However, per your statement, wouldn't this cause a problem, if I were to
use another provider? Since using only calls .Dispose, but not .Close?

Also, why would you advise using the 'using' statement, rather than
manually closing/disposing connections in the finally section? Other
than code clarity, are there any downsides?

Regards
 
N

Nicholas Paldino [.NET/C# MVP]

Frank,

I wasn't saying that you should use another provider. I'm saying that
some providers can override the behavior that I mentioned, but for the most
part, they do not.

As for the using statement, you are right, I suggest it for the sake of
code clarity.
 
A

Andy

Besides clarity, a using statement guarantees that Dispose is called no
matter how you're exiting the block (ie, even if there's an exception).
If you're already calling dispose in a finally block, there will be
no difference except that the code is cleaner.

Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top