Fix locking

This commit is contained in:
Flamefire committed 2014-09-25 13:06:20 +02:00
1 parent bf7eec4233
commit 3bce89eecf
2 files changed
+27 -9

No files matched your search

+7 -6
View File
@@ -35,13 +35,13 @@ namespace Vocaluxe.Lib.Database
{
public class CCoverDB : CDatabaseBase
{
//You have to lock all actions using cover connection or transaction, otherwhise order is not guaranted
private readonly object _CoverMutex = new object();
private SQLiteTransaction _TransactionCover;
public CCoverDB(string filePath) : base(filePath) {}
public override bool Init()
{
lock (_Mutex)
{
if (!base.Init())
return false;
@@ -50,13 +50,14 @@ namespace Vocaluxe.Lib.Database
return _CreateCoverDB();
if (_Version < CSettings.DatabaseCoverVersion)
throw new NotImplementedException("Upgrading of cover DB not implemented");
}
return true;
}
public override void Close()
{
//Do commit and close atomicly otherwhise we may loose changes
lock (_CoverMutex)
lock (_Mutex)
{
_CommitCovers();
@@ -74,7 +75,7 @@ namespace Vocaluxe.Lib.Database
return false;
}
lock (_CoverMutex)
lock (_Mutex)
{
//Double check here because we may have just closed our connection
if (_Connection == null)
@@ -147,7 +148,7 @@ namespace Vocaluxe.Lib.Database
tex = CDraw.EnqueueTexture(size.Width, size.Height, data);
lock (_CoverMutex)
lock (_Mutex)
{
//Double check here because we may have just closed our connection
if (_Connection == null)
@@ -184,7 +185,7 @@ namespace Vocaluxe.Lib.Database
public void CommitCovers()
{
lock (_CoverMutex)
lock (_Mutex)
{
_CommitCovers();
}
+20 -3
View File
@@ -17,7 +17,6 @@
using System;
using System.IO;
using Vocaluxe.Base;
#if WIN
using System.Data.SQLite;
@@ -27,20 +26,27 @@ using Mono.Data.Sqlite;
namespace Vocaluxe.Lib.Database
{
public class CDatabaseBase
public abstract class CDatabaseBase
{
protected readonly string _FilePath;
protected SQLiteConnection _Connection;
//You have to lock all actions using connection or transaction, otherwhise order is not guaranted
protected readonly object _Mutex = new object();
protected int _Version; //Version of the database (value of row "Value" in table "Version") or -1 if none
public CDatabaseBase(string filePath)
protected CDatabaseBase(string filePath)
{
_FilePath = filePath;
}
public virtual bool Init()
{
lock (_Mutex)
{
if (_Connection != null)
return false;
_Connection = new SQLiteConnection("Data Source=" + _FilePath);
try
{
@@ -73,10 +79,13 @@ namespace Vocaluxe.Lib.Database
if (reader != null)
reader.Dispose();
}
}
return true;
}
public virtual void Close()
{
lock (_Mutex)
{
if (_Connection != null)
{
@@ -84,6 +93,7 @@ namespace Vocaluxe.Lib.Database
_Connection = null;
}
}
}
protected static long _UnixTimeToTicks(int unixTime)
{
@@ -92,6 +102,13 @@ namespace Vocaluxe.Lib.Database
return t70.Ticks;
}
/// <summary>
/// Reads the contents of a field as a byte array <br />
/// Assumes you did the locking!
/// </summary>
/// <param name="reader"></param>
/// <param name="field">Field number to read from (default=first)</param>
/// <returns></returns>
protected static byte[] _GetBytes(SQLiteDataReader reader, int field = 0)
{
const int chunkSize = 2 * 1024;