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

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.

# Conflicts:
#	osu.Game/Database/SingletonContextFactory.cs
This commit is contained in:
Dean Herbert 2018-07-18 16:43:46 +09:00
parent d0f7bdbcb8
commit 0c24244340
5 changed files with 15 additions and 31 deletions

View File

@ -62,7 +62,12 @@ namespace osu.Game.Tests.Visual
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 @@ namespace osu.Game.Database
private IDbContextTransaction currentWriteTransaction;
public DatabaseContextFactory(GameHost host)
public DatabaseContextFactory(Storage storage)
{
this.host = host;
this.storage = storage;
recycleThreadContexts();
}
@ -117,7 +117,7 @@ namespace osu.Game.Database
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 @@ namespace osu.Game.Database
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);
}
}

View File

@ -107,7 +107,7 @@ namespace osu.Game
{
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);
}
}