NpgsqlParameterCollection.Clear() doesn't reset NpgsqlParameter.Collection #231

Closed
FransBouma opened this Issue May 7, 2014 · 3 comments

Projects

None yet

2 participants

@FransBouma
Contributor

See also the dev mailing list.

var p = new NpgsqlParameter();
var c1 = new NpgsqlCommand();
var c2 = new NpgsqlCommand();
c1.Parameters.Add(p);
c1.Parameters.Clear();
c2.Parameters.Add(p); // <<<<<<<< crash

This is because NpgsqlParameterCollection.Clear() doesn't reset parameter.Collection on the parameters it removes:

    /// <summary>
    /// Removes all items from the collection.
    /// </summary>
    public override void Clear()
    {
        NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Clear");
        this.InternalList.Clear();
        this.InvalidateHashLookups();
    }

This thus causes problems when parameters have to be moved to another Command because two commands are merged into 1 query.

@FransBouma
Contributor

Updated :)

@franciscojunior franciscojunior added this to the 2.2 milestone May 8, 2014
@franciscojunior franciscojunior added the bug label May 8, 2014
@franciscojunior
Member

Fixed by #234 .

@drunaar drunaar added a commit to drunaar/Npgsql that referenced this issue May 14, 2014
@FransBouma @drunaar FransBouma + drunaar Fixes for issue #231
See: npgsql#231

Fixed the issue by implementing proper clean up in the Clear() method.
Cleaned up the Remove methods in NpgsqlParameterCollection as they
re-implemented the same code 6 times (Remove and RemoveAt+overloads).
Now all end up in 1 Remove method which performs the remove and the
parameter cleanup. Easier to maintain ;)
Added a test to verify my fix. Please verify the Remove changes as I
can't run the rest of the tests.
6e1596d
@FransBouma
Contributor

2.2.3 has this problem again. See PR #234

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment