Remove SingletonContextFactory

It is dangerous to use this as it doesn't correctly handle contexts and can cause issues that will never actually arise in normal execution.
This commit is contained in:
Dean Herbert 2018-07-18 16:43:46 +09:00
parent 90840c9384
commit 1d52231d4f
5 changed files with 15 additions and 31 deletions

View File

@ -62,7 +62,12 @@ private void load()
var storage = new TestStorage(@"TestCasePlaySongSelect");
// this is by no means clean. should be replacing inside of OsuGameBase somehow.
IDatabaseContextFactory factory = new SingletonContextFactory(new OsuDbContext());
DatabaseContextFactory factory = new DatabaseContextFactory(storage);
factory.ResetDatabase();
using (var usage = factory.Get())
usage.Migrate();
Dependencies.Cache(rulesets = new RulesetStore(factory));
Dependencies.Cache(manager = new BeatmapManager(storage, factory, rulesets, null, null)

View File

@ -11,7 +11,7 @@ namespace osu.Game.Database
{
public class DatabaseContextFactory : IDatabaseContextFactory
{
private readonly GameHost host;
private readonly Storage storage;
private const string database_name = @"client";
@ -26,9 +26,9 @@ public class DatabaseContextFactory : IDatabaseContextFactory
private IDbContextTransaction currentWriteTransaction;
public DatabaseContextFactory(GameHost host)
public DatabaseContextFactory(Storage storage)
{
this.host = host;
this.storage = storage;
recycleThreadContexts();
}
@ -117,7 +117,7 @@ private void usageCompleted(DatabaseWriteUsage usage)
private void recycleThreadContexts() => threadContexts = new ThreadLocal<OsuDbContext>(CreateContext);
protected virtual OsuDbContext CreateContext() => new OsuDbContext(host.Storage.GetDatabaseConnectionString(database_name))
protected virtual OsuDbContext CreateContext() => new OsuDbContext(storage.GetDatabaseConnectionString(database_name))
{
Database = { AutoTransactionsEnabled = false }
};
@ -129,7 +129,7 @@ public void ResetDatabase()
recycleThreadContexts();
GC.Collect();
GC.WaitForPendingFinalizers();
host.Storage.DeleteDatabase(database_name);
storage.DeleteDatabase(database_name);
}
}
}

View File

@ -1,19 +0,0 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
namespace osu.Game.Database
{
public class SingletonContextFactory : IDatabaseContextFactory
{
private readonly OsuDbContext context;
public SingletonContextFactory(OsuDbContext context)
{
this.context = context;
}
public OsuDbContext Get() => context;
public DatabaseWriteUsage GetForWrite(bool withTransaction = true) => new DatabaseWriteUsage(context, null) { IsTransactionLeader = true };
}
}

View File

@ -107,7 +107,7 @@ private void load()
{
Resources.AddStore(new DllResourceStore(@"osu.Game.Resources.dll"));
dependencies.Cache(contextFactory = new DatabaseContextFactory(Host));
dependencies.Cache(contextFactory = new DatabaseContextFactory(Host.Storage));
dependencies.Cache(new LargeTextureStore(new RawTextureLoaderStore(new NamespacedResourceStore<byte[]>(Resources, @"Textures"))));

View File

@ -7,13 +7,11 @@ namespace osu.Game.Tests.Platform
{
public class TestStorage : DesktopStorage
{
public TestStorage(string baseName) : base(baseName, null)
public TestStorage(string baseName)
: base(baseName, null)
{
}
public override string GetDatabaseConnectionString(string name)
{
return "DataSource=:memory:";
}
public override string GetDatabaseConnectionString(string name) => "Data Source=" + GetUsablePathFor($"{(object)name}.db", true);
}
}