Skip to content

NullReferenceException on Npgsql.NpgsqlConnection.Close (trunk) #2127

@monty241

Description

@monty241

During debugging another project, the Close of NpgsqlConnection raised a NullReferenceException.

Debugger halted at the line with // *** since Connector variable was null:

else  // Non-pooled connection
{
    if (EnlistedTransaction == null)
        Connector.Close();
    // If a non-pooled connection is being closed but is enlisted in an ongoing
    // TransactionScope, simply detach the connector from the connection and leave
    // it open. It will be closed when the TransactionScope is disposed.
    Connector.Connection = null; // *** CONNECTOR == NULL
    EnlistedTransaction = null;
}

Suggested change:

else  // Non-pooled connection
{
    if (Connector != null)
    {
        if (EnlistedTransaction == null)
            Connector.Close();
        // If a non-pooled connection is being closed but is enlisted in an ongoing
        // TransactionScope, simply detach the connector from the connection and leave
        // it open. It will be closed when the TransactionScope is disposed.
        Connector.Connection = null; // *** CONNECTOR == NULL
    }
    EnlistedTransaction = null;
}

Details:

  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=Npgsql
  StackTrace:
   at Npgsql.NpgsqlConnection.Close(Boolean wasBroken) 
     in C:\projects\npgsql\src\Npgsql\NpgsqlConnection.cs:line 644

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions