Skip to content

Dispose DbCommand in DbMetaDataFactory.ExecuteCommand#103593

Closed
omajid wants to merge 1 commit into
dotnet:mainfrom
omajid:dispose-command-dbmetadatafactory
Closed

Dispose DbCommand in DbMetaDataFactory.ExecuteCommand#103593
omajid wants to merge 1 commit into
dotnet:mainfrom
omajid:dispose-command-dbmetadatafactory

Conversation

@omajid

@omajid omajid commented Jun 17, 2024

Copy link
Copy Markdown
Member

A static analysis tool has flagged that DbCommand is an IDisposable, so it should be Disposed() to free up resources.

@ghost ghost added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Jun 17, 2024
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Jun 17, 2024
throw ADP.TooManyRestrictions(collectionName);
}

command = connection.CreateCommand();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move the declaration of command together with assignment:

using DbCommand command = connection.CreateCommand();

@huoyaoyuan huoyaoyuan added area-System.Data and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Jun 19, 2024
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @roji, @ajcvickers
See info in area-owners.md if you want to be subscribed.

A static analysis tool has flagged that DbCommand is an IDisposable, so
it should be Disposed() to free up resources.
@roji

roji commented Aug 29, 2024

Copy link
Copy Markdown
Member

We discussed this in the data team triage, and we don't think we should do this.

DbCommand indeed implements IDisposable, since for legacy reasons it extends Component. Now, in System.Data there is a long tradition of DbCommand not actually requiring disposal; EF6 did not dispose DbCommands (though EF Core does, IIRC), and ADO.NET providers generally take to not require DbCommand to be disposed (both the Npgsql and Firebird had to be modified to not leak resources via undisposed commands).

Now, that in itself isn't a reason to not dispose commands, and both users and ORMs do so. However, this PR specifically changes a piece of very old code within System.Data, which hasn't been touched in a long time, and the bar for touching it is generally very high. Because of this, we'd rather prioritize stability here and keep the code as-is, unless a demonstrated problem is reported on this.

So I'm going to go ahead and close it for now.

@roji roji closed this Aug 29, 2024
@omajid

omajid commented Aug 29, 2024

Copy link
Copy Markdown
Member Author

Thanks for the explanation. Your decision makes sense to me.

@github-actions github-actions Bot locked and limited conversation to collaborators Oct 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-System.Data community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants