update save methods

pull/702/head
Luke Pulverenti 8 years ago
parent ffb1ec76a7
commit 52227ce00d

@ -51,7 +51,7 @@ namespace Emby.Server.Implementations.Activity
throw new ArgumentNullException("entry"); throw new ArgumentNullException("entry");
} }
lock (WriteLock) using (WriteLock.Write())
{ {
using (var connection = CreateConnection()) using (var connection = CreateConnection())
{ {
@ -76,7 +76,7 @@ namespace Emby.Server.Implementations.Activity
public QueryResult<ActivityLogEntry> GetActivityLogEntries(DateTime? minDate, int? startIndex, int? limit) public QueryResult<ActivityLogEntry> GetActivityLogEntries(DateTime? minDate, int? startIndex, int? limit)
{ {
lock (WriteLock) using (WriteLock.Read())
{ {
using (var connection = CreateConnection(true)) using (var connection = CreateConnection(true))
{ {

@ -12,7 +12,7 @@ namespace Emby.Server.Implementations.Data
public abstract class BaseSqliteRepository : IDisposable public abstract class BaseSqliteRepository : IDisposable
{ {
protected string DbFilePath { get; set; } protected string DbFilePath { get; set; }
protected SemaphoreSlim WriteLock = new SemaphoreSlim(1, 1); protected ReaderWriterLockSlim WriteLock = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion);
protected ILogger Logger { get; private set; } protected ILogger Logger { get; private set; }
protected BaseSqliteRepository(ILogger logger) protected BaseSqliteRepository(ILogger logger)
@ -130,11 +130,12 @@ namespace Emby.Server.Implementations.Data
{ {
lock (_disposeLock) lock (_disposeLock)
{ {
WriteLock.Wait(); using (WriteLock.Write())
{
CloseConnection(); CloseConnection();
} }
} }
}
catch (Exception ex) catch (Exception ex)
{ {
Logger.ErrorException("Error disposing database", ex); Logger.ErrorException("Error disposing database", ex);
@ -178,4 +179,51 @@ namespace Emby.Server.Implementations.Data
})); }));
} }
} }
public static class ReaderWriterLockSlimExtensions
{
private sealed class ReadLockToken : IDisposable
{
private ReaderWriterLockSlim _sync;
public ReadLockToken(ReaderWriterLockSlim sync)
{
_sync = sync;
sync.EnterReadLock();
}
public void Dispose()
{
if (_sync != null)
{
_sync.ExitReadLock();
_sync = null;
}
}
}
private sealed class WriteLockToken : IDisposable
{
private ReaderWriterLockSlim _sync;
public WriteLockToken(ReaderWriterLockSlim sync)
{
_sync = sync;
sync.EnterWriteLock();
}
public void Dispose()
{
if (_sync != null)
{
_sync.ExitWriteLock();
_sync = null;
}
}
}
public static IDisposable Read(this ReaderWriterLockSlim obj)
{
return new ReadLockToken(obj);
}
public static IDisposable Write(this ReaderWriterLockSlim obj)
{
return new WriteLockToken(obj);
}
}
} }

@ -86,7 +86,7 @@ namespace Emby.Server.Implementations.Data
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
lock (WriteLock) using (WriteLock.Write())
{ {
using (var connection = CreateConnection()) using (var connection = CreateConnection())
{ {
@ -127,7 +127,7 @@ namespace Emby.Server.Implementations.Data
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
lock (WriteLock) using (WriteLock.Write())
{ {
using (var connection = CreateConnection()) using (var connection = CreateConnection())
{ {
@ -159,6 +159,8 @@ namespace Emby.Server.Implementations.Data
var guidId = displayPreferencesId.GetMD5(); var guidId = displayPreferencesId.GetMD5();
using (WriteLock.Read())
{
using (var connection = CreateConnection(true)) using (var connection = CreateConnection(true))
{ {
var commandText = "select data from userdisplaypreferences where id = ? and userId=? and client=?"; var commandText = "select data from userdisplaypreferences where id = ? and userId=? and client=?";
@ -179,6 +181,7 @@ namespace Emby.Server.Implementations.Data
}; };
} }
} }
}
/// <summary> /// <summary>
/// Gets all display preferences for the given user. /// Gets all display preferences for the given user.
@ -190,6 +193,8 @@ namespace Emby.Server.Implementations.Data
{ {
var list = new List<DisplayPreferences>(); var list = new List<DisplayPreferences>();
using (WriteLock.Read())
{
using (var connection = CreateConnection(true)) using (var connection = CreateConnection(true))
{ {
var commandText = "select data from userdisplaypreferences where userId=?"; var commandText = "select data from userdisplaypreferences where userId=?";
@ -202,6 +207,7 @@ namespace Emby.Server.Implementations.Data
list.Add(Get(row)); list.Add(Get(row));
} }
} }
}
return list; return list;
} }

@ -83,7 +83,7 @@ namespace Emby.Server.Implementations.Data
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
lock (WriteLock) using (WriteLock.Write())
{ {
using (var connection = CreateConnection()) using (var connection = CreateConnection())
{ {
@ -107,6 +107,8 @@ namespace Emby.Server.Implementations.Data
{ {
var list = new List<User>(); var list = new List<User>();
using (WriteLock.Read())
{
using (var connection = CreateConnection(true)) using (var connection = CreateConnection(true))
{ {
foreach (var row in connection.Query("select guid,data from users")) foreach (var row in connection.Query("select guid,data from users"))
@ -122,6 +124,7 @@ namespace Emby.Server.Implementations.Data
} }
} }
} }
}
return list; return list;
} }
@ -142,7 +145,7 @@ namespace Emby.Server.Implementations.Data
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
lock (WriteLock) using (WriteLock.Write())
{ {
using (var connection = CreateConnection()) using (var connection = CreateConnection())
{ {

@ -48,7 +48,7 @@ namespace Emby.Server.Implementations.Notifications
{ {
var result = new NotificationResult(); var result = new NotificationResult();
lock (WriteLock) using (WriteLock.Read())
{ {
using (var connection = CreateConnection(true)) using (var connection = CreateConnection(true))
{ {
@ -103,7 +103,7 @@ namespace Emby.Server.Implementations.Notifications
{ {
var result = new NotificationsSummary(); var result = new NotificationsSummary();
lock (WriteLock) using (WriteLock.Read())
{ {
using (var connection = CreateConnection(true)) using (var connection = CreateConnection(true))
{ {
@ -214,7 +214,7 @@ namespace Emby.Server.Implementations.Notifications
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
lock (WriteLock) using (WriteLock.Write())
{ {
using (var connection = CreateConnection()) using (var connection = CreateConnection())
{ {
@ -273,7 +273,7 @@ namespace Emby.Server.Implementations.Notifications
{ {
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
lock (WriteLock) using (WriteLock.Write())
{ {
using (var connection = CreateConnection()) using (var connection = CreateConnection())
{ {
@ -289,7 +289,7 @@ namespace Emby.Server.Implementations.Notifications
{ {
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
lock (WriteLock) using (WriteLock.Write())
{ {
using (var connection = CreateConnection()) using (var connection = CreateConnection())
{ {

@ -63,7 +63,7 @@ namespace Emby.Server.Implementations.Security
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
lock (WriteLock) using (WriteLock.Write())
{ {
using (var connection = CreateConnection()) using (var connection = CreateConnection())
{ {
@ -195,7 +195,7 @@ namespace Emby.Server.Implementations.Security
throw new ArgumentNullException("id"); throw new ArgumentNullException("id");
} }
lock (WriteLock) using (WriteLock.Read())
{ {
using (var connection = CreateConnection(true)) using (var connection = CreateConnection(true))
{ {

@ -50,7 +50,7 @@ namespace Emby.Server.Implementations.Social
throw new ArgumentNullException("info.Id"); throw new ArgumentNullException("info.Id");
} }
lock (WriteLock) using (WriteLock.Write())
{ {
using (var connection = CreateConnection()) using (var connection = CreateConnection())
{ {
@ -75,7 +75,7 @@ namespace Emby.Server.Implementations.Social
throw new ArgumentNullException("id"); throw new ArgumentNullException("id");
} }
lock (WriteLock) using (WriteLock.Read())
{ {
using (var connection = CreateConnection(true)) using (var connection = CreateConnection(true))
{ {

@ -95,7 +95,7 @@ namespace Emby.Server.Implementations.Sync
throw new ArgumentNullException("id"); throw new ArgumentNullException("id");
} }
lock (WriteLock) using (WriteLock.Read())
{ {
using (var connection = CreateConnection(true)) using (var connection = CreateConnection(true))
{ {
@ -206,7 +206,7 @@ namespace Emby.Server.Implementations.Sync
CheckDisposed(); CheckDisposed();
lock (WriteLock) using (WriteLock.Write())
{ {
using (var connection = CreateConnection()) using (var connection = CreateConnection())
{ {
@ -259,7 +259,7 @@ namespace Emby.Server.Implementations.Sync
CheckDisposed(); CheckDisposed();
lock (WriteLock) using (WriteLock.Write())
{ {
using (var connection = CreateConnection()) using (var connection = CreateConnection())
{ {
@ -281,7 +281,7 @@ namespace Emby.Server.Implementations.Sync
CheckDisposed(); CheckDisposed();
lock (WriteLock) using (WriteLock.Read())
{ {
using (var connection = CreateConnection(true)) using (var connection = CreateConnection(true))
{ {
@ -379,7 +379,7 @@ namespace Emby.Server.Implementations.Sync
CheckDisposed(); CheckDisposed();
lock (WriteLock) using (WriteLock.Read())
{ {
var guid = new Guid(id); var guid = new Guid(id);
@ -407,7 +407,7 @@ namespace Emby.Server.Implementations.Sync
throw new ArgumentNullException("query"); throw new ArgumentNullException("query");
} }
lock (WriteLock) using (WriteLock.Read())
{ {
using (var connection = CreateConnection(true)) using (var connection = CreateConnection(true))
{ {
@ -487,7 +487,7 @@ namespace Emby.Server.Implementations.Sync
var now = DateTime.UtcNow; var now = DateTime.UtcNow;
lock (WriteLock) using (WriteLock.Read())
{ {
using (var connection = CreateConnection(true)) using (var connection = CreateConnection(true))
{ {
@ -650,7 +650,7 @@ namespace Emby.Server.Implementations.Sync
CheckDisposed(); CheckDisposed();
lock (WriteLock) using (WriteLock.Write())
{ {
using (var connection = CreateConnection()) using (var connection = CreateConnection())
{ {

@ -996,7 +996,7 @@ namespace MediaBrowser.MediaEncoding.Probing
{ {
_splitWhiteList = new List<string> _splitWhiteList = new List<string>
{ {
"AC/DV" "AC/DC"
}; };
} }

Loading…
Cancel
Save