From 2d263636bed92b6da59564aa7358e65e8e8c4cc6 Mon Sep 17 00:00:00 2001 From: Arthur Vickers Date: Fri, 28 Jun 2019 07:20:48 -0700 Subject: [PATCH] Return transaction even if no interceptor registered Fixes #16336 to unblock ASP.NET https://github.com/aspnet/AspNetCore/pull/11626 --- .../Diagnostics/RelationalLoggerExtensions.cs | 20 ++++++++-- .../TransactionInterceptionTestBase.cs | 38 +++++++++++++++++++ 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/src/EFCore.Relational/Diagnostics/RelationalLoggerExtensions.cs b/src/EFCore.Relational/Diagnostics/RelationalLoggerExtensions.cs index 2a5486ca1f5..d36b1fc4680 100644 --- a/src/EFCore.Relational/Diagnostics/RelationalLoggerExtensions.cs +++ b/src/EFCore.Relational/Diagnostics/RelationalLoggerExtensions.cs @@ -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; @@ -1896,7 +1899,10 @@ public static Task 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); @@ -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; @@ -2034,7 +2043,10 @@ public static Task 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); diff --git a/test/EFCore.Relational.Specification.Tests/TransactionInterceptionTestBase.cs b/test/EFCore.Relational.Specification.Tests/TransactionInterceptionTestBase.cs index c23ab34b9d5..1fc834b3f4d 100644 --- a/test/EFCore.Relational.Specification.Tests/TransactionInterceptionTestBase.cs +++ b/test/EFCore.Relational.Specification.Tests/TransactionInterceptionTestBase.cs @@ -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; @@ -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())) + { + 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())) + { + 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)]