Skip to content

Commit

Permalink
Add SELECT 1 check to Exists/CanConnect checks
Browse files Browse the repository at this point in the history
Fixes #18330
  • Loading branch information
roji committed Oct 23, 2019
1 parent 22679cf commit fd4cd15
Showing 1 changed file with 41 additions and 11 deletions.
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

0 comments on commit fd4cd15

Please sign in to comment.