From b882fcf7653a3f6911fd174d9e4b2b492cf5b389 Mon Sep 17 00:00:00 2001 From: Arthur Vickers Date: Wed, 3 Jul 2019 09:45:03 -0700 Subject: [PATCH] Use new ADO.NET async APIs Fixes #9015 Fixes #15917 --- .../Diagnostics/DbConnectionInterceptor.cs | 4 ++-- .../Diagnostics/DbTransactionInterceptor.cs | 14 +++++++------- .../Diagnostics/IDbConnectionInterceptor.cs | 4 ++-- .../Diagnostics/IDbTransactionInterceptor.cs | 14 +++++++------- .../Storage/RelationalConnection.cs | 6 +++--- .../Storage/RelationalTransaction.cs | 4 ++-- .../TestUtilities/SqlServerTestStore.cs | 11 +++++++---- 7 files changed, 30 insertions(+), 27 deletions(-) diff --git a/src/EFCore.Relational/Diagnostics/DbConnectionInterceptor.cs b/src/EFCore.Relational/Diagnostics/DbConnectionInterceptor.cs index 17d95f5a0b8..cfa7b9f92b3 100644 --- a/src/EFCore.Relational/Diagnostics/DbConnectionInterceptor.cs +++ b/src/EFCore.Relational/Diagnostics/DbConnectionInterceptor.cs @@ -109,7 +109,7 @@ public virtual Task ConnectionOpenedAsync( => result; /// - /// Called just before EF intends to call in an async context. + /// Called just before EF intends to call in an async context. /// /// The connection. /// Contextual information about the connection. @@ -143,7 +143,7 @@ public virtual void ConnectionClosed( } /// - /// Called just after EF has called . + /// Called just after EF has called . /// /// The connection. /// Contextual information about the connection. diff --git a/src/EFCore.Relational/Diagnostics/DbTransactionInterceptor.cs b/src/EFCore.Relational/Diagnostics/DbTransactionInterceptor.cs index 65997df9f36..8c23fca24b2 100644 --- a/src/EFCore.Relational/Diagnostics/DbTransactionInterceptor.cs +++ b/src/EFCore.Relational/Diagnostics/DbTransactionInterceptor.cs @@ -67,7 +67,7 @@ public virtual DbTransaction TransactionStarted( => result; /// - /// Called just before EF intends to call . + /// Called just before EF intends to call . /// /// The connection. /// Contextual information about connection and transaction. @@ -92,7 +92,7 @@ public virtual DbTransaction TransactionStarted( /// /// - /// Called immediately after EF calls . + /// Called immediately after EF calls . /// /// /// This method is still called if an interceptor suppressed creation in . @@ -102,7 +102,7 @@ public virtual DbTransaction TransactionStarted( /// The connection. /// Contextual information about connection and transaction. /// - /// The result of the call to . + /// The result of the call to . /// This value is typically used as the return value for the implementation of this method. /// /// The cancellation token. @@ -197,7 +197,7 @@ public virtual void TransactionCommitted( } /// - /// Called just before EF intends to call . + /// Called just before EF intends to call . /// /// The transaction. /// Contextual information about connection and transaction. @@ -222,7 +222,7 @@ public virtual void TransactionCommitted( => Task.FromResult(result); /// - /// Called immediately after EF calls . + /// Called immediately after EF calls . /// /// The transaction. /// Contextual information about connection and transaction. @@ -269,7 +269,7 @@ public virtual void TransactionRolledBack( } /// - /// Called just before EF intends to call . + /// Called just before EF intends to call . /// /// The transaction. /// Contextual information about connection and transaction. @@ -294,7 +294,7 @@ public virtual void TransactionRolledBack( => Task.FromResult(result); /// - /// Called immediately after EF calls . + /// Called immediately after EF calls . /// /// The transaction. /// Contextual information about connection and transaction. diff --git a/src/EFCore.Relational/Diagnostics/IDbConnectionInterceptor.cs b/src/EFCore.Relational/Diagnostics/IDbConnectionInterceptor.cs index 203613290ce..6b3cb476e32 100644 --- a/src/EFCore.Relational/Diagnostics/IDbConnectionInterceptor.cs +++ b/src/EFCore.Relational/Diagnostics/IDbConnectionInterceptor.cs @@ -99,7 +99,7 @@ Task ConnectionOpenedAsync( CancellationToken cancellationToken = default); /// - /// Called just before EF intends to call . + /// Called just before EF intends to call . /// /// The connection. /// Contextual information about the connection. @@ -152,7 +152,7 @@ void ConnectionClosed( [NotNull] ConnectionEndEventData eventData); /// - /// Called just after EF has called . + /// Called just after EF has called . /// /// The connection. /// Contextual information about the connection. diff --git a/src/EFCore.Relational/Diagnostics/IDbTransactionInterceptor.cs b/src/EFCore.Relational/Diagnostics/IDbTransactionInterceptor.cs index 2842dd4f481..9e4bd358883 100644 --- a/src/EFCore.Relational/Diagnostics/IDbTransactionInterceptor.cs +++ b/src/EFCore.Relational/Diagnostics/IDbTransactionInterceptor.cs @@ -82,7 +82,7 @@ DbTransaction TransactionStarted( [CanBeNull] DbTransaction result); /// - /// Called just before EF intends to call . + /// Called just before EF intends to call . /// /// The connection. /// Contextual information about connection and transaction. @@ -108,7 +108,7 @@ DbTransaction TransactionStarted( /// /// - /// Called immediately after EF calls . + /// Called immediately after EF calls . /// /// /// This method is still called if an interceptor suppressed creation in . @@ -118,7 +118,7 @@ DbTransaction TransactionStarted( /// The connection. /// Contextual information about connection and transaction. /// - /// The result of the call to . + /// The result of the call to . /// This value is typically used as the return value for the implementation of this method. /// /// The cancellation token. @@ -210,7 +210,7 @@ void TransactionCommitted( [NotNull] TransactionEndEventData eventData); /// - /// Called just before EF intends to call . + /// Called just before EF intends to call . /// /// The transaction. /// Contextual information about connection and transaction. @@ -234,7 +234,7 @@ void TransactionCommitted( CancellationToken cancellationToken = default); /// - /// Called immediately after EF calls . + /// Called immediately after EF calls . /// /// The transaction. /// Contextual information about connection and transaction. @@ -277,7 +277,7 @@ void TransactionRolledBack( [NotNull] TransactionEndEventData eventData); /// - /// Called just before EF intends to call . + /// Called just before EF intends to call . /// /// The transaction. /// Contextual information about connection and transaction. @@ -301,7 +301,7 @@ void TransactionRolledBack( CancellationToken cancellationToken = default); /// - /// Called immediately after EF calls . + /// Called immediately after EF calls . /// /// The transaction. /// Contextual information about connection and transaction. diff --git a/src/EFCore.Relational/Storage/RelationalConnection.cs b/src/EFCore.Relational/Storage/RelationalConnection.cs index 6af11f9ccca..e604fd3c0f6 100644 --- a/src/EFCore.Relational/Storage/RelationalConnection.cs +++ b/src/EFCore.Relational/Storage/RelationalConnection.cs @@ -268,7 +268,7 @@ public virtual async Task BeginTransactionAsync( var dbTransaction = interceptionResult.HasValue ? interceptionResult.Value.Result - : DbConnection.BeginTransaction(isolationLevel); // Use BeginTransactionAsync when available + : await DbConnection.BeginTransactionAsync(isolationLevel, cancellationToken); dbTransaction = await Dependencies.TransactionLogger.TransactionStartedAsync( this, @@ -451,7 +451,7 @@ public virtual async Task OpenAsync(CancellationToken cancellationToken, b { if (DbConnection.State == ConnectionState.Broken) { - DbConnection.Close(); + await DbConnection.CloseAsync(); } var wasOpened = false; @@ -684,7 +684,7 @@ public virtual async Task CloseAsync() { if (interceptionResult == null) { - DbConnection.Close(); // Since no CloseAsync yet + await DbConnection.CloseAsync(); } wasClosed = true; diff --git a/src/EFCore.Relational/Storage/RelationalTransaction.cs b/src/EFCore.Relational/Storage/RelationalTransaction.cs index 043e2a597b7..b924def4b95 100644 --- a/src/EFCore.Relational/Storage/RelationalTransaction.cs +++ b/src/EFCore.Relational/Storage/RelationalTransaction.cs @@ -192,7 +192,7 @@ public virtual async Task CommitAsync(CancellationToken cancellationToken = defa if (interceptionResult == null) { - _dbTransaction.Commit(); + await _dbTransaction.CommitAsync(cancellationToken); } await Logger.TransactionCommittedAsync( @@ -242,7 +242,7 @@ public virtual async Task RollbackAsync(CancellationToken cancellationToken = de if (interceptionResult == null) { - _dbTransaction.Rollback(); // Use RollbackAsync when available + await _dbTransaction.RollbackAsync(cancellationToken); } await Logger.TransactionRolledBackAsync( diff --git a/test/EFCore.SqlServer.FunctionalTests/TestUtilities/SqlServerTestStore.cs b/test/EFCore.SqlServer.FunctionalTests/TestUtilities/SqlServerTestStore.cs index 99a1a181168..e983f56c05c 100644 --- a/test/EFCore.SqlServer.FunctionalTests/TestUtilities/SqlServerTestStore.cs +++ b/test/EFCore.SqlServer.FunctionalTests/TestUtilities/SqlServerTestStore.cs @@ -394,13 +394,13 @@ private static async Task ExecuteCommandAsync( { if (connection.State != ConnectionState.Closed) { - connection.Close(); + await connection.CloseAsync(); } await connection.OpenAsync(); try { - using (var transaction = useTransaction ? connection.BeginTransaction() : null) + using (var transaction = useTransaction ? await connection.BeginTransactionAsync() : null) { T result; using (var command = CreateCommand(connection, sql, parameters)) @@ -408,7 +408,10 @@ private static async Task ExecuteCommandAsync( result = await executeAsync(command); } - transaction?.Commit(); + if (transaction != null) + { + await transaction.CommitAsync(); + } return result; } @@ -417,7 +420,7 @@ private static async Task ExecuteCommandAsync( { if (connection.State != ConnectionState.Closed) { - connection.Close(); + await connection.CloseAsync(); } } }