Skip to content
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
29 changes: 23 additions & 6 deletions src/EFCore.Relational/Storage/RelationalConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ public virtual bool Open(bool errorsExpected = false)
{
CurrentTransaction?.Dispose();
ClearTransactions(clearAmbient: false);
OpenDbConnection(errorsExpected);
OpenInternal(errorsExpected);
wasOpened = true;
}

Expand Down Expand Up @@ -668,7 +668,7 @@ public virtual async Task<bool> OpenAsync(CancellationToken cancellationToken, b
}

ClearTransactions(clearAmbient: false);
await OpenDbConnectionAsync(errorsExpected, cancellationToken).ConfigureAwait(false);
await OpenInternalAsync(errorsExpected, cancellationToken).ConfigureAwait(false);
wasOpened = true;
}

Expand Down Expand Up @@ -701,7 +701,7 @@ private void ClearTransactions(bool clearAmbient)
}
}

private void OpenDbConnection(bool errorsExpected)
private void OpenInternal(bool errorsExpected)
{
var startTime = DateTimeOffset.UtcNow;
var stopwatch = Stopwatch.StartNew();
Expand All @@ -712,7 +712,7 @@ private void OpenDbConnection(bool errorsExpected)
{
if (!interceptionResult.IsSuppressed)
{
DbConnection.Open();
OpenDbConnection(errorsExpected);
}

Dependencies.ConnectionLogger.ConnectionOpened(this, startTime, stopwatch.Elapsed);
Expand All @@ -730,7 +730,15 @@ private void OpenDbConnection(bool errorsExpected)
}
}

private async Task OpenDbConnectionAsync(bool errorsExpected, CancellationToken cancellationToken)
/// <summary>
/// Template method that by default calls <see cref="System.Data.Common.DbConnection.Open"/> but can be overriden
/// by providers to make a different call instead.
/// </summary>
/// <param name="errorsExpected"> Indicates if the connection errors are expected and should be logged as debug message. </param>
protected virtual void OpenDbConnection(bool errorsExpected)
=> DbConnection.Open();

private async Task OpenInternalAsync(bool errorsExpected, CancellationToken cancellationToken)
{
var startTime = DateTimeOffset.UtcNow;
var stopwatch = Stopwatch.StartNew();
Expand All @@ -743,7 +751,7 @@ var interceptionResult
{
if (!interceptionResult.IsSuppressed)
{
await DbConnection.OpenAsync(cancellationToken).ConfigureAwait(false);
await OpenDbConnectionAsync(errorsExpected, cancellationToken).ConfigureAwait(false);
}

await Dependencies.ConnectionLogger.ConnectionOpenedAsync(this, startTime, stopwatch.Elapsed, cancellationToken)
Expand All @@ -769,6 +777,15 @@ await Dependencies.ConnectionLogger.ConnectionErrorAsync(
}
}

/// <summary>
/// Template method that by default calls <see cref="M:System.Data.Common.DbConnection.OpenAsync"/> but can be overriden
/// by providers to make a different call instead.
/// </summary>
/// <param name="errorsExpected"> Indicates if the connection errors are expected and should be logged as debug message. </param>
/// <param name="cancellationToken"> A <see cref="CancellationToken" /> to observe while waiting for the task to complete. </param>
protected virtual Task OpenDbConnectionAsync(bool errorsExpected, CancellationToken cancellationToken)
=> DbConnection.OpenAsync(cancellationToken);

private void HandleAmbientTransactions()
{
var current = Transaction.Current;
Expand Down
20 changes: 20 additions & 0 deletions src/EFCore.SqlServer/Storage/Internal/SqlServerConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,26 @@ public SqlServerConnection([NotNull] RelationalConnectionDependencies dependenci
{
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override void OpenDbConnection(bool errorsExpected)
{
// Note: Not needed for the Async overload: see https://github.com/dotnet/SqlClient/issues/615
if (errorsExpected
&& DbConnection is SqlConnection sqlConnection)
{
sqlConnection.Open(SqlConnectionOverrides.OpenWithoutRetry);
}
else
{
DbConnection.Open();
}
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
Expand Down