Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add creation and disposal to connection interception #27920

Merged
merged 11 commits into from
May 4, 2022

Conversation

ajcvickers
Copy link
Member

Fixes #23087.

Part of #626.

@ajcvickers ajcvickers requested a review from a team April 30, 2022 19:45
<comment>Debug RelationalEventId.ConnectionCreated int</comment>
</data>
<data name="LogConnectionCreating" xml:space="preserve">
<value>Creating DbConnection.</value>
Copy link
Member

Choose a reason for hiding this comment

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

Should we add '{database}' and '{server}' to these too?

Copy link
Member Author

Choose a reason for hiding this comment

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

We could, but sometimes we won't know because we don't have a connection string. We could have two different messages. Or we could say it's okay to not include it here and look at the open events for this when it is always known and correct.

Copy link
Member Author

Choose a reason for hiding this comment

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

Leaving as is.

@ajcvickers ajcvickers merged commit 7ad294c into main May 4, 2022
@ajcvickers ajcvickers deleted the TheRainbowConnection0426 branch May 4, 2022 16:30
Assert.False(interceptor.DisposedCalled);

var connection = context.Database.GetDbConnection();
connection.Disposed += (_, __) => connectionDisposed = true;
Copy link
Member

Choose a reason for hiding this comment

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

@ajcvickers FYI in Npgsql we don't fire this event, for better perf (and because it's mostly an artifact of DbConnection extending Component, and is ridiculous!). Nobody's ever complained until now 😉

Filed npgsql/efcore.pg#2368 to see what to do (someday:tm:)

Copy link
Contributor

Choose a reason for hiding this comment

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

FYI, this also broke the Pomelo/MySqlConnector tests: PomeloFoundation/Pomelo.EntityFrameworkCore.MySql#1718

Is depending on Component.Disposed really the right choice here?

Copy link
Contributor

Choose a reason for hiding this comment

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

Arguably, this is a regression in providers that override DisposeAsync; see https://mysql-net.github.io/AdoNetResults/#Dispose_raises_Disposed.

Because of https://github.com/dotnet/runtime/blob/v6.0.10/src/libraries/System.Data.Common/src/System/Data/Common/DbConnection.cs#L105-L109, the base DbConnection implementation delegates to Component.Dispose, which calls Component.Dispose(true), which raises the event. Once DisposeAsync (which is introduced at DbConnection) is overridden, this default behaviour no longer occurs.

If Component followed the DisposeAsync pattern (which I'm sure it can't, due to significant ripple effects throughout the ecosystem), then it could take care of raising the event in both sync and async code paths. Since it doesn't... maybe providers should call down to base.Dispose(true) in their DisposeAsync implementation to run the (sync parts of the) base class's Dispose?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add ConnectionCreating/ConnectionCreated to DbConnectionInterceptor
4 participants