Added a lock on the database commit level to see if I can improve locked db's

pull/2900/head
Jamie Rees 6 years ago
parent a039de24b0
commit 3592781e94

@ -15,6 +15,7 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.2" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.2.2" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.2.2" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" /> <PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
<PackageReference Include="Nito.AsyncEx" Version="5.0.0-pre-05" />
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="1.1.9" /> <PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="1.1.9" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

@ -5,6 +5,7 @@ using System.Linq.Expressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Query; using Microsoft.EntityFrameworkCore.Query;
using Nito.AsyncEx;
using Ombi.Store.Context; using Ombi.Store.Context;
using Ombi.Store.Entities; using Ombi.Store.Entities;
@ -19,6 +20,7 @@ namespace Ombi.Store.Repository
} }
public DbSet<T> _db { get; } public DbSet<T> _db { get; }
private readonly U _ctx; private readonly U _ctx;
private readonly AsyncLock _mutex = new AsyncLock();
public async Task<T> Find(object key) public async Task<T> Find(object key)
{ {
@ -40,32 +42,32 @@ namespace Ombi.Store.Repository
_db.AddRange(content); _db.AddRange(content);
if (save) if (save)
{ {
await SaveChangesAsync(); await InternalSaveChanges();
} }
} }
public async Task<T> Add(T content) public async Task<T> Add(T content)
{ {
await _db.AddAsync(content); await _db.AddAsync(content);
await SaveChangesAsync(); await InternalSaveChanges();
return content; return content;
} }
public async Task Delete(T request) public async Task Delete(T request)
{ {
_db.Remove(request); _db.Remove(request);
await SaveChangesAsync(); await InternalSaveChanges();
} }
public async Task DeleteRange(IEnumerable<T> req) public async Task DeleteRange(IEnumerable<T> req)
{ {
_db.RemoveRange(req); _db.RemoveRange(req);
await SaveChangesAsync(); await InternalSaveChanges();
} }
public async Task<int> SaveChangesAsync() public async Task<int> SaveChangesAsync()
{ {
return await _ctx.SaveChangesAsync(); return await InternalSaveChanges();
} }
public IIncludableQueryable<TEntity, TProperty> Include<TEntity, TProperty>( public IIncludableQueryable<TEntity, TProperty> Include<TEntity, TProperty>(
@ -79,6 +81,14 @@ namespace Ombi.Store.Repository
{ {
await _ctx.Database.ExecuteSqlCommandAsync(sql); await _ctx.Database.ExecuteSqlCommandAsync(sql);
} }
private async Task<int> InternalSaveChanges()
{
using (await _mutex.LockAsync())
{
return await _ctx.SaveChangesAsync();
}
}
private bool _disposed; private bool _disposed;

Loading…
Cancel
Save