Skip to content

Commit

Permalink
Use new ADO.NET async APIs
Browse files Browse the repository at this point in the history
Fixes #9015
Fixes #15917
  • Loading branch information
ajcvickers committed Jul 3, 2019
1 parent bb2fc27 commit 64177ee
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/EFCore.Relational/Diagnostics/DbConnectionInterceptor.cs
Expand Up @@ -109,7 +109,7 @@ public virtual void ConnectionOpened(DbConnection connection, ConnectionEndEvent
=> result;

/// <summary>
/// Called just before EF intends to call <see cref="DbConnection.Close()" /> in an async context.
/// Called just before EF intends to call <see cref="DbConnection.CloseAsync()" /> in an async context.
/// </summary>
/// <param name="connection"> The connection. </param>
/// <param name="eventData"> Contextual information about the connection. </param>
Expand Down Expand Up @@ -143,7 +143,7 @@ public virtual void ConnectionOpened(DbConnection connection, ConnectionEndEvent
}

/// <summary>
/// Called just after EF has called <see cref="DbConnection.Close()" />.
/// Called just after EF has called <see cref="DbConnection.CloseAsync()" />.
/// </summary>
/// <param name="connection"> The connection. </param>
/// <param name="eventData"> Contextual information about the connection. </param>
Expand Down
14 changes: 7 additions & 7 deletions src/EFCore.Relational/Diagnostics/DbTransactionInterceptor.cs
Expand Up @@ -67,7 +67,7 @@ public abstract class DbTransactionInterceptor : IDbTransactionInterceptor
=> result;

/// <summary>
/// Called just before EF intends to call <see cref="DbConnection.BeginTransaction(IsolationLevel)" />.
/// Called just before EF intends to call <see cref="DbConnection.BeginTransactionAsync(IsolationLevel, CancellationToken)" />.
/// </summary>
/// <param name="connection"> The connection. </param>
/// <param name="eventData"> Contextual information about connection and transaction. </param>
Expand All @@ -92,7 +92,7 @@ public abstract class DbTransactionInterceptor : IDbTransactionInterceptor

/// <summary>
/// <para>
/// Called immediately after EF calls <see cref="DbConnection.BeginTransaction(IsolationLevel)" />.
/// Called immediately after EF calls <see cref="DbConnection.BeginTransactionAsync(IsolationLevel, CancellationToken)" />.
/// </para>
/// <para>
/// This method is still called if an interceptor suppressed creation in <see cref="IDbTransactionInterceptor.TransactionStarting" />.
Expand All @@ -102,7 +102,7 @@ public abstract class DbTransactionInterceptor : IDbTransactionInterceptor
/// <param name="connection"> The connection. </param>
/// <param name="eventData"> Contextual information about connection and transaction. </param>
/// <param name="result">
/// The result of the call to <see cref="DbConnection.BeginTransaction(IsolationLevel)" />.
/// The result of the call to <see cref="DbConnection.BeginTransactionAsync(IsolationLevel, CancellationToken)" />.
/// This value is typically used as the return value for the implementation of this method.
/// </param>
/// <param name="cancellationToken"> The cancellation token. </param>
Expand Down Expand Up @@ -197,7 +197,7 @@ public abstract class DbTransactionInterceptor : IDbTransactionInterceptor
}

/// <summary>
/// Called just before EF intends to call <see cref="DbTransaction.Commit" />.
/// Called just before EF intends to call <see cref="DbTransaction.CommitAsync" />.
/// </summary>
/// <param name="transaction"> The transaction. </param>
/// <param name="eventData"> Contextual information about connection and transaction. </param>
Expand All @@ -222,7 +222,7 @@ public abstract class DbTransactionInterceptor : IDbTransactionInterceptor
=> Task.FromResult(result);

