Merge pull request #18951 from peppy/fix-realm-missing-transaction-rollback

Fix `PerformWrite` not rolling back transaction on exception
This commit is contained in:
Dan Balasescu
2022-06-30 17:17:56 +09:00
committed by GitHub
3 changed files with 28 additions and 5 deletions

View File

@ -91,6 +91,25 @@ namespace osu.Game.Tests.Database
Assert.IsFalse(liveBeatmap.PerformRead(l => l.Hidden)); Assert.IsFalse(liveBeatmap.PerformRead(l => l.Hidden));
} }
[Test]
public void TestTransactionRolledBackOnException()
{
RunTestWithRealm((realm, _) =>
{
var beatmap = new BeatmapInfo(CreateRuleset(), new BeatmapDifficulty(), new BeatmapMetadata());
realm.Run(r => r.Write(_ => r.Add(beatmap)));
var liveBeatmap = beatmap.ToLive(realm);
Assert.Throws<InvalidOperationException>(() => liveBeatmap.PerformWrite(l => throw new InvalidOperationException()));
Assert.IsFalse(liveBeatmap.PerformRead(l => l.Hidden));
liveBeatmap.PerformWrite(l => l.Hidden = true);
Assert.IsTrue(liveBeatmap.PerformRead(l => l.Hidden));
});
}
[Test] [Test]
public void TestScopedReadWithoutContext() public void TestScopedReadWithoutContext()
{ {

View File

@ -104,9 +104,12 @@ namespace osu.Game.Database
PerformRead(t => PerformRead(t =>
{ {
var transaction = t.Realm.BeginWrite(); using (var transaction = t.Realm.BeginWrite())
{
perform(t); perform(t);
transaction.Commit(); transaction.Commit();
}
RealmLiveStatistics.WRITES.Value++; RealmLiveStatistics.WRITES.Value++;
}); });
} }

View File

@ -1,6 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System;
using JetBrains.Annotations; using JetBrains.Annotations;
using osu.Framework.Testing; using osu.Framework.Testing;
using osu.Game.Database; using osu.Game.Database;
@ -19,8 +20,8 @@ namespace osu.Game.Models
public RealmNamedFileUsage(RealmFile file, string filename) public RealmNamedFileUsage(RealmFile file, string filename)
{ {
File = file; File = file ?? throw new ArgumentNullException(nameof(file));
Filename = filename; Filename = filename ?? throw new ArgumentNullException(nameof(filename));
} }
[UsedImplicitly] [UsedImplicitly]