mirror of
https://github.com/jellyfin/jellyfin.git
synced 2025-12-18 23:13:06 +03:00
use individual connections
This commit is contained in:
@@ -17,14 +17,13 @@ namespace MediaBrowser.Server.Implementations.Persistence
|
||||
/// </summary>
|
||||
public class SqliteUserRepository : BaseSqliteRepository, IUserRepository
|
||||
{
|
||||
private IDbConnection _connection;
|
||||
private readonly IServerApplicationPaths _appPaths;
|
||||
private readonly IJsonSerializer _jsonSerializer;
|
||||
|
||||
public SqliteUserRepository(ILogManager logManager, IServerApplicationPaths appPaths, IJsonSerializer jsonSerializer) : base(logManager)
|
||||
public SqliteUserRepository(ILogManager logManager, IServerApplicationPaths appPaths, IJsonSerializer jsonSerializer, IDbConnector dbConnector) : base(logManager, dbConnector)
|
||||
{
|
||||
_appPaths = appPaths;
|
||||
_jsonSerializer = jsonSerializer;
|
||||
|
||||
DbFilePath = Path.Combine(appPaths.DataPath, "users.db");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -43,13 +42,11 @@ namespace MediaBrowser.Server.Implementations.Persistence
|
||||
/// Opens the connection to the database
|
||||
/// </summary>
|
||||
/// <returns>Task.</returns>
|
||||
public async Task Initialize(IDbConnector dbConnector)
|
||||
public async Task Initialize()
|
||||
{
|
||||
var dbFile = Path.Combine(_appPaths.DataPath, "users.db");
|
||||
|
||||
_connection = await dbConnector.Connect(dbFile).ConfigureAwait(false);
|
||||
|
||||
string[] queries = {
|
||||
using (var connection = await CreateConnection().ConfigureAwait(false))
|
||||
{
|
||||
string[] queries = {
|
||||
|
||||
"create table if not exists users (guid GUID primary key, data BLOB)",
|
||||
"create index if not exists idx_users on users(guid)",
|
||||
@@ -61,7 +58,8 @@ namespace MediaBrowser.Server.Implementations.Persistence
|
||||
"pragma shrink_memory"
|
||||
};
|
||||
|
||||
_connection.RunQueries(queries, Logger);
|
||||
connection.RunQueries(queries, Logger);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -84,55 +82,54 @@ namespace MediaBrowser.Server.Implementations.Persistence
|
||||
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
await WriteLock.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
IDbTransaction transaction = null;
|
||||
|
||||
try
|
||||
using (var connection = await CreateConnection().ConfigureAwait(false))
|
||||
{
|
||||
transaction = _connection.BeginTransaction();
|
||||
IDbTransaction transaction = null;
|
||||
|
||||
using (var cmd = _connection.CreateCommand())
|
||||
try
|
||||
{
|
||||
cmd.CommandText = "replace into users (guid, data) values (@1, @2)";
|
||||
cmd.Parameters.Add(cmd, "@1", DbType.Guid).Value = user.Id;
|
||||
cmd.Parameters.Add(cmd, "@2", DbType.Binary).Value = serialized;
|
||||
transaction = connection.BeginTransaction();
|
||||
|
||||
cmd.Transaction = transaction;
|
||||
using (var cmd = connection.CreateCommand())
|
||||
{
|
||||
cmd.CommandText = "replace into users (guid, data) values (@1, @2)";
|
||||
cmd.Parameters.Add(cmd, "@1", DbType.Guid).Value = user.Id;
|
||||
cmd.Parameters.Add(cmd, "@2", DbType.Binary).Value = serialized;
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
cmd.Transaction = transaction;
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
transaction.Commit();
|
||||
}
|
||||
|
||||
transaction.Commit();
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
if (transaction != null)
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
transaction.Rollback();
|
||||
if (transaction != null)
|
||||
{
|
||||
transaction.Rollback();
|
||||
}
|
||||
|
||||
throw;
|
||||
}
|
||||
|
||||
throw;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.ErrorException("Failed to save user:", e);
|
||||
|
||||
if (transaction != null)
|
||||
catch (Exception e)
|
||||
{
|
||||
transaction.Rollback();
|
||||
}
|
||||
Logger.ErrorException("Failed to save user:", e);
|
||||
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (transaction != null)
|
||||
if (transaction != null)
|
||||
{
|
||||
transaction.Rollback();
|
||||
}
|
||||
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
transaction.Dispose();
|
||||
if (transaction != null)
|
||||
{
|
||||
transaction.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
WriteLock.Release();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,25 +139,32 @@ namespace MediaBrowser.Server.Implementations.Persistence
|
||||
/// <returns>IEnumerable{User}.</returns>
|
||||
public IEnumerable<User> RetrieveAllUsers()
|
||||
{
|
||||
using (var cmd = _connection.CreateCommand())
|
||||
var list = new List<User>();
|
||||
|
||||
using (var connection = CreateConnection(true).Result)
|
||||
{
|
||||
cmd.CommandText = "select guid,data from users";
|
||||
|
||||
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult))
|
||||
using (var cmd = connection.CreateCommand())
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
var id = reader.GetGuid(0);
|
||||
cmd.CommandText = "select guid,data from users";
|
||||
|
||||
using (var stream = reader.GetMemoryStream(1))
|
||||
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult))
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
var user = _jsonSerializer.DeserializeFromStream<User>(stream);
|
||||
user.Id = id;
|
||||
yield return user;
|
||||
var id = reader.GetGuid(0);
|
||||
|
||||
using (var stream = reader.GetMemoryStream(1))
|
||||
{
|
||||
var user = _jsonSerializer.DeserializeFromStream<User>(stream);
|
||||
user.Id = id;
|
||||
list.Add(user);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -179,69 +183,54 @@ namespace MediaBrowser.Server.Implementations.Persistence
|
||||
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
await WriteLock.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
IDbTransaction transaction = null;
|
||||
|
||||
try
|
||||
using (var connection = await CreateConnection().ConfigureAwait(false))
|
||||
{
|
||||
transaction = _connection.BeginTransaction();
|
||||
IDbTransaction transaction = null;
|
||||
|
||||
using (var cmd = _connection.CreateCommand())
|
||||
try
|
||||
{
|
||||
cmd.CommandText = "delete from users where guid=@guid";
|
||||
transaction = connection.BeginTransaction();
|
||||
|
||||
cmd.Parameters.Add(cmd, "@guid", DbType.Guid).Value = user.Id;
|
||||
using (var cmd = connection.CreateCommand())
|
||||
{
|
||||
cmd.CommandText = "delete from users where guid=@guid";
|
||||
|
||||
cmd.Transaction = transaction;
|
||||
cmd.Parameters.Add(cmd, "@guid", DbType.Guid).Value = user.Id;
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
cmd.Transaction = transaction;
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
transaction.Commit();
|
||||
}
|
||||
|
||||
transaction.Commit();
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
if (transaction != null)
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
transaction.Rollback();
|
||||
if (transaction != null)
|
||||
{
|
||||
transaction.Rollback();
|
||||
}
|
||||
|
||||
throw;
|
||||
}
|
||||
|
||||
throw;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.ErrorException("Failed to delete user:", e);
|
||||
|
||||
if (transaction != null)
|
||||
catch (Exception e)
|
||||
{
|
||||
transaction.Rollback();
|
||||
}
|
||||
Logger.ErrorException("Failed to delete user:", e);
|
||||
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (transaction != null)
|
||||
if (transaction != null)
|
||||
{
|
||||
transaction.Rollback();
|
||||
}
|
||||
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
transaction.Dispose();
|
||||
if (transaction != null)
|
||||
{
|
||||
transaction.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
WriteLock.Release();
|
||||
}
|
||||
}
|
||||
|
||||
protected override void CloseConnection()
|
||||
{
|
||||
if (_connection != null)
|
||||
{
|
||||
if (_connection.IsOpen())
|
||||
{
|
||||
_connection.Close();
|
||||
}
|
||||
|
||||
_connection.Dispose();
|
||||
_connection = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user