[Mono-list] Npgsql error

Jaroslaw Kowalski jaak at jkowalski.net
Fri Aug 5 00:06:02 EDT 2005


> Cool, a question on that:
> What happens if a exception occurs?
> I meand close() gets called at the end of the using block, but does it get 
> also called when an exception occurs (sort of finally)?
> If yes, I'm changing my programming style :)
> Another question, how would it look like if you had to catch an exception 
> (i.e. to rollback a transaction)?

The following code:

====
using (A a = new A())
{
    code
};
====

Is equivalent to:

=======
A a = null;

try
{
    a = new A();
    /// code
}
finally
{
    if (a != null) ((IDisposable)a).Dispose();
}
=======

It's the C# compiler that translates every using() to something like the 
above code. The IDisposable.Dispose method on data providers automatically 
rolls back any open and uncommitted transactions so you don't need to do 
explicit rollbacks.

BTW. I've seen many (MS and other) examples where they don't call Dispose 
the IDbCommand object, even though it implements IDisposable, I'm not sure 
whether it leads to potential resource leaks or not.

-- 
Jaroslaw Kowalski
http://blog.jkowalski.net/



More information about the Mono-list mailing list