Skip to content

Commit

Permalink
Return transaction even if no interceptor registered
Browse files Browse the repository at this point in the history
Fixes #16336 to unblock ASP.NET dotnet/aspnetcore#11626
  • Loading branch information
ajcvickers committed Jun 28, 2019
1 parent 437d339 commit 2d26363
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/EFCore.Relational/Diagnostics/RelationalLoggerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1849,7 +1849,10 @@ public static DbTransaction TransactionStarted(
definition,
diagnosticSourceEnabled);

return interceptor?.TransactionStarted(connection.DbConnection, eventData, transaction);
if (interceptor != null)
{
return interceptor.TransactionStarted(connection.DbConnection, eventData, transaction);
}
}

return transaction;
Expand Down Expand Up @@ -1896,7 +1899,10 @@ public static Task<DbTransaction> TransactionStartedAsync(
definition,
diagnosticSourceEnabled);

return interceptor?.TransactionStartedAsync(connection.DbConnection, eventData, transaction, cancellationToken);
if (interceptor != null)
{
return interceptor.TransactionStartedAsync(connection.DbConnection, eventData, transaction, cancellationToken);
}
}

return Task.FromResult(transaction);
Expand Down Expand Up @@ -1990,7 +1996,10 @@ public static DbTransaction TransactionUsed(
definition,
diagnosticSourceEnabled);

return interceptor?.TransactionUsed(connection.DbConnection, eventData, transaction);
if (interceptor != null)
{
return interceptor.TransactionUsed(connection.DbConnection, eventData, transaction);
}
}

return transaction;
Expand Down Expand Up @@ -2034,7 +2043,10 @@ public static Task<DbTransaction> TransactionUsedAsync(
definition,
diagnosticSourceEnabled);

return interceptor?.TransactionUsedAsync(connection.DbConnection, eventData, transaction, cancellationToken);
if (interceptor != null)
{
return interceptor.TransactionUsedAsync(connection.DbConnection, eventData, transaction, cancellationToken);
}
}

return Task.FromResult(transaction);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Diagnostics;
Expand All @@ -19,6 +20,43 @@ protected TransactionInterceptionTestBase(InterceptionFixtureBase fixture)
{
}

[ConditionalTheory]
[InlineData(false)]
[InlineData(true)]
public virtual async Task BeginTransaction_without_interceptor(bool async)
{
using (var context = CreateContext(Enumerable.Empty<IInterceptor>()))
{
using (var transaction = async
? await context.Database.BeginTransactionAsync()
: context.Database.BeginTransaction())
{
Assert.NotNull(transaction.GetDbTransaction());
}
}
}

[ConditionalTheory]
[InlineData(false)]
[InlineData(true)]
public virtual async Task UseTransaction_without_interceptor(bool async)
{
using (var context = CreateContext(Enumerable.Empty<IInterceptor>()))
{
using (var transaction = context.Database.GetDbConnection().BeginTransaction())
{
var contextTransaction = async
? await context.Database.UseTransactionAsync(transaction)
: context.Database.UseTransaction(transaction);

{
Assert.NotNull(contextTransaction.GetDbTransaction());
Assert.Same(transaction, contextTransaction.GetDbTransaction());
}
}
}
}

[ConditionalTheory]
[InlineData(false)]
[InlineData(true)]
Expand Down

0 comments on commit 2d26363

Please sign in to comment.