Make cover loading thread save (Just DB part!!!)

This commit is contained in:
Flamefire committed 2014-03-16 18:41:52 +01:00
1 parent e1f4b93d03
commit 542c8a5b37
1 file changed
+21 -3
+21 -3
View File
@@ -53,6 +53,8 @@ namespace Vocaluxe.Base
private static string _CoverFilePath; private static string _CoverFilePath;
private static string _CreditsRessourcesFilePath; private static string _CreditsRessourcesFilePath;
//You have to lock all actions using cover connection or transaction, otherwhise order is not guaranted
private static readonly object _CoverMutex = new object();
private static SQLiteConnection _ConnectionCover; private static SQLiteConnection _ConnectionCover;
private static SQLiteTransaction _TransactionCover; private static SQLiteTransaction _TransactionCover;
@@ -77,7 +79,6 @@ namespace Vocaluxe.Base
CLog.LogError("Error initializing Credits-DB", true, true); CLog.LogError("Error initializing Credits-DB", true, true);
return false; return false;
} }
GC.Collect();
return true; return true;
} }
@@ -1059,6 +1060,8 @@ namespace Vocaluxe.Base
return false; return false;
} }
lock (_CoverMutex)
{
if (_ConnectionCover == null) if (_ConnectionCover == null)
{ {
_ConnectionCover = new SQLiteConnection {ConnectionString = "Data Source=" + _CoverFilePath}; _ConnectionCover = new SQLiteConnection {ConnectionString = "Data Source=" + _CoverFilePath};
@@ -1164,21 +1167,35 @@ namespace Vocaluxe.Base
} }
} }
} }
}
return false; return false;
} }
public static void CommitCovers() public static void CommitCovers()
{
lock (_CoverMutex)
{
_CommitCovers();
}
}
/// <summary>
/// You have to hold the CoverMutex when calling this!
/// </summary>
private static void _CommitCovers()
{ {
if (_TransactionCover != null) if (_TransactionCover != null)
{ {
_TransactionCover.Commit(); _TransactionCover.Commit();
_TransactionCover.Dispose();
_TransactionCover = null; _TransactionCover = null;
GC.Collect();
} }
} }
public static void Close() public static void Close()
{
//Do commit and close atomicly otherwhise we may loose changes
lock (_CoverMutex)
{ {
CommitCovers(); CommitCovers();
@@ -1188,6 +1205,7 @@ namespace Vocaluxe.Base
_ConnectionCover = null; _ConnectionCover = null;
} }
} }
}
private static bool _InitCoverDB() private static bool _InitCoverDB()
{ {