rework repo disposal

This commit is contained in:
Luke Pulverenti
2015-05-02 18:59:01 -04:00
parent ee2fbf59d0
commit ae99233759
8 changed files with 165 additions and 332 deletions

View File

@@ -16,11 +16,15 @@ namespace MediaBrowser.Server.Implementations.Persistence
/// <summary>
/// Class SQLiteDisplayPreferencesRepository
/// </summary>
public class SqliteDisplayPreferencesRepository : IDisplayPreferencesRepository
public class SqliteDisplayPreferencesRepository : BaseSqliteRepository, IDisplayPreferencesRepository
{
private IDbConnection _connection;
private readonly ILogger _logger;
public SqliteDisplayPreferencesRepository(ILogManager logManager, IJsonSerializer jsonSerializer, IApplicationPaths appPaths) : base(logManager)
{
_jsonSerializer = jsonSerializer;
_appPaths = appPaths;
}
/// <summary>
/// Gets the name of the repository
@@ -44,36 +48,6 @@ namespace MediaBrowser.Server.Implementations.Persistence
/// </summary>
private readonly IApplicationPaths _appPaths;
private readonly SemaphoreSlim _writeLock = new SemaphoreSlim(1, 1);
/// <summary>
/// Initializes a new instance of the <see cref="SqliteDisplayPreferencesRepository" /> class.
/// </summary>
/// <param name="appPaths">The app paths.</param>
/// <param name="jsonSerializer">The json serializer.</param>
/// <param name="logManager">The log manager.</param>
/// <exception cref="System.ArgumentNullException">
/// jsonSerializer
/// or
/// appPaths
/// </exception>
public SqliteDisplayPreferencesRepository(IApplicationPaths appPaths, IJsonSerializer jsonSerializer, ILogManager logManager)
{
if (jsonSerializer == null)
{
throw new ArgumentNullException("jsonSerializer");
}
if (appPaths == null)
{
throw new ArgumentNullException("appPaths");
}
_jsonSerializer = jsonSerializer;
_appPaths = appPaths;
_logger = logManager.GetLogger(GetType().Name);
}
/// <summary>
/// Opens the connection to the database
/// </summary>
@@ -82,7 +56,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
{
var dbFile = Path.Combine(_appPaths.DataPath, "displaypreferences.db");
_connection = await SqliteExtensions.ConnectToDb(dbFile, _logger).ConfigureAwait(false);
_connection = await SqliteExtensions.ConnectToDb(dbFile, Logger).ConfigureAwait(false);
string[] queries = {
@@ -95,7 +69,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
"pragma shrink_memory"
};
_connection.RunQueries(queries, _logger);
_connection.RunQueries(queries, Logger);
}
/// <summary>
@@ -122,7 +96,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
var serialized = _jsonSerializer.SerializeToBytes(displayPreferences);
await _writeLock.WaitAsync(cancellationToken).ConfigureAwait(false);
await WriteLock.WaitAsync(cancellationToken).ConfigureAwait(false);
IDbTransaction transaction = null;
@@ -157,7 +131,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
}
catch (Exception e)
{
_logger.ErrorException("Failed to save display preferences:", e);
Logger.ErrorException("Failed to save display preferences:", e);
if (transaction != null)
{
@@ -173,7 +147,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
transaction.Dispose();
}
_writeLock.Release();
WriteLock.Release();
}
}
@@ -194,7 +168,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
cancellationToken.ThrowIfCancellationRequested();
await _writeLock.WaitAsync(cancellationToken).ConfigureAwait(false);
await WriteLock.WaitAsync(cancellationToken).ConfigureAwait(false);
IDbTransaction transaction = null;
@@ -235,7 +209,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
}
catch (Exception e)
{
_logger.ErrorException("Failed to save display preferences:", e);
Logger.ErrorException("Failed to save display preferences:", e);
if (transaction != null)
{
@@ -251,7 +225,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
transaction.Dispose();
}
_writeLock.Release();
WriteLock.Release();
}
}
@@ -322,45 +296,17 @@ namespace MediaBrowser.Server.Implementations.Persistence
}
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
protected override void CloseConnection()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private readonly object _disposeLock = new object();
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected virtual void Dispose(bool dispose)
{
if (dispose)
if (_connection != null)
{
try
if (_connection.IsOpen())
{
lock (_disposeLock)
{
if (_connection != null)
{
if (_connection.IsOpen())
{
_connection.Close();
}
_connection.Close();
}
_connection.Dispose();
_connection = null;
}
}
}
catch (Exception ex)
{
_logger.ErrorException("Error disposing database", ex);
}
_connection.Dispose();
_connection = null;
}
}
}