Skip to content

Commit 0f95611

Browse files
committed
Correcting ownership of EF transaction (it disposes of the context itself later)
1 parent 4654104 commit 0f95611

3 files changed

Lines changed: 17 additions & 4 deletions

File tree

bitwarden_license/test/Scim.IntegrationTest/Controllers/v2/UsersControllerConcurrencyTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ public async Task Post_ConcurrentInvites_DoNotOvershootMaxAutoscaleSeats()
2929
{
3030
TestDatabase = new SqlServerTestDatabase()
3131
};
32-
factory.SubstituteService((IFeatureService f)
33-
=> f.IsEnabled(FeatureFlagKeys.ScimInviteUserOptimization).Returns(true));
32+
33+
factory.SubstituteService((IFeatureService f) => f.IsEnabled(FeatureFlagKeys.ScimInviteUserOptimization)
34+
.Returns(true));
3435

3536
try
3637
{
@@ -74,7 +75,7 @@ public async Task Post_ConcurrentInvites_DoNotOvershootMaxAutoscaleSeats()
7475
}
7576
finally
7677
{
77-
factory.Dispose();
78+
await factory.DisposeAsync();
7879
}
7980
}
8081

src/Core/Platform/Data/TransactionState.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ public sealed class TransactionHolder : IAsyncDisposable
2121
public bool Committed { get; set; }
2222
public bool Doomed { get; set; }
2323

24+
/// <summary>
25+
/// True when this holder is responsible for disposing <see cref="Connection"/>.
26+
/// EF reuses the DbContext's connection and must leave its lifetime to the scope;
27+
/// Dapper opens its own connection and must dispose it here.
28+
/// </summary>
29+
public bool OwnsConnection { get; init; } = true;
30+
2431
/// <summary>
2532
/// For EF: the DatabaseContext associated with this transaction.
2633
/// </summary>
@@ -46,7 +53,11 @@ public async ValueTask DisposeAsync()
4653
}
4754

4855
await Transaction.DisposeAsync();
49-
await Connection.DisposeAsync();
56+
57+
if (OwnsConnection)
58+
{
59+
await Connection.DisposeAsync();
60+
}
5061

5162
if (Scope is not null)
5263
{

src/Infrastructure.EntityFramework/Data/EfTransactionManager.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public async Task<ITransactionScope> BeginTransactionAsync(
4040
{
4141
Connection = connection,
4242
Transaction = transaction,
43+
OwnsConnection = false,
4344
DbContext = dbContext,
4445
Scope = scope,
4546
};

0 commit comments

Comments
 (0)