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 22, 2019
1 parent 069d334 commit 2e5839e
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions src/EFCore.SqlServer/Storage/Internal/SqlServerDatabaseCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,18 @@ private bool Exists(bool retryOnNotExists)
{
try
{
using (new TransactionScope(TransactionScopeOption.Suppress))
{
_connection.Open(errorsExpected: true);
_connection.Close();
}

using var _ = new TransactionScope(TransactionScopeOption.Suppress);
_connection.Open(errorsExpected: true);

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

_connection.Close();
return true;
}
catch (SqlException e)
Expand Down Expand Up @@ -229,13 +235,19 @@ private Task<bool> ExistsAsync(bool retryOnNotExists, CancellationToken cancella
{
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);

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

await _connection.CloseAsync();
return true;
}
catch (SqlException e)
Expand Down

0 comments on commit 2e5839e

Please sign in to comment.