Skip to content

Commit

Permalink
Fix db context concurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaliumhexacyanoferrat committed Apr 25, 2024
1 parent 717d9a5 commit 60d49af
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions frameworks/CSharp/genhttp/Benchmarks/Model/DatabaseContext.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Internal;
using PooledAwait;
using System;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading.Tasks;

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Internal;

using PooledAwait;

namespace Benchmarks.Model
{

Expand Down Expand Up @@ -54,11 +52,12 @@ public DatabaseContext(DbContextOptions options) : base(options) { }

public static async PooledValueTask WithNoTrackingAsync(Func<DatabaseContext, PooledValueTask> logic)
{
using var lease = _NoTrackingPool.Rent();
var lease = _NoTrackingPool.Rent();

try
{
await logic((DatabaseContext)lease);
await lease.DisposeAsync();
}
finally
{
Expand All @@ -68,11 +67,15 @@ public static async PooledValueTask WithNoTrackingAsync(Func<DatabaseContext, Po

public static async PooledValueTask<T> WithNoTrackingAsync<T>(Func<DatabaseContext, PooledValueTask<T>> logic)
{
using var lease = _NoTrackingPool.Rent();
var lease = _NoTrackingPool.Rent();

try
{
return await logic((DatabaseContext)lease);
var result = await logic((DatabaseContext)lease);

await lease.DisposeAsync();

return result;
}
finally
{
Expand All @@ -82,19 +85,19 @@ public static async PooledValueTask<T> WithNoTrackingAsync<T>(Func<DatabaseConte

public static async PooledValueTask WithTrackingAsync(Func<DatabaseContext, PooledValueTask> logic)
{
using var lease = _TrackingPool.Rent();
var lease = _TrackingPool.Rent();

try
{
await logic((DatabaseContext)lease);
await lease.DisposeAsync();
}
finally
{
await _TrackingPool.ReturnAsync(lease);
}
}


#endregion

#region Entities
Expand Down

0 comments on commit 60d49af

Please sign in to comment.