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 SELECT 1 check to Exists/CanConnect checks #18508

Merged
merged 1 commit into from
Oct 23, 2019
Merged
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
52 changes: 41 additions & 11 deletions src/EFCore.SqlServer/Storage/Internal/SqlServerDatabaseCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,21 @@ private bool Exists(bool retryOnNotExists)
{
while (true)
{
var opened = false;
try
{
using (new TransactionScope(TransactionScopeOption.Suppress))
{
_connection.Open(errorsExpected: true);
_connection.Close();
}
using var _ = new TransactionScope(TransactionScopeOption.Suppress);
_connection.Open(errorsExpected: true);
opened = true;

_rawSqlCommandBuilder
.Build("SELECT 1")
.ExecuteNonQuery(
new RelationalCommandParameterObject(
_connection,
null,
Dependencies.CurrentContext.Context,
Dependencies.CommandLogger));

return true;
}
Expand All @@ -209,6 +217,13 @@ private bool Exists(bool retryOnNotExists)

Thread.Sleep(RetryDelay);
}
finally
{
if (opened)
{
_connection.Close();
}
}
}
});

Expand All @@ -227,14 +242,22 @@ private Task<bool> ExistsAsync(bool retryOnNotExists, CancellationToken cancella
{
while (true)
{
var opened = false;

try
{
using (new TransactionScope(TransactionScopeOption.Suppress, TransactionScopeAsyncFlowOption.Enabled))
{
await _connection.OpenAsync(ct, errorsExpected: true);

await _connection.CloseAsync();
}
using var _ = new TransactionScope(TransactionScopeOption.Suppress, TransactionScopeAsyncFlowOption.Enabled);
await _connection.OpenAsync(ct, errorsExpected: true);
opened = true;

await _rawSqlCommandBuilder
.Build("SELECT 1")
.ExecuteNonQueryAsync(new RelationalCommandParameterObject(
_connection,
null,
Dependencies.CurrentContext.Context,
Dependencies.CommandLogger),
ct);

return true;
}
Expand All @@ -254,6 +277,13 @@ private Task<bool> ExistsAsync(bool retryOnNotExists, CancellationToken cancella

await Task.Delay(RetryDelay, ct);
}
finally
{
if (opened)
{
await _connection.CloseAsync();
}
}
}
}, cancellationToken);

Expand Down