[Mono-devel-list] Npgsql possible bug

Jaroslaw Kowalski jaak at zd.com.pl
Fri Dec 26 07:17:45 EST 2003


> Yes, that code shouldn't be there but when we use NpgsqlDataAdapter with
> DataSet, DataTable, etc... the connection is not closed:
>
> LOG:  pq_recvbuf: unexpected EOF on client connection
>
> I think that when a command is disposed, it's underlying connection
> shouldn't be disposed too but the physical connection can be closed and
> the connection changes its inner state. In this way the connection can
> be reopenend...

I'm not sure about that. I think that the only semantics
IDbCommand.Dispose() has is: "release any database resources related to the
command (un-prepare it)". In general there can be open transactions or an
open datareader, so you cannot close the database connection.

In some book I've seen a pattern like:

using (IDbConnection c = ...)
{
    IDbTransaction tran = c.BeginTransaction();

    using (IDbCommand cmd = c.CreateCommand())
    {
        cmd.Transaction = tran;
        using (IDataReader reader = cmd.ExecuteReader())
        {
        }
    }
    using (IDbCommand cmd = c.CreateCommand())
    {
        cmd.Transaction = tran;
        using (IDataReader reader = cmd.ExecuteReader())
        {
        }
    }
    tran.Close();
}

This would obviously fail with IDbCommand.Dispose() calling
IDbConnection.Dispose().

P.S. If you want to make sure the connection gets closed after the whole
IDataReader has been processed you should use
"CommandBehavior.CloseConnection" on ExecuteReader(). This is implemented in
NpgsqlDataReader.

Jarek




More information about the Mono-devel-list mailing list