Skip to content

Commit

Permalink
Reset CommandTimeout in context pooling (#24936)
Browse files Browse the repository at this point in the history
Fixes #24914
  • Loading branch information
roji authored May 20, 2021
1 parent c02d1b3 commit b2fc119
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
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

0 comments on commit b2fc119

Please sign in to comment.