1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-21 12:12:56 +08:00

Fix transaction not being disposed

This commit is contained in:
Dean Herbert 2022-07-07 18:15:15 +09:00
parent 5197d0fa9e
commit ac216d94a8
2 changed files with 41 additions and 9 deletions

View File

@ -59,6 +59,24 @@ namespace osu.Game.Tests.Database
}); });
} }
[Test]
public void TestFailedWritePerformsRollback()
{
RunTestWithRealm((realm, _) =>
{
Assert.Throws<InvalidOperationException>(() =>
{
realm.Write(r =>
{
r.Add(new BeatmapInfo(CreateRuleset(), new BeatmapDifficulty(), new BeatmapMetadata()));
throw new InvalidOperationException();
});
});
Assert.That(realm.Run(r => r.All<BeatmapInfo>()), Is.Empty);
});
}
[Test] [Test]
public void TestNestedWriteCalls() public void TestNestedWriteCalls()
{ {

View File

@ -20,12 +20,19 @@ namespace osu.Game.Database
{ {
Transaction? transaction = null; Transaction? transaction = null;
if (!realm.IsInTransaction) try
transaction = realm.BeginWrite(); {
if (!realm.IsInTransaction)
transaction = realm.BeginWrite();
function(realm); function(realm);
transaction?.Commit(); transaction?.Commit();
}
finally
{
transaction?.Dispose();
}
} }
/// <summary> /// <summary>
@ -40,14 +47,21 @@ namespace osu.Game.Database
{ {
Transaction? transaction = null; Transaction? transaction = null;
if (!realm.IsInTransaction) try
transaction = realm.BeginWrite(); {
if (!realm.IsInTransaction)
transaction = realm.BeginWrite();
var result = function(realm); var result = function(realm);
transaction?.Commit(); transaction?.Commit();
return result; return result;
}
finally
{
transaction?.Dispose();
}
} }
/// <summary> /// <summary>