chore: Add a read only connection for routes like Shows/NextUp

This commit is contained in:
Jayson Reis 2022-01-22 21:52:30 +00:00
parent cd4587b43f
commit 2e4dd02f76
No known key found for this signature in database
GPG Key ID: 32CAA472083D88E2
2 changed files with 11 additions and 3 deletions

View File

@ -97,9 +97,16 @@ namespace Emby.Server.Implementations.Data
/// </summary> /// </summary>
/// <value>The write connection.</value> /// <value>The write connection.</value>
protected SQLiteDatabaseConnection WriteConnection { get; set; } protected SQLiteDatabaseConnection WriteConnection { get; set; }
protected SQLiteDatabaseConnection ReadConnection { get; set; }
protected ManagedConnection GetConnection(bool readOnly = false) protected ManagedConnection GetConnection(bool readOnly = false)
{ {
if (readOnly)
{
ReadConnection ??= SQLite3.Open(DbFilePath, DefaultConnectionFlags | ConnectionFlags.ReadOnly, null);
return new ManagedConnection(ReadConnection, null);
}
WriteLock.Wait(); WriteLock.Wait();
if (WriteConnection != null) if (WriteConnection != null)
{ {

View File

@ -9,11 +9,12 @@ namespace Emby.Server.Implementations.Data
{ {
public sealed class ManagedConnection : IDisposable public sealed class ManagedConnection : IDisposable
{ {
private readonly SemaphoreSlim _writeLock; private readonly SemaphoreSlim? _writeLock;
private SQLiteDatabaseConnection? _db; private SQLiteDatabaseConnection? _db;
private bool _disposed = false; private bool _disposed;
public ManagedConnection(SQLiteDatabaseConnection db, SemaphoreSlim writeLock) public ManagedConnection(SQLiteDatabaseConnection db, SemaphoreSlim writeLock)
{ {
@ -73,7 +74,7 @@ namespace Emby.Server.Implementations.Data
return; return;
} }
_writeLock.Release(); _writeLock?.Release();
_db = null; // Don't dispose it _db = null; // Don't dispose it
_disposed = true; _disposed = true;