runtime icon indicating copy to clipboard operation
runtime copied to clipboard

DbDataReader.Close() doesn't unify with System.Data.Common

Open bricelam opened this issue 11 years ago • 28 comments

If I write a provider against System.Data.Common on .NET Standard 1.2 and implement Dispose appropriately, then I try to use that provider on .NET Framework 4.6 or .NET Core 2.0, the following code won't work.

reader.Close(); // No-op
Assert.True(reader.IsClosed); // Fails since Dispose is never called

It fails because on .NET Framework and .NET Core 2.0, Dispose calls Close (not the other way around) and since there is no virtual Close method on .NET Standard 1.2, I cannot override it without cross-targeting the frameworks that do have it.

bricelam avatar Jul 06 '15 15:07 bricelam

/cc @YoungGah @saurabh500

joshfree avatar Oct 12 '15 17:10 joshfree

@bricelam: It is not ideal that Dispose calls Close but unfortunately that is how Dispose is implemented on NET Framework. As for Close being removed from DbDataReader, we carefully reviewed all Close methods under System.Data.Common as Close is a synonym for Dispose. We concluded that Close on DbDataReader is redundant and does not provide any additional value unlike Close method on DbConnection. Close method on DbConnection is appropriate as DBConnection has Open method.

This issue is not ideal, but given that the recommendation is for users to call Dispose method by "using" statement with DbDataReader, we won't be fixing this issue.

YoungGah avatar Oct 28 '15 23:10 YoungGah

I'm re-opening this because it is a real issue for provider writers that prevents them from (responsibly) creating a provider that just targets .NET Standard 1.2 (without cross-targeting .NET Framework 4.5.1).

IMHO, the solution here is to add Close() back to DbDataReader in .NET Standard 1.2 (as we're doing in .NET Standard 2.0)

cc @divega

bricelam avatar Feb 09 '17 21:02 bricelam

Maybe even put [EditorBrowsable(EditorBrowsableState.Never)] on it so users won't call it, but provider writers can provide a correct implementation.

bricelam avatar Feb 09 '17 21:02 bricelam

This issue now appears on netstandard2.0 and netcoreapp2.0 (in addition to net4x). Because DbDataReader.Close() has been added back, you can write the code above, but if a netstandard1.x provider hasn't worked around it by corss-targeting net4x and netstandard2.0 now, calling Close() is a no-op.

bricelam avatar Mar 30 '17 18:03 bricelam

I have looked at SqlDataReader, DataTableReader and OdbcDataReader and they all have Close() implemented and Dispose is calling into Close()

For DbDataReader there is not logic in Close() on Desktop which I believe is expected since this is abstract class.

Can this be closed or am I missing something?

krwq avatar Apr 15 '17 00:04 krwq

This is an issue in the .NET Standard 1.2 System.Data.Common contract that has since been fixed in 2.0. This issue is tracking whether we should backport the fix (add a virtual Close method to DbDataReader) into the .NET Standard 1.2 System.Data.Common contract. @divega, @terrajobst, @ericstj, et al. are currently investigating whether this is feasible.

bricelam avatar Apr 15 '17 19:04 bricelam

(I've updated the main description to better articulate the issue.)

bricelam avatar Apr 15 '17 19:04 bricelam

I've assigned them to this task since they are working on it

krwq avatar Apr 17 '17 19:04 krwq

/cc @weshaggard

ericstj avatar Apr 18 '17 00:04 ericstj

I would imagine that is possible if you rebuild the ns1.2 ref and ensure that all implementations have the Close method (including those where it is inbox like xamarin). It doesn't appear to be inbox anywhere else (Desktop, .NET Core, UAP) so it seems feasible.

This is actually the first I've heard of this, but I think the right course of action would be for the folks who own Data.Common to propose a solution, test that on all frameworks, then take the change to FxDC if it involves any API changes.

ericstj avatar Apr 19 '17 00:04 ericstj

@bricelam @karelz this is neither a netcoreapp nor a 2.0 issue but affects it. I suggest moving it to netstandard and mark it as critical production issue.

ViktorHofer avatar May 06 '17 14:05 ViktorHofer

@ViktorHofer this isn't something that would be fixed in the standard repo. If we were to take a fix for this, which I'm not convinced we should, then it would need to happen in the corefx repo.

weshaggard avatar May 08 '17 16:05 weshaggard

I see, thanks for enlighten me

ViktorHofer avatar May 08 '17 16:05 ViktorHofer

As of tomorrows ZBB, we need to make the decision of either ignoring the issue for 2.0 or fixing it. @danmosemsft @weshaggard @karelz

ViktorHofer avatar May 09 '17 21:05 ViktorHofer

@saurabh500 @corivera @ericstj can you please chime in?

karelz avatar May 09 '17 21:05 karelz

We've already added the Close() API in 2.0.0, so this just seems like a 1.2 servicing issue.

corivera avatar May 09 '17 23:05 corivera

Which servicing branch does it live in? 1.0.x or 1.1.x - can you please set the milestone to match that?

karelz avatar May 09 '17 23:05 karelz

According to @weshaggard we would have to make changes in 1.1.0

corivera avatar May 09 '17 23:05 corivera

Actually, looks like DbDataReader.Close() was already added to 1.1.0 in this commit: https://github.com/dotnet/corefx/commit/27240e90597d477c213b0af81142e6b53b6ea315#diff-7ce02b6f11b3db97a63d932124bd3cdaR288

corivera avatar May 10 '17 00:05 corivera