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

Reset CommandTimeout in context pooling #24936

Merged
merged 1 commit into from
May 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/EFCore.Relational/Storage/RelationalConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public abstract class RelationalConnection : IRelationalConnection, ITransaction
private bool _connectionOwned;
private int _openedCount;
private bool _openedInternally;
private int? _commandTimeout;
private int? _commandTimeout, _defaultCommandTimeout;
private readonly ConcurrentStack<Transaction> _ambientTransactions = new();
private DbConnection? _connection;
private readonly IRelationalCommandBuilder _relationalCommandBuilder;
Expand All @@ -62,7 +62,7 @@ protected RelationalConnection(RelationalConnectionDependencies dependencies)

var relationalOptions = RelationalOptionsExtension.Extract(dependencies.ContextOptions);

_commandTimeout = relationalOptions.CommandTimeout;
_defaultCommandTimeout = _commandTimeout = relationalOptions.CommandTimeout;

_connectionString = string.IsNullOrWhiteSpace(relationalOptions.ConnectionString)
? null
Expand Down Expand Up @@ -1033,6 +1033,8 @@ protected virtual void ResetState(bool disposeDbConnection)
CurrentTransaction?.Dispose();
ClearTransactions(clearAmbient: true);

_commandTimeout = _defaultCommandTimeout;

_openedCount = 0;
_openedInternally = false;

Expand Down Expand Up @@ -1063,6 +1065,8 @@ protected virtual async ValueTask ResetStateAsync(bool disposeDbConnection)

ClearTransactions(clearAmbient: true);

_commandTimeout = _defaultCommandTimeout;

if (disposeDbConnection
&& _connectionOwned
&& _connection is not null)
Expand Down
4 changes: 4 additions & 0 deletions test/EFCore.SqlServer.FunctionalTests/DbContextPoolingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -674,13 +674,16 @@ public async Task Context_configuration_is_reset(bool useInterface, bool async)
? (DbContext)scopedProvider.GetService<IPooledContext>()
: scopedProvider.GetService<PooledContext>();

Assert.Null(context1.Database.GetCommandTimeout());

context1.ChangeTracker.AutoDetectChangesEnabled = true;
context1.ChangeTracker.LazyLoadingEnabled = true;
context1.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
context1.ChangeTracker.CascadeDeleteTiming = CascadeTiming.Immediate;
context1.ChangeTracker.DeleteOrphansTiming = CascadeTiming.Immediate;
context1.Database.AutoTransactionsEnabled = true;
context1.Database.AutoSavepointsEnabled = true;
context1.Database.SetCommandTimeout(1);
context1.SavingChanges += (sender, args) => { };
context1.SavedChanges += (sender, args) => { };
context1.SaveChangesFailed += (sender, args) => { };
Expand Down Expand Up @@ -711,6 +714,7 @@ public async Task Context_configuration_is_reset(bool useInterface, bool async)
Assert.Equal(CascadeTiming.Never, context2.ChangeTracker.DeleteOrphansTiming);
Assert.False(context2.Database.AutoTransactionsEnabled);
Assert.False(context2.Database.AutoSavepointsEnabled);
Assert.Null(context1.Database.GetCommandTimeout());
}

[ConditionalTheory]
Expand Down