Back to a single connection
This commit is contained in:
parent
30842656a7
commit
27c29bbb4c
|
@ -43,7 +43,6 @@ namespace Emby.Server.Implementations.Activity
|
||||||
|
|
||||||
private void InitializeInternal()
|
private void InitializeInternal()
|
||||||
{
|
{
|
||||||
CreateConnections().GetAwaiter().GetResult();
|
|
||||||
using (var connection = GetConnection())
|
using (var connection = GetConnection())
|
||||||
{
|
{
|
||||||
RunDefaultInitialization(connection);
|
RunDefaultInitialization(connection);
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Concurrent;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using SQLitePCL;
|
using SQLitePCL;
|
||||||
using SQLitePCL.pretty;
|
using SQLitePCL.pretty;
|
||||||
|
@ -33,8 +31,6 @@ namespace Emby.Server.Implementations.Data
|
||||||
|
|
||||||
private SQLiteDatabaseConnection WriteConnection;
|
private SQLiteDatabaseConnection WriteConnection;
|
||||||
|
|
||||||
private readonly BlockingCollection<SQLiteDatabaseConnection> ReadConnectionPool = new BlockingCollection<SQLiteDatabaseConnection>();
|
|
||||||
|
|
||||||
static BaseSqliteRepository()
|
static BaseSqliteRepository()
|
||||||
{
|
{
|
||||||
ThreadSafeMode = raw.sqlite3_threadsafe();
|
ThreadSafeMode = raw.sqlite3_threadsafe();
|
||||||
|
@ -43,70 +39,37 @@ namespace Emby.Server.Implementations.Data
|
||||||
|
|
||||||
private string _defaultWal;
|
private string _defaultWal;
|
||||||
|
|
||||||
protected async Task CreateConnections()
|
|
||||||
{
|
|
||||||
await WriteLock.WaitAsync().ConfigureAwait(false);
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
if (WriteConnection == null)
|
|
||||||
{
|
|
||||||
WriteConnection = SQLite3.Open(
|
|
||||||
DbFilePath,
|
|
||||||
DefaultConnectionFlags | ConnectionFlags.Create | ConnectionFlags.ReadWrite,
|
|
||||||
null);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(_defaultWal))
|
|
||||||
{
|
|
||||||
_defaultWal = WriteConnection.Query("PRAGMA journal_mode").SelectScalarString().First();
|
|
||||||
|
|
||||||
Logger.LogInformation("Default journal_mode for {0} is {1}", DbFilePath, _defaultWal);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (EnableTempStoreMemory)
|
|
||||||
{
|
|
||||||
WriteConnection.Execute("PRAGMA temp_store = memory");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
WriteConnection.Execute("PRAGMA temp_store = file");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
WriteLock.Release();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add one reading connection for each thread
|
|
||||||
int threads = System.Environment.ProcessorCount;
|
|
||||||
for (int i = 0; i <= threads; i++)
|
|
||||||
{
|
|
||||||
ReadConnectionPool.Add(SQLite3.Open(DbFilePath, DefaultConnectionFlags | ConnectionFlags.ReadOnly, null));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected ManagedConnection GetConnection(bool isReadOnly = false)
|
protected ManagedConnection GetConnection(bool isReadOnly = false)
|
||||||
{
|
{
|
||||||
if (isReadOnly)
|
WriteLock.Wait();
|
||||||
|
if (WriteConnection != null)
|
||||||
{
|
{
|
||||||
return new ManagedConnection(ReadConnectionPool.Take(), ReadConnectionPool);
|
return new ManagedConnection(WriteConnection, WriteLock);
|
||||||
|
}
|
||||||
|
|
||||||
|
WriteConnection = SQLite3.Open(
|
||||||
|
DbFilePath,
|
||||||
|
DefaultConnectionFlags | ConnectionFlags.Create | ConnectionFlags.ReadWrite,
|
||||||
|
null);
|
||||||
|
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(_defaultWal))
|
||||||
|
{
|
||||||
|
_defaultWal = WriteConnection.Query("PRAGMA journal_mode").SelectScalarString().First();
|
||||||
|
|
||||||
|
Logger.LogInformation("Default journal_mode for {0} is {1}", DbFilePath, _defaultWal);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (EnableTempStoreMemory)
|
||||||
|
{
|
||||||
|
WriteConnection.Execute("PRAGMA temp_store = memory");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (WriteConnection == null)
|
WriteConnection.Execute("PRAGMA temp_store = file");
|
||||||
{
|
|
||||||
throw new InvalidOperationException("Can't access the write connection at this time.");
|
|
||||||
}
|
|
||||||
|
|
||||||
WriteLock.Wait();
|
|
||||||
return new ManagedConnection(WriteConnection, WriteLock);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return new ManagedConnection(WriteConnection, WriteLock);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IStatement PrepareStatement(ManagedConnection connection, string sql)
|
public IStatement PrepareStatement(ManagedConnection connection, string sql)
|
||||||
|
@ -217,14 +180,11 @@ namespace Emby.Server.Implementations.Data
|
||||||
WriteLock.Release();
|
WriteLock.Release();
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var i in ReadConnectionPool)
|
WriteLock.Dispose();
|
||||||
{
|
|
||||||
i.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
ReadConnectionPool.Dispose();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
WriteConnection = null;
|
||||||
|
|
||||||
_disposed = true;
|
_disposed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Concurrent;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using SQLitePCL.pretty;
|
using SQLitePCL.pretty;
|
||||||
|
@ -9,8 +8,7 @@ namespace Emby.Server.Implementations.Data
|
||||||
public class ManagedConnection : IDisposable
|
public class ManagedConnection : IDisposable
|
||||||
{
|
{
|
||||||
private SQLiteDatabaseConnection _db;
|
private SQLiteDatabaseConnection _db;
|
||||||
private SemaphoreSlim _writeLock;
|
private readonly SemaphoreSlim _writeLock;
|
||||||
private BlockingCollection<SQLiteDatabaseConnection> _readConPool;
|
|
||||||
private bool _disposed = false;
|
private bool _disposed = false;
|
||||||
|
|
||||||
public ManagedConnection(SQLiteDatabaseConnection db, SemaphoreSlim writeLock)
|
public ManagedConnection(SQLiteDatabaseConnection db, SemaphoreSlim writeLock)
|
||||||
|
@ -19,12 +17,6 @@ namespace Emby.Server.Implementations.Data
|
||||||
_writeLock = writeLock;
|
_writeLock = writeLock;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ManagedConnection(SQLiteDatabaseConnection db, BlockingCollection<SQLiteDatabaseConnection> queue)
|
|
||||||
{
|
|
||||||
_db = db;
|
|
||||||
_readConPool = queue;
|
|
||||||
}
|
|
||||||
|
|
||||||
public IStatement PrepareStatement(string sql)
|
public IStatement PrepareStatement(string sql)
|
||||||
{
|
{
|
||||||
return _db.PrepareStatement(sql);
|
return _db.PrepareStatement(sql);
|
||||||
|
@ -77,8 +69,7 @@ namespace Emby.Server.Implementations.Data
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
_writeLock?.Release();
|
_writeLock.Release();
|
||||||
_readConPool?.Add(_db);
|
|
||||||
|
|
||||||
_db = null; // Don't dispose it
|
_db = null; // Don't dispose it
|
||||||
_disposed = true;
|
_disposed = true;
|
||||||
|
|
|
@ -61,7 +61,6 @@ namespace Emby.Server.Implementations.Data
|
||||||
/// <returns>Task.</returns>
|
/// <returns>Task.</returns>
|
||||||
private void InitializeInternal()
|
private void InitializeInternal()
|
||||||
{
|
{
|
||||||
CreateConnections().GetAwaiter().GetResult();
|
|
||||||
using (var connection = GetConnection())
|
using (var connection = GetConnection())
|
||||||
{
|
{
|
||||||
RunDefaultInitialization(connection);
|
RunDefaultInitialization(connection);
|
||||||
|
|
|
@ -99,7 +99,6 @@ namespace Emby.Server.Implementations.Data
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Initialize(SqliteUserDataRepository userDataRepo, IUserManager userManager)
|
public void Initialize(SqliteUserDataRepository userDataRepo, IUserManager userManager)
|
||||||
{
|
{
|
||||||
CreateConnections().GetAwaiter().GetResult();
|
|
||||||
using (var connection = GetConnection())
|
using (var connection = GetConnection())
|
||||||
{
|
{
|
||||||
RunDefaultInitialization(connection);
|
RunDefaultInitialization(connection);
|
||||||
|
|
|
@ -34,7 +34,6 @@ namespace Emby.Server.Implementations.Data
|
||||||
/// <returns>Task.</returns>
|
/// <returns>Task.</returns>
|
||||||
public void Initialize(IUserManager userManager)
|
public void Initialize(IUserManager userManager)
|
||||||
{
|
{
|
||||||
CreateConnections().GetAwaiter().GetResult();
|
|
||||||
using (var connection = GetConnection())
|
using (var connection = GetConnection())
|
||||||
{
|
{
|
||||||
var userDatasTableExists = TableExists(connection, "UserDatas");
|
var userDatasTableExists = TableExists(connection, "UserDatas");
|
||||||
|
|
|
@ -40,7 +40,6 @@ namespace Emby.Server.Implementations.Data
|
||||||
/// <returns>Task.</returns>
|
/// <returns>Task.</returns>
|
||||||
public void Initialize()
|
public void Initialize()
|
||||||
{
|
{
|
||||||
CreateConnections().GetAwaiter().GetResult();
|
|
||||||
using (var connection = GetConnection())
|
using (var connection = GetConnection())
|
||||||
{
|
{
|
||||||
RunDefaultInitialization(connection);
|
RunDefaultInitialization(connection);
|
||||||
|
@ -90,8 +89,7 @@ namespace Emby.Server.Implementations.Data
|
||||||
user.Password = null;
|
user.Password = null;
|
||||||
var serialized = _jsonSerializer.SerializeToBytes(user);
|
var serialized = _jsonSerializer.SerializeToBytes(user);
|
||||||
|
|
||||||
using (WriteLock.Write())
|
using (var connection = GetConnection())
|
||||||
using (var connection = CreateConnection())
|
|
||||||
{
|
{
|
||||||
connection.RunInTransaction(db =>
|
connection.RunInTransaction(db =>
|
||||||
{
|
{
|
||||||
|
|
|
@ -23,7 +23,6 @@ namespace Emby.Server.Implementations.Security
|
||||||
|
|
||||||
public void Initialize()
|
public void Initialize()
|
||||||
{
|
{
|
||||||
CreateConnections().GetAwaiter().GetResult();
|
|
||||||
using (var connection = GetConnection())
|
using (var connection = GetConnection())
|
||||||
{
|
{
|
||||||
RunDefaultInitialization(connection);
|
RunDefaultInitialization(connection);
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using MediaBrowser.Controller.Net;
|
using MediaBrowser.Controller.Net;
|
||||||
using MediaBrowser.Model.Events;
|
using MediaBrowser.Model.Events;
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using MediaBrowser.Controller.Library;
|
using MediaBrowser.Controller.Library;
|
||||||
using MediaBrowser.Controller.Net;
|
using MediaBrowser.Controller.Net;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user