/// <summary>
/// Called immediately after EF calls <see cref="DbTransaction.Commit" />.
/// Called immediately after EF calls <see cref="DbTransaction.CommitAsync" />.
/// </summary>
/// <param name="transaction"> The transaction. </param>
/// <param name="eventData"> Contextual information about connection and transaction. </param>
Expand Down Expand Up @@ -269,7 +269,7 @@ public abstract class DbTransactionInterceptor : IDbTransactionInterceptor
}

/// <summary>
/// Called just before EF intends to call <see cref="DbTransaction.Rollback" />.
/// Called just before EF intends to call <see cref="DbTransaction.RollbackAsync" />.
/// </summary>
/// <param name="transaction"> The transaction. </param>
/// <param name="eventData"> Contextual information about connection and transaction. </param>
Expand All @@ -294,7 +294,7 @@ public abstract class DbTransactionInterceptor : IDbTransactionInterceptor
=> Task.FromResult(result);

/// <summary>
/// Called immediately after EF calls <see cref="DbTransaction.Rollback" />.
/// Called immediately after EF calls <see cref="DbTransaction.RollbackAsync" />.
/// </summary>
/// <param name="transaction"> The transaction. </param>
/// <param name="eventData"> Contextual information about connection and transaction. </param>
Expand Down
4 changes: 2 additions & 2 deletions src/EFCore.Relational/Diagnostics/IDbConnectionInterceptor.cs
Expand Up @@ -99,7 +99,7 @@ public interface IDbConnectionInterceptor : IInterceptor
CancellationToken cancellationToken = default);

/// <summary>
/// Called just before EF intends to call <see cref="DbConnection.Close()" />.
/// Called just before EF intends to call <see cref="DbConnection.CloseAsync()" />.
/// </summary>
/// <param name="connection"> The connection. </param>
/// <param name="eventData"> Contextual information about the connection. </param>
Expand Down Expand Up @@ -152,7 +152,7 @@ public interface IDbConnectionInterceptor : IInterceptor
[NotNull] ConnectionEndEventData eventData);

/// <summary>
/// Called just after EF has called <see cref="DbConnection.Close()" />.
/// Called just after EF has called <see cref="DbConnection.CloseAsync()" />.
/// </summary>
/// <param name="connection"> The connection. </param>
/// <param name="eventData"> Contextual information about the connection. </param>
Expand Down
14 changes: 7 additions & 7 deletions src/EFCore.Relational/Diagnostics/IDbTransactionInterceptor.cs
Expand Up @@ -82,7 +82,7 @@ public interface IDbTransactionInterceptor : IInterceptor
[CanBeNull] DbTransaction result);

/// <summary>
/// Called just before EF intends to call <see cref="DbConnection.BeginTransaction(IsolationLevel)" />.
/// Called just before EF intends to call <see cref="DbConnection.BeginTransactionAsync(IsolationLevel, CancellationToken)" />.
/// </summary>
/// <param name="connection"> The connection. </param>
/// <param name="eventData"> Contextual information about connection and transaction. </param>
Expand All @@ -108,7 +108,7 @@ public interface IDbTransactionInterceptor : IInterceptor

/// <summary>
/// <para>
/// Called immediately after EF calls <see cref="DbConnection.BeginTransaction(IsolationLevel)" />.
/// Called immediately after EF calls <see cref="DbConnection.BeginTransactionAsync(IsolationLevel, CancellationToken)" />.
/// </para>
/// <para>
/// This method is still called if an interceptor suppressed creation in <see cref="TransactionStarting" />.
Expand All @@ -118,7 +118,7 @@ public interface IDbTransactionInterceptor : IInterceptor
/// <param name="connection"> The connection. </param>
/// <param name="eventData"> Contextual information about connection and transaction. </param>
/// <param name="result">
/// The result of the call to <see cref="DbConnection.BeginTransaction(IsolationLevel)" />.
/// The result of the call to <see cref="DbConnection.BeginTransactionAsync(IsolationLevel, CancellationToken)" />.
/// This value is typically used as the return value for the implementation of this method.
/// </param>
/// <param name="cancellationToken"> The cancellation token. </param>
Expand Down Expand Up @@ -210,7 +210,7 @@ public interface IDbTransactionInterceptor : IInterceptor
[NotNull] TransactionEndEventData eventData);

/// <summary>
/// Called just before EF intends to call <see cref="DbTransaction.Commit" />.
/// Called just before EF intends to call <see cref="DbTransaction.CommitAsync" />.
/// </summary>
/// <param name="transaction"> The transaction. </param>
/// <param name="eventData"> Contextual information about connection and transaction. </param>
Expand All @@ -234,7 +234,7 @@ public interface IDbTransactionInterceptor : IInterceptor
CancellationToken cancellationToken = default);

/// <summary>
/// Called immediately after EF calls <see cref="DbTransaction.Commit" />.
/// Called immediately after EF calls <see cref="DbTransaction.CommitAsync" />.
/// </summary>
/// <param name="transaction"> The transaction. </param>
/// <param name="eventData"> Contextual information about connection and transaction. </param>
Expand Down Expand Up @@ -277,7 +277,7 @@ public interface IDbTransactionInterceptor : IInterceptor
[NotNull] TransactionEndEventData eventData);

/// <summary>
/// Called just before EF intends to call <see cref="DbTransaction.Rollback" />.
/// Called just before EF intends to call <see cref="DbTransaction.RollbackAsync" />.
/// </summary>
/// <param name="transaction"> The transaction. </param>
/// <param name="eventData"> Contextual information about connection and transaction. </param>
Expand All @@ -301,7 +301,7 @@ public interface IDbTransactionInterceptor : IInterceptor
CancellationToken cancellationToken = default);

/// <summary>
/// Called immediately after EF calls <see cref="DbTransaction.Rollback" />.
/// Called immediately after EF calls <see cref="DbTransaction.RollbackAsync" />.
/// </summary>
/// <param name="transaction"> The transaction. </param>
/// <param name="eventData"> Contextual information about connection and transaction. </param>
Expand Down
6 changes: 3 additions & 3 deletions src/EFCore.Relational/Storage/RelationalConnection.cs
Expand Up @@ -268,7 +268,7 @@ public virtual IDbContextTransaction BeginTransaction(System.Data.IsolationLevel

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,
Expand Down Expand Up @@ -451,7 +451,7 @@ public virtual async Task<bool> OpenAsync(CancellationToken cancellationToken, b
{
if (DbConnection.State == ConnectionState.Broken)
{
DbConnection.Close();
await DbConnection.CloseAsync();
}

var wasOpened = false;
Expand Down Expand Up @@ -684,7 +684,7 @@ public virtual async Task<bool> CloseAsync()
{
if (interceptionResult == null)
{
DbConnection.Close(); // Since no CloseAsync yet
await DbConnection.CloseAsync();
}

wasClosed = true;
Expand Down
4 changes: 2 additions & 2 deletions src/EFCore.Relational/Storage/RelationalTransaction.cs
Expand Up @@ -192,7 +192,7 @@ public virtual async Task CommitAsync(CancellationToken cancellationToken = defa

if (interceptionResult == null)
{
_dbTransaction.Commit();
await _dbTransaction.CommitAsync(cancellationToken);
}

await Logger.TransactionCommittedAsync(
Expand Down Expand Up @@ -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(
Expand Down
Expand Up @@ -394,21 +394,24 @@ private static Task<IEnumerable<T>> QueryAsync<T>(DbConnection connection, strin
{
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))
{
result = await executeAsync(command);
}

transaction?.Commit();
if (transaction != null)
{
await transaction.CommitAsync();
}

return result;
}
Expand All @@ -417,7 +420,7 @@ private static Task<IEnumerable<T>> QueryAsync<T>(DbConnection connection, strin
{
if (connection.State != ConnectionState.Closed)
{
connection.Close();
await connection.CloseAsync();
}
}
}
Expand Down

0 comments on commit 64177ee

Please sign in to comment.