1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-21 12:03:21 +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,6 +20,8 @@ namespace osu.Game.Database
{ {
Transaction? transaction = null; Transaction? transaction = null;
try
{
if (!realm.IsInTransaction) if (!realm.IsInTransaction)
transaction = realm.BeginWrite(); transaction = realm.BeginWrite();
@ -27,6 +29,11 @@ namespace osu.Game.Database
transaction?.Commit(); transaction?.Commit();
} }
finally
{
transaction?.Dispose();
}
}
/// <summary> /// <summary>
/// Perform a write operation against the provided realm instance. /// Perform a write operation against the provided realm instance.
@ -40,6 +47,8 @@ namespace osu.Game.Database
{ {
Transaction? transaction = null; Transaction? transaction = null;
try
{
if (!realm.IsInTransaction) if (!realm.IsInTransaction)
transaction = realm.BeginWrite(); transaction = realm.BeginWrite();
@ -49,6 +58,11 @@ namespace osu.Game.Database
return result; return result;
} }
finally
{
transaction?.Dispose();
}
}
/// <summary> /// <summary>
/// Whether the provided change set has changes to the top level collection. /// Whether the provided change set has changes to the top level collection.