2019-01-24 08:43:03 +00:00
|
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2018-02-12 08:55:11 +00:00
|
|
|
|
using System;
|
2018-05-28 10:56:27 +00:00
|
|
|
|
using System.Collections.Generic;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2018-02-12 08:55:11 +00:00
|
|
|
|
namespace osu.Game.Database
|
|
|
|
|
{
|
|
|
|
|
public class DatabaseWriteUsage : IDisposable
|
|
|
|
|
{
|
|
|
|
|
public readonly OsuDbContext Context;
|
|
|
|
|
private readonly Action<DatabaseWriteUsage> usageCompleted;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2018-02-12 08:55:11 +00:00
|
|
|
|
public DatabaseWriteUsage(OsuDbContext context, Action<DatabaseWriteUsage> onCompleted)
|
|
|
|
|
{
|
|
|
|
|
Context = context;
|
|
|
|
|
usageCompleted = onCompleted;
|
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2018-02-12 10:57:21 +00:00
|
|
|
|
public bool PerformedWrite { get; private set; }
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2018-02-12 10:57:21 +00:00
|
|
|
|
private bool isDisposed;
|
2018-05-28 10:56:27 +00:00
|
|
|
|
public List<Exception> Errors = new List<Exception>();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Whether this write usage will commit a transaction on completion.
|
|
|
|
|
/// If false, there is a parent usage responsible for transaction commit.
|
|
|
|
|
/// </summary>
|
2020-11-01 17:47:40 +00:00
|
|
|
|
public bool IsTransactionLeader;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2018-02-12 10:57:21 +00:00
|
|
|
|
protected void Dispose(bool disposing)
|
2018-02-12 08:55:11 +00:00
|
|
|
|
{
|
2018-02-12 10:57:21 +00:00
|
|
|
|
if (isDisposed) return;
|
2019-02-28 04:31:40 +00:00
|
|
|
|
|
2018-02-12 10:57:21 +00:00
|
|
|
|
isDisposed = true;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2018-05-28 09:38:42 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2018-05-28 10:56:27 +00:00
|
|
|
|
PerformedWrite |= Context.SaveChanges() > 0;
|
2018-05-28 09:38:42 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-05-28 10:56:27 +00:00
|
|
|
|
Errors.Add(e);
|
2018-05-29 05:25:27 +00:00
|
|
|
|
throw;
|
2018-05-28 09:38:42 +00:00
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
usageCompleted?.Invoke(this);
|
|
|
|
|
}
|
2018-02-12 08:55:11 +00:00
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2018-02-12 10:57:21 +00:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Dispose(true);
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
}
|
2018-02-12 08:55:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